#!/bin/sh

MMDDHHMMYY=1215021096

LOGFILE=install.log

exit_or_cont() {
	status="$1"
	if [ "$status" != 0 ]; then
		while [ 0 ]; do
			echo -n 'E[x]it or [c]ontinue? '
			read answer
			if [ "$answer" = "x" ]; then
				exit 0
			elif [ "$answer" = "c" ]; then
				return 0
			fi
		done
	fi
	return 0
}

Inst() {
	echo $3 >> $LOGFILE
	rm -f $3
	cp $2 $3
	exit_or_cont $?
	chmod $1 $3
	exit_or_cont $?
	touch $MMDDHHMMYY $3
	exit_or_cont $?
	return $?
}

instexe() {
	Inst 755 $1 $2
	return $?
}

instdata() {
	Inst 444 $1 $2
	return $?
}

instcat() {
	Inst 644 $1 $2
	return $?
}

instlicense() {
	Inst 644 $1 $2
	return $?
}

######################################Initial settings

SRC=`pwd`
DEST="/usr/local"

CDRDIR=cdrprogs
BINDIR=BIN
DATADIR=fragdata

BABEL_DIR="babel-1.1gx"
SCRIPT_DIR="/usr/local/bin"
MAN_DIR=
for i in /usr/local/man /usr/contrib/man /usr/catman/local; do
	test -d $i && { MAN_DIR=$i; break; }
done
test -z "$MAN_DIR" && test -d /usr/catman && MAN_DIR=/usr/catman/local
test -z "$MAN_DIR" && MAN_DIR=/usr/local/man

XPRED_VER="XPred 1.12"
PROLOGD_VER="PrologD 2.01"
PKALC_VER="pKalc 3.21"
PROLOGP_VER="PrologP 5.11"
BABEL_VER="Babel 1.1gx"

rm -f $LOGFILE

###################################### Introduction
OLDDIR=`pwd`

cat << END


************************************************************

Welcome to the setup program for CompuDrug's prediction software.
You must have root privileges to run this setup. You can install
the following modules:

-	$XPRED_VER
-	$PROLOGD_VER
-	$PKALC_VER
-	$PROLOGP_VER
-	$BABEL_VER

In the following steps you can specify 
-	The source from which you are installing 
-	Which modules to install
-	The destination directories	
Finally setup will create scripts for initializing and running
the executables.

During setup an $LOGFILE file will be created.

END

################################## Install XPred?

while [ 0 ]; do
cat << END
XPred provides a friendly user interface to run predictions
END
	echo -n "Do you want to install $XPRED_VER ([y]es, [n]o)? "
	read INST_XPred
	if [ "$INST_XPred" = "y" -o  "$INST_XPred" = "n" ]; then
		break;
	fi
done

echo


################################### Install PrologD?

while [ 0 ]; do
cat << END
PrologD is a program for predicting logD (logarithm of the
distribution coefficient for n-octanol/water system) for user
defined pH values. Make sure that pKalc and PrologP are installed,
too, before using PrologD.
END
	echo -n "Do you want to install $PROLOGD_VER ([y]es, [n]o)? "
	read INST_PrologD
	if [ "$INST_PrologD" = "y" -o  "$INST_PrologD" = "n" ]; then
		break;
	fi
done

################################### Install pKalc?

while [ 0 ]; do
cat << END
pKalc is a program for predicting pKa."
END
	echo -n "Do you want to install $PKALC_VER ([y]es, [n]o)? "
	read INST_pKalc
	if [ "$INST_pKalc" = "y" -o  "$INST_pKalc" = "n" ]; then
		break;
	fi
done

################################### Install PrologP?

while [ 0 ]; do
cat << END
PrologP is a program for predicting logP (logarithm of the
partition coefficient for n-octanol/water system).
END
	echo -n "Do you want to install $PROLOGP_VER ([y]es, [n]o)? "
	read INST_PrologP
	if [ "$INST_PrologP" = "y" -o  "$INST_PrologP" = "n" ]; then
		break;
	fi
done


################################## Install Babel?

while [ 0 ]; do
	cat << END
Babel is a shareware program developed by the University of 
Arizona to interconvert a number of file formats used in molecular
modeling. If you don't have Babel or if you have an older version
please install it.
END
	echo -n "Do you want to install $BABEL_VER ([y]es, [n]o)? "
	read INST_Babel
	if [ "$INST_Babel" = "y" -o  "$INST_Babel" = "n" ]; then
		break;
	fi
