#!/bin/sh
##########################################################################
# Start of configurable variable... check everything from here down to....

PERL=`which perl`
GREP=`which grep`
#these only used by this install program:
TAR=`which tar`
INSTALL=`which install`
ED=`which ed`


BPHOME=/usr/local/lib/bp
PICSDIR=/usr/local/lib/pix
BINDEST=/usr/local/bin
SIXDIR=/usr/local/lib/sixpack
SIXDOC=$SIXDIR/doc
BIBXRESFILE=$SIXDIR/Bib


# CONFDEST is the location of the configuration file.
CONFDEST=/etc/sixpack.rc
# CONFDEST=~/.sixpack.rc

#these will be spliced into the conf files:
CONV=$BINDEST/bibconv
# WGET is optional. If not set, then importing from url isn't possible.
WGET=`which wget`
echo $WGET
which wget
# Comment out the following lines, if you use WGET, but set it to something non-executable.
if( test -x $WGET ) ; then {
echo ok
} ; else {
echo problem with $WGET
WGET="blahh"
}; 
fi

# ....here.
# You probably don't need to change anything below here, though.
#######################################################################

BP=bp-0.2.97

mkdir -p $BINDEST
mkdir -p $PICSDIR/sixpack
mkdir -p $SIXDIR
mkdir -p $SIXDOC

if ( test \( ""$1 = "with-bp" \) -o \( ""$2 = "with-bp" \) ) ; then {
  cat > edcoms <<END
/#
.d
0a
#!$PERL
.
w
q
END
  \rm -R $BP
  $TAR -z-xf $BP.tar.gz
  for a in $BP/bin/*pl ; do ed $a <edcoms ; done
  
  cat >edcoms <<END
?BPHOME =
.d
^a
BPHOME = $BPHOME
.
\$l
?BINDEST =
.d
^a
BINDEST = $BINDEST
.
w
q
END
  $ED $BP/Makefile <edcoms
  CURDIR=$PWD
  echo $PWD
  echo cd $BP
  cd $BP
  make install
  cd $CURDIR
};
fi

  cat > edcoms <<END
/#
.d
0a
#!$PERL
.
/globalCONFFILE
.d
^a
my \$globalCONFFILE = "$CONFDEST" ;
.
w
q
END

$ED bib <edcoms
$ED bib-remote <edcoms

cat > edcoms <<END
/CONV =
.d
^a
CONV = $BINDEST/bibconv
.
/GREP =
.d
^a
GREP = $GREP
.
/PICSDIR =
.d
^a
PICSDIR = $PICSDIR
.
/SIXDOC =
.d
^a
SIXDOC = $SIXDOC
.
/BPHOME =
.d
^a
BPHOME = $BPHOME
.
/BIBXRESFILE =
.d
^a
BIBXRESFILE = $BIBXRESFILE
.
END
if ( test ""$WGET != "" ) ; then {
echo wget found
cat >>edcoms <<END
/WGET =
.d
^a
WGET = $WGET
.
END
};
fi
cat >>edcoms <<END
w
q
END


$ED sixpack.rc <edcoms
$INSTALL -m 644 sixpack/Pics/* $PICSDIR/sixpack
$INSTALL -m 644 sixpack/doc/* $SIXDOC
$INSTALL -m 644 sixpack/Bib $BIBXRESFILE
$INSTALL -m 644 sixpack.rc $CONFDEST
$INSTALL -m 755 bib  $BINDEST/bib
$INSTALL -m 755 bib-remote $BINDEST/bib-remote

if ( test \( ""$1 = "with-mod" \) -o \( ""$2 = "with-mod" \) ) ; then {
$INSTALL -m 644 bp_additional_modules/bp-*pl $BPHOME
};
fi

\rm -f edcoms
