#! /bin/sh

echo GenEthic1.1 configuration process...

#for NEEDED in perl 
#do
#	if [ -x `which $NEEDED` ]; then echo "OK Found $NEEDED"; else echo "Error, could not find $NEEDED"; exit 1; fi
#done


if [ "$1" = "" ]
then
	echo 'Syntax error'
	echo "$0 <path_to_install>"
	exit 1
else
	TEST=`echo $1 | sed "s/^\/.*/X/"`
	if [ "$TEST" != "X" ]
	then
		echo Error, $1 is not an absolute path
		exit 1
	fi
fi

if [ -d "$1" ]
then
	echo Looks good, using $1 as installation directory
else
	echo  Creating $1 directory
	mkdir $1
fi

echo -n 'Trying to find out where you are hiding your perl... '

PERL=`which perl`;

if [ -x "$PERL" ]
then
	PVER=`$PERL -v | grep version | sed "s/.*version //"`
	echo $PERL $PVER
else
	echo 
	echo Sorry, cant find it
	exit 1
fi

echo Looking if manadatory perl modules are installed
for module in DBI IO::Socket
do

MODTEST=`$PERL -M$module -e 'print "ok";'`

if [ -n "$MODTEST" ]
then
	echo PERL Module $module found...
else
	echo PERL Module $module not found... aborting...
	exit 1
fi

done

echo Creating needed directories...
mkdir $1/etc
mkdir $1/bin
mkdir $1/var
mkdir $1/log
echo Copying files...
cd src
echo "#!$PERL"  >  $1/bin/genethic.pl
cat genethic.pl >> $1/bin/genethic.pl
chmod 755          $1/bin/genethic.pl
cp sample.conf     $1/etc
cp ../README       $1/
echo done
echo Now do cd $1
echo and read the  README