done

echo

#################################### Specifying source

	cat << END

You will be prompted for names of directories. Please hit enter
to accept the default path, or specify the full path in each
case!

END

cat << END
Please specify the source directory you are installing from.
Just pressing enter will use '$SRC'
END
OLDSRC=$SRC
while [ 0 ]; do
	echo -n "Source? "
	read ASK
	if [ "$ASK" != "" ]; then
		SRC=$ASK
	fi
	if [ -d "$SRC" ]; then
		break
	else
		echo "Directory '$SRC' doesn't exist!"
		SRC=$OLDSRC
	fi

done

echo


############################# Destination

if [ "$INST_pKalc" = "y" -o "$INST_XPred" = "y"  -o "$INST_Babel" = "y" ]; then
	cat << END
Now, you may specify an existing destination directory where the
new directories should be added to contain the programs. 
WARNING! If older versions of the programs exist, the old files
may be overwritten.
If you have selected all programs for installation and you specify
$DEST then the following directories will be created:
	$DEST/$CDRDIR
	$DEST/$CDRDIR/$BINDIR
	$DEST/$CDRDIR/$DATADIR
	$DEST/$BABEL_DIR
Just pressing enter will use the default destination, 
'$DEST'
END
	OLDDEST=$DEST
	while [ 0 ]; do

		echo -n "Destination? "
		read ASK
		if [ "$ASK" != "" ]; then
			DEST=$ASK
		fi
		if [ -d "$DEST" ]; then
			break
		else
			echo "Directory '$DEST' doesn't exist!"
			DEST=$OLDDEST
		fi
	done

	echo
		
fi

#################################### Installing XPred

if [ "$INST_XPred" = "y" ]; then
	echo "Installing XPred to '$DEST/$CDRDIR' ...."
	if [ ! -d $DEST/$CDRDIR/$BINDIR ]; then mkdir -p $DEST/$CDRDIR/$BINDIR; fi
	instexe $SRC/bin/xpred $DEST/$CDRDIR/$BINDIR/xpred
	f=".text_extras_menu"
	instdata $SRC/$f $DEST/$CDRDIR/$f
	exit_or_cont $?
fi

#################################### Installing PrologD

if [ "$INST_PrologD" = "y" ]; then
	echo "Installing PrologD to '$DEST/$CDRDIR' ...."
	if [ ! -d $DEST/$CDRDIR/$BINDIR ]; then mkdir -p $DEST/$CDRDIR/$BINDIR; fi
	instexe $SRC/bin/prologd $DEST/$CDRDIR/$BINDIR/prologd
	exit_or_cont $?
fi

#################################### Installing pKalc

if [ "$INST_pKalc" = "y" ]; then
	echo "Installing pKalc to '$DEST/$CDRDIR' ...."
	if [ ! -d $DEST/$CDRDIR/$BINDIR ]; then mkdir -p $DEST/$CDRDIR/$BINDIR; fi
	if [ ! -d $DEST/$CDRDIR/$DATADIR ]; then mkdir -p $DEST/$CDRDIR/$DATADIR; fi
	instexe $SRC/bin/pkalc $DEST/$CDRDIR/$BINDIR/pkalc
	exit_or_cont $?
	for f in `cd fragdata; echo *.tab pkafrag.str`; do
		instdata $SRC/fragdata/$f $DEST/$CDRDIR/$DATADIR/$f
		exit_or_cont $?
	done
fi

#################################### Installing PrologP

if [ "$INST_PrologP" = "y" ]; then
	echo "Installing PrologP to '$DEST/$CDRDIR' ...."
	if [ ! -d $DEST/$CDRDIR/$BINDIR ]; then mkdir -p $DEST/$CDRDIR/$BINDIR; fi
	if [ ! -d $DEST/$CDRDIR/$DATADIR ]; then mkdir -p $DEST/$CDRDIR/$DATADIR; fi
	instexe $SRC/bin/prologp $DEST/$CDRDIR/$BINDIR/prologp
	exit_or_cont $?
	for f in `cd fragdata; echo *.lpf *.lps defatoms.tab`; do
		instdata $SRC/fragdata/$f $DEST/$CDRDIR/$DATADIR/$f
		exit_or_cont $?
	done
