#!/bin/bash
export CUR_DIR=`pwd`

if ! [ -d "$CUR_DIR/src" ]
then
	echo "Run this script from the main directory and not from any subdirectory"
	exit
fi

#set the directory where the sources are present
export YPSRCDIR=$CUR_DIR/src

echo "This script will set the necessary parameters for compilation"
echo "Use this script for compilation purposes"

export BIN_LOC=/usr/local/bin
echo "Default location for binary files is $BIN_LOC"
echo "Do you want to use the default location ?(Y/N)"
read binloc_choice
if [ $binloc_choice = "N" -o $binloc_choice = "n" ]
then
	echo "Enter the location where you want to install the files"
	read binloc_location
	export BIN_LOC=$binloc_location
fi	

#library file location
export LIB_LOC=/usr/local/lib
echo "Default location for library files is $LIB_LOC"
echo "Do you want to use the default location ?(Y/N)"
read libloc_choice
if [ $libloc_choice = "N" -o $libloc_choice = "n" ]
then
	echo "Enter the location where you want to install the files"
	read libloc_location
	export LIB_LOC=$libloc_location
fi	

#config file location
export CONFIG_LOC=/usr/local/share
echo "Default location for config files(ypopsrc) is $CONFIG_LOC"
echo "Do you want to use the default location ?(Y/N)"
read config_choice
if [ $config_choice = "N" -o $config_choice = "n" ]
then
	echo "Enter the location where you want to install the files"
	read config_location
	export CONFIG_LOC=$config_location
fi	

#libxml
#echo "Checking for libxml"
#XML_CONFIG=`which xml-config`
#if [ -z $XML_CONFIG ]
#then
#	echo "The libxml-devel package is not installed for your system."
#	echo "Please download and install libxml from www.xmlsoft.org"
#	echo "Do you have libxml installed in any alternative location? (Y/N)"
#	if [ $config_choice = "N" -o $config_choice = "n" ]
#	then
#		exit
#	fi
#	echo "Enter the alternative xml location"
#	read xml_choice
#	if ! [ $xml_choice/bin/xml-config ]
#	then
#		echo "Incorrect xml location specified.There is no file xml-config present in $xml_choice/bin"
#		exit
#	fi
#	export XML_LOC=$xml_choice
#	export XML_INC=`$xml_choice/bin/xml-config --cflags`
#	export XML_LIB=`$xml_choice/bin/xml-config --libs`
#else
#export XML_INC=`$XML_CONFIG --cflags`
#export XML_LIB=`$XML_CONFIG --libs`
#fi
#echo "Using xml installed in $XML_INC"

#openssl libraries
echo "Ypops! uses openssl libraries from http://www.openssl.org"
echo "Please download and compile any version greater than or equal to 0.9.7a"
echo "Enter the root directory of your openssl installation"
read openssl_choice
if ! [ -d $openssl_choice/include ]
then
	echo "Incorrect curl location specified"
exit
fi
export SSL_INC=$openssl_choice/include
export SSL_LIB=$openssl_choice/lib

#curl libraries
echo "Ypops! uses Curl libraries from http://curl.haxx.se/"
echo "Please download and compile any version greater than or equal to 7.10.5"
echo "Enter the root directory of your curl installation"
read curl_choice
if ! [ -d $curl_choice/include ]
then
	echo "Incorrect curl location specified"
exit
fi
export CURL_INC=$curl_choice/include
export CURL_LIB=$curl_choice/lib

#mimepp sources
echo "Yahoopops! uses mimepp sources from http://yahoopops.sourceforge.net/downloads"
echo "Please download it and extract to any directory.DO NOT COMPILE IT."
echo "Enter the root directory of your mimepp sources"
read mimepp_choice
if ! [ -d $mimepp_choice/examples/email ]
then
	echo "Incorrect mimepp location specified"
exit
fi
export MIMEPP=$mimepp_choice

echo "Starting compilation process"
cd $YPSRCDIR;make

#the installation process
if ! [ -x "$CUR_DIR/src/ypops" ]
then
	echo "Yahoopops has not yet been built.Please build it"
	exit
else
	echo "Starting installation of yahoopops"
	if ! `cp $CUR_DIR/src/ypops $BIN_LOC/ypops.bin`
	then
		echo "Installation failed.Please create the directory $BIN_LOC"
		echo "If directory is already present please check permissions"
		exit
	fi
	if ! `cp $CUR_DIR/src/ypops_samplerc $CONFIG_LOC/ypopsrc`
	then
		echo "Unable to copy the config files.Please create the directory $CONFIG_LOC"
		echo "If directory is already present please check permissions"
		exit
	else
		`cp $CUR_DIR/src/localization* $CONFIG_LOC`
	fi
	echo "#!/bin/bash" > $BIN_LOC/ypops
	if ! [ -z $XML_LOC/lib ]
	then
		echo "export LD_LIBRARY_PATH=$LIB_LOC:$XML_LOC/lib:$LD_LIBRARY_PATH" >> $BIN_LOC/ypops
	else
		echo "export LD_LIBRARY_PATH=$LIB_LOC:$LD_LIBRARY_PATH" >> $BIN_LOC/ypops
	fi	
	echo "$BIN_LOC/ypops.bin" >> $BIN_LOC/ypops
	`chmod 755 $BIN_LOC/ypops`
	echo "*************************************************************************************"
	echo "Ypops is installed in $BIN_LOC as $BIN_LOC/ypops.You can start it by typing"
	echo "$BIN_LOC/ypops on the command line.Have fun using it and report bugs/comments/suggestions to
	osdndevel@users.sourceforge.net/anujseth@users.sourceforge.net"
	echo "*************************************************************************************"
fi


