#! /bin/sh -e
###############################################################################
#
# Install query system in the correct place.
#
###############################################################################


config=query.conf


usage()
{
	echo ""
	echo "Use: InstallQuery install|symlink|copyto <parameters>"
	echo ""
	echo "     InstallQuery install <cgi-path> <data-path> <images-path> <images-dir>"
	echo "     InstallQuery symlink <cgi-path> <data-path> <images-path> <images-dir>"
	echo "     InstallQuery copyto  <new-path>"
	echo ""
	echo "Actions:"
	echo "    install       install query system by copying all files"
	echo "    symlink       install query system by sym-linking most files to this dir"
	echo "    copyto        copy query system to permanent location for future installs"
	echo ""
	echo "Parameters:"
	echo "    cgi-path      full path of where to install cgi-bin files"
	echo "    data-path     full path of where to install data files"
	echo "    images-path   full path of where to install images"
	echo "    images-dir    path to the images directory as seen in a URL"
	echo "    new-path      full path where to put installable system"
	echo ""
	echo "Eg: InstallQuery install /var/web/cgi-bin/ferret /var/web/data/ferret \\"
	echo "                         /var/web/images/ferret  /images/ferret"
	echo ""
	echo "If you wish to provide different index searches on your webpages, one way is"
	echo "to 'copyto' the query system to a permanent place (eg. \"/usr/local/lib/ferret\")"
	echo "and then install from there multiple times using 'symlink' to directories such"
	echo "as \"/.../cgi-bin/ferret/hiscoolstuff\" and \"/.../cgi-bin/ferret/hercoolstuff\"."
	echo "Each system will have a unique 'query.conf' file that can be customized for"
	echo "that directory."
	echo ""
	exit 1
}


if   [ $# = 0 ]; then usage; fi

action=$1
if [ $action = "install" ]
then
	if [ $# != 5 ]; then usage; fi;
	cgidir=$2
	datdir=$3
	imgdir=$4
	imgurl=$5
elif [ $action = "symlink" ]
then
	if [ $# != 5 ]; then usage; fi;
	cgidir=$2
	datdir=$3
	imgdir=$4
	imgurl=$5
elif [ $action = "copyto" ]
then
	if [ $# != 2 ]; then usage; fi;
	insdir=$2
	cgidir=$2/cgi-bin
	datdir=$2/data
	imgdir=$2/images
else
	usage
fi


perlexe=`type -path perl`;
if [ $action != "copyto" -a -z $perlexe ]
then
	echo "Perl-5 executable not found!"
	exit 1
fi

echo "Installing 'cgi' files in: $cgidir"
echo "Installing data  files in: $datdir"
echo "Installing image files in: $imgdir"
echo "Images as seen by URLs is: $imgurl"
echo "Perl-5 executable located: $perlexe"


if [ ! -d cgi-bin -o ! -d data -o ! -d images -o ! -f InstallQuery ]
then
	echo ""
	echo "Could not find query system!  (please run from .../ferret/query/)"
	exit 1
fi


umask 022
mkdir -p $cgidir $datdir $imgdir


for i in cgi-bin/*
do
	f=`basename $i`

	if [ -f $i ]
	then
		rm -f $cgidir/$f
		if [ $action != "symlink" ]
		then
			(echo "#! $perlexe"; sed -e '1d') <$i >$cgidir/$f
			if [ -x $i ]
			then
				chmod 755 $cgidir/$f
			else
				chmod 644 $cgidir/$f
			fi
		else
			(mypwd=$PWD; cd $cgidir; ln -s $mypwd/cgi-bin/$f $f)
		fi
	fi
done

for i in data/*
do
	f=`basename $i`

	if [ -f $i ]
	then
		rm -f $datdir/$f
		if [ $action != "symlink" ]
		then
			cp $i $datdir/$f
			chmod 644 $datdir/$f
		else
			(mypwd=$PWD; cd $datdir; ln -s $mypwd/data/$f $f)
		fi
	fi
done

for i in images/*
do
	f=`basename $i`

	if [ -f $i ]
	then
		rm -f $imgdir/$f
		if [ $action != "symlink" ]
		then
			cp $i $imgdir/$f
			chmod 644 $imgdir/$f
		else
			(mypwd=$PWD; cd $imgdir; ln -s $mypwd/images/$f $f)
		fi
	fi
done


if [ $action = "copyto" ]
then
	rm -f $insdir/InstallQuery
	cp InstallQuery $insdir/InstallQuery
	chmod 755 $insdir/InstallQuery
	cp $config $insdir/$config
	chmod 644 $insdir/$config
	exit 0
fi


if [ -f $cgidir/$config ]
then
	echo ""
	echo "Config file '$cgidir/$config' exists -- not replaced"
	echo ""
	echo "If you wish to replace it, please remove the existing file and"
	echo "re-run this program."
	echo ""
else
	sed 	-e "s|===CGI-DIR===|$cgidir|"	\
		-e "s|===DAT-DIR===|$datdir|"	\
		-e "s|===IMG-URL===|$imgurl|"	\
	<$config >$cgidir/$config
	chmod 644 $cgidir/$config

	echo ""
	echo "Please edit '$cgidir/$config' to reflect your system."
	echo ""
fi