fi

#################################### Installing Babel

if [ "$INST_Babel" = "y" ]; then
	echo "Installing Babel to '$DEST/$BABEL_DIR' ...."
	if [ ! -d $DEST/$BABEL_DIR ]; then mkdir -p $DEST/$BABEL_DIR; fi
	for f in `cd babel; echo *`; do
		if [ -x $f ]; then
			instexe $SRC/babel/$f $DEST/$BABEL_DIR/$f
		else
			instdata $SRC/babel/$f $DEST/$BABEL_DIR/$f
		fi
		exit_or_cont $?
	done
fi

################################# Where to place scripts?

if [ "$INST_pKalc" = "y" -o "$INST_PrologP" = "y" -o "$INST_PrologD" = "y" -o "$INST_XPred" = "y" -o "$INST_Babel" = "y" ]; then
	cat << END
Now, specify a directory that is written in the PATH environmental
variable where some scripts will be written. These scripts will
set some environmental variables and call the programs. Just
pressing enter will use the default, '$SCRIPT_DIR'
END
	OLDSCRIPT_DIR=$SCRIPT_DIR
	while [ 0 ]; do
		echo -n "Directory for scripts? "
		read ASK
		if [ "$ASK" != "" ]; then
			SCRIPT_DIR=$ASK
		fi
		if [ -d "$SCRIPT_DIR" ]; then
			break
		else
			echo "Directory '$SCRIPT_DIR' doesn't exist!"
			SCRIPT_DIR=$OLDSCRIPT_DIR
		fi
	done

	echo
		
fi

########################## Creating script: xpred

if [ "$INST_XPred" = "y" ]; then
	echo "Creating script: '$SCRIPT_DIR/xpred'"
	if [ ! -d $SCRIPT_DIR ]; then mkdir -p $SCRIPT_DIR; fi
	cat << END >$SCRIPT_DIR/tmp$$
#!/bin/sh
CDRPROGS_DIR=$DEST/$CDRDIR
export CDRPROGS_DIR
if [ "\$BABEL_DIR" = "" ]; then
	BABEL_DIR=$DEST/$BABEL_DIR
	export BABEL_DIR
fi
\$CDRPROGS_DIR/$BINDIR/xpred \$*
exit \$?
END
	instexe $SCRIPT_DIR/tmp$$ $SCRIPT_DIR/xpred
	rm -f $SCRIPT_DIR/tmp$$
fi

########################## Creating script: prologd

if [ "$INST_PrologD" = "y"  ]; then
	echo "Creating script: '$SCRIPT_DIR/prologd'"
	if [ ! -d $SCRIPT_DIR ]; then mkdir -p $SCRIPT_DIR; fi
	cat << END >$SCRIPT_DIR/tmp$$
#!/bin/sh
CDRPROGS_DIR=$DEST/$CDRDIR
export CDRPROGS_DIR
if [ "\$BABEL_DIR" = "" ]; then
	BABEL_DIR=$DEST/$BABEL_DIR
	export BABEL_DIR
fi
\$CDRPROGS_DIR/$BINDIR/prologd \$*
exit \$?
END
	instexe $SCRIPT_DIR/tmp$$ $SCRIPT_DIR/prologd
	rm -f $SCRIPT_DIR/tmp$$
fi
########################## Creating script: pkalc

if [ "$INST_pKalc" = "y"  ]; then
	echo "Creating script: '$SCRIPT_DIR/pkalc'"
	if [ ! -d $SCRIPT_DIR ]; then mkdir -p $SCRIPT_DIR; fi
	cat << END >$SCRIPT_DIR/tmp$$
#!/bin/sh
CDRPROGS_DIR=$DEST/$CDRDIR
export CDRPROGS_DIR
if [ "\$BABEL_DIR" = "" ]; then
	BABEL_DIR=$DEST/$BABEL_DIR
	export BABEL_DIR
fi
\$CDRPROGS_DIR/$BINDIR/pkalc \$*
exit \$?
END
	instexe $SCRIPT_DIR/tmp$$ $SCRIPT_DIR/pkalc
	rm -f $SCRIPT_DIR/tmp$$
fi


########################## Creating script: prologp

