binary=`which bash`
binperl=`which perl`


#! $binary


instfile=`echo $0 | sed 's/\.\///'`

function getamigenepath {
		echo "Enter the new PATH : "
                read AMIGENE
                echo "Your choice : " $AMIGENE
} 

function changeperlpath {

	for i in *.pl 
	do cp $i $i.tmp 
	done 
	
	for i in *.pl.tmp
	do j=`echo $i | sed 's/\.tmp$//'`
		cat "$i" | gawk -v perlpath=$binperl '{if ( $0 ~ /\#\! / ) {print "#! "perlpath ; } else { print $0;} }' > "$j" 
	done 
 
	rm -f *.tmp
}

function changebashpath {

 
	ls *.sh | grep -v "$instfile" | while read b ; do  cp "$b" "$b.tmp" ; done 

	for i in *.sh.tmp
	do j=`echo $i | sed 's/\.tmp$//'`
		cat "$i" | gawk -v bashpath=$binary '{if ( $0 ~ /\#\! / ) {print "#! "bashpath ; } else { print $0;} }' > "$j"
	done 
	
	rm -f *.tmp
		
}

AMIGENE="/usr/local/share/micheck/"

echo "|------------------------------------------------------|"
echo "|                                                      |" 
echo "|             Amigene Package Installation             |"
echo "|                                                      |" 
echo "|------------------------------------------------------|"
echo
echo
echo "Current Amigene directory : " $AMIGENE
echo "Do you want to install Micheck package in this directory ? (yes|no) "
read answer
while [ $answer != "yes" -a $answer != "no" ]   
do
	echo "answer by yes or no" ; read answer
done

case $answer in
	yes) echo "Proceeding ...."
		changeperlpath
		changebashpath
	;;
	no)	getamigenepath 
		echo "Are you sure ? (yes|no)"
		read answer2
		while [ $answer2 != "yes" -a $answer2 != "no" ]
		do
			echo "answer by yes or no" ; read answer2
		done
		if [ $answer2 = "no" ]
			then getamigenepath
		fi
		changeperlpath
		changebashpath
		cp runMICHECK.sh runMICHECK.sh.tmp
		cat runMICHECK.sh.tmp | gawk -v amigenepath=$AMIGENE -v bashpath=$binary '{if ($0 ~ /AMIGAHOME="/) { print "AMIGAHOME=\""amigenepath"\""; } else { if ( $0 ~ /\#\! / ) {print "#! "bashpath ; } else { print $0;} } }' > runMICHECK.sh
	;;
esac

echo "Creating Amigene file : "$AMIGENE

mkdir $AMIGENE 2> /dev/null

echo "Copying Parameters files" 

cp *.seuils $AMIGENE

echo "Copying Micheck binairies"

ls * | grep -v "$instfile" | while read file ; do cp "$file" "$AMIGENE/$file" ; done

ln -s $AMIGENE/runMICHECK.sh /usr/local/bin/ 2> /dev/null

if [ $? != "0" ] 
	then echo "Ask to root to put $AMIGENE/runAMIGA.sh in the PATH"
		echo "but you can work with  $AMIGENE/runAMIGA.sh" 
fi

echo "Change the rights"

chmod -R 777 $AMIGENE/*