if [ "$INST_PrologP" = "y"  ]; then
	echo "Creating script: '$SCRIPT_DIR/prologp'"
	if [ ! -d $SCRIPT_DIR ]; then mkdir -p $SCRIPT_DIR; fi
	cat << END >$SCRIPT_DIR/tmp$$
#!/bin/sh
CDRPROGS_DIR=$DEST/$CDRDIR
export CDRPROGS_DIR
if [ "\$BABEL_DIR" = "" ]; then
	BABEL_DIR=$DEST/$BABEL_DIR
	export BABEL_DIR
fi
\$CDRPROGS_DIR/$BINDIR/prologp \$*
exit \$?
END
	instexe $SCRIPT_DIR/tmp$$ $SCRIPT_DIR/prologp
	rm -f $SCRIPT_DIR/tmp$$
fi
########################## Creating script: babel

if [ "$INST_Babel" = "y"  ]; then
	echo "Creating script: '$SCRIPT_DIR/babel'"
	if [ ! -d $SCRIPT_DIR ]; then mkdir -p $SCRIPT_DIR; fi
	cat << END >$SCRIPT_DIR/tmp$$
#!/bin/sh
if [ "\$BABEL_DIR" = "" ]; then
	BABEL_DIR=$DEST/$BABEL_DIR
	export BABEL_DIR
fi

\$BABEL_DIR/babel \$*
exit \$?
END
	instexe $SCRIPT_DIR/tmp$$ $SCRIPT_DIR/babel
	rm -f $SCRIPT_DIR/tmp$$
fi

################################# Where to place man pages?

if [ "$INST_pKalc" = "y" -o "$INST_PrologP" = "y" -o "$INST_PrologD" = "y" -o "$INST_XPred" = "y" ]; then
	cat << END

Now, specify a directory where the programs' man pages will be
written. Just pressing enter will use the default,
'$MAN_DIR'
END
	OLDMAN_DIR=$MAN_DIR
	while [ 0 ]; do
		echo -n "Directory for man pages? "
		read ASK
		if [ "$ASK" != "" ]; then
 			MAN_DIR=$ASK
		fi
		if [ -d "$MAN_DIR" ]; then
			break
		else
			echo "Directory $MAN_DIR doesn't exist!"
			MAN_DIR=$OLDMAN_DIR
		fi
	done

	echo
		
fi

#################################### Installing man pages

if [ "$INST_XPred" = "y" ]; then
	echo "Installing xpred manual to '$MAN_DIR/cat1' ...."
	if [ ! -d $MAN_DIR/cat1 ]; then mkdir -p $MAN_DIR/cat1; fi
	instcat cat1/xpred.1x $MAN_DIR/cat1/xpred.1x
fi

if [ "$INST_PrologD" = "y" ]; then
	echo "Installing prologd manual to '$MAN_DIR/cat1' ...."
	if [ ! -d $MAN_DIR/cat1 ]; then mkdir -p $MAN_DIR/cat1; fi
	instcat cat1/prologd.1 $MAN_DIR/cat1/prologd.1
fi

if [ "$INST_pKalc" = "y" ]; then
	echo "Installing pkalc manual to '$MAN_DIR/cat1' ...."
	if [ ! -d $MAN_DIR/cat1 ]; then mkdir -p $MAN_DIR/cat1; fi
	instcat cat1/pkalc.1 $MAN_DIR/cat1/pkalc.1
fi

if [ "$INST_PrologP" = "y" ]; then
	echo "Installing prologp manual to '$MAN_DIR/cat1' ...."
	if [ ! -d $MAN_DIR/cat1 ]; then mkdir -p $MAN_DIR/cat1; fi
	instcat cat1/prologp.1 $MAN_DIR/cat1/prologp.1
fi


########################### Info on licensing

echo
echo Copying license file to $DEST/license
instlicense $SRC/license $DEST/$CDRDIR/license

echo $DEST/$CDRDIR/$LOGFILE >> $LOGFILE
rm -f $DEST/$CDRDIR/$LOGFILE
cp $LOGFILE $DEST/$CDRDIR/$LOGFILE

cat << END

To reinstall the prediction programs with other parameters, first
you may uninstall everything by typing rm -f \`cat $LOGFILE\`.

To run the prediction program(s) you need to fill in the license
file and send it to the address given in the license file. Then 
you will get a license key. Please read the license file:
	'$DEST/$CDRDIR/license'
for more information. 
END

########################### Finishing the job
cd $OLDDIR
