#!/bin/sh

#check if intstall directory provided
if [ $# -eq 1 ]
then
	BASE_DIR=$1
	INSTALL_DIR=$1
	DOCS_DIR=$INSTALL_DIR
		
elif [ $# -eq 0 ]	
then
	INSTALL_DIR="/usr/local"
	DOCS_DIR="/usr/local/share/doc/lip.2.0"
	
else
	echo "..."
fi

DEFAULT_DIR="/usr/local"

#run configure script is it exist with install directory as parameter
if [ -f configure ]
then 
	./configure --prefix=$INSTALL_DIR
#	echo "configure goes here"
else 
	echo "Configure script missing!"

	if [ -d $INSTALL_DIR ]
	then
		echo "directory already exists!"
	else
		mkdir-p $INSTALL_DIR
	fi
fi

#copydocumentation into the directory docs directory
if [ -d ./examples -a -d ./docs ]
then
	echo "Installing documentation ..."
	
	# Check to see if apropriate directories exist if not
	# create them and copy docs to apropriate dirs.
	if  [ ! -d $INSTALL_DIR ]
	then
		mkdir -p $INSTALL_DIR
	fi

	if [ "$DEFAULT_DIR" != "$INSTALL_DIR" ]
	then
		if [ -d $INSTALL_DIR/examples -a -d $INSTALL_DIR/DOCS ]
		then
			echo "..."
		else
			mkdir -p $INSTALL_DIR/examples
			mkdir -p $INSTALL_DIR/docs
		fi
		
		#copy docuemnts in to appropriate directories
		echo " cp -r ./EXAMPLES $INSTALL_DIR/EXAMPLES "
		cp -r ./examples/* $INSTALL_DIR/examples/

		echo " cp -r ./DOCS $INSTALL_DIR/DOCS "
		cp -r ./docs/* $INSTALL_DIR/docs/

		#save documents directory path for later unistall		
		echo $INSTALL_DIR > docs_dir
		
	else
		if [ ! -d $INSTALL_DIR/share ]
		then
			mkdir -p $INSTALL_DIR/share/
		fi
		
		if [ ! -d $INSTALL_DIR/share/doc ]
		then
			mkdir -p $INSTALL_DIR/share/doc
		fi
		
		if [ ! -d $DOCS_DIR ]
		then
			mkdir -p $DOCS_DIR
			mkdir -p $DOCS_DIR/examples
			mkdir -p $DOCS_DIR/docs
		fi

		#copy docuemnts in to appropriate directories
		echo " cp -r ./EXAMPLES $DOCS_DIR/EXAMPLES "
		cp -r ./examples/* $DOCS_DIR/examples/

		echo " cp -r ./DOCS $DOCS_DIR/DOCS "
		cp -r ./docs/* $DOCS_DIR/docs
		
		#save documents directory path for later unistall
		echo $DOCS_DIR/ > docs_dir
	fi

else
	echo "documentation not found!"	

fi

#run make file target make isntall to compile and install the library
if [ -f Makefile ]
then
	make install
#	echo "make install goes here"
fi

#Create the make file for the examples


echo '#############################################################################' 	> $DOCS_DIR/examples/Makefile
echo '#									    #' 		>> $DOCS_DIR/examples/Makefile
echo '#	CLASS LIBRARY LIP FOR MULTIVARIATE SCATTERED DATA INTERPOLATION     #' 		>> $DOCS_DIR/examples/Makefile
echo '#									    #' 		>> $DOCS_DIR/examples/Makefile
echo '#	This makefile gives targets that show how to compile and link       #' 		>> $DOCS_DIR/examples/Makefile
echo '#	user code to the Lip shared library and statatic library.           #' 		>> $DOCS_DIR/examples/Makefile
echo '#									    #' 		>> $DOCS_DIR/examples/Makefile
echo '#############################################################################' 	>> $DOCS_DIR/examples/Makefile
echo '#' 										>> $DOCS_DIR/examples/Makefile
echo '# This make file show how to compile and link examples included with this ' 	>> $DOCS_DIR/examples/Makefile
echo '# distribution of LIP assuming different installations of the library. this' 	>> $DOCS_DIR/examples/Makefile
echo '# include the following examples for both static and shared linking.' 		>> $DOCS_DIR/examples/Makefile
echo '#' 										>> $DOCS_DIR/examples/Makefile
echo '# liblipex:		shows how to compile and link library when install' 	>> $DOCS_DIR/examples/Makefile
echo '#			in the library search path used to load libraries' 		>> $DOCS_DIR/examples/Makefile
echo '#' 										>> $DOCS_DIR/examples/Makefile
echo '# exampleprocedural:		shows how to compile and link by implicitly telling' >> $DOCS_DIR/examples/Makefile
echo '#			 the linker where to look for the library' 			>> $DOCS_DIR/examples/Makefile
echo '#			 shows how to compile and link procedural C conde.' 		>> $DOCS_DIR/examples/Makefile
echo '#' 										>> $DOCS_DIR/examples/Makefile
echo '############################################################################' 	>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# location where the library is installed' 					>> $DOCS_DIR/examples/Makefile
echo MYPATH= $BASE_DIR	 								>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# compiler' 									>> $DOCS_DIR/examples/Makefile
echo 'CC = g++' 									>> $DOCS_DIR/examples/Makefile
echo 'GCC = gcc' 									>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# Some options probably not needed: -g (which enables the debugger options).' 	>> $DOCS_DIR/examples/Makefile
echo 'FLAGS = -g -O -Wno-deprecated' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# Object file fo the example' 							>> $DOCS_DIR/examples/Makefile
echo 'OBJ1 = liblipex.o' 								>> $DOCS_DIR/examples/Makefile
echo 'OBJ2 = exampleprocedural.o' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# LIB_PATH used to store the path in which the library files were installed.' 	>> $DOCS_DIR/examples/Makefile
echo '# The commented out assignment is for when the library is installed into the' 	>> $DOCS_DIR/examples/Makefile
echo '# users home directory. NOTE: $(HOME) referes to env varialble HOME.' 		>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#directory where the liblip.a is installed' 					>> $DOCS_DIR/examples/Makefile
echo 'LIB_PATH = $(MYPATH)/lib/' 						>> $DOCS_DIR/examples/Makefile
echo '#LIB_PATH = /usr/local/lib/' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# INCLUDE_PATH used to store the path in which the *.h files have been' 		>> $DOCS_DIR/examples/Makefile
echo '# placed. The commented out assignment is for when the *.h files are placed' 	>> $DOCS_DIR/examples/Makefile
echo '# in the users home directory.' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#directory' 									>> $DOCS_DIR/examples/Makefile
echo 'INCLUDE_PATH = $(MYPATH)/include' 					>> $DOCS_DIR/examples/Makefile
echo '#INCLUDE_PATH = /usr/local/include/' 						>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#include directories holdign the header files needed for liblip' 			>> $DOCS_DIR/examples/Makefile
echo '#INCLUDE= -I$(INCLUDE_PATH)/tnt  -I$(INCLUDE_PATH)/glpk -I$(INCLUDE_PATH)' 	>> $DOCS_DIR/examples/Makefile
echo 'INCLUDE= -I$(INCLUDE_PATH)/tnt  -I$(INCLUDE_PATH)' 				>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#location of glpklib.a this is a static library and should be compiled from source.' >> $DOCS_DIR/examples/Makefile
echo 'GLPK_STATIC_PATH=$(HOME)/glpklib/lib/' 						>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'all:	static_example2  static_example shared_example ' 			>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#################################################################################' >> $DOCS_DIR/examples/Makefile
echo '# linking examplelint. If you have succesfully installed lip library and have' 	>> $DOCS_DIR/examples/Makefile
echo '# LIB_PATH to /etc/ld.so.conf Or you have added LIB_PATH TO LD_LIBRARY_PATH' 	>> $DOCS_DIR/examples/Makefile
echo '# then compiling is as eassy as this.  ' 						>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# shared_example target links liblipex.o to the liblip shared library. To make' 	>> $DOCS_DIR/examples/Makefile
echo '# up shared_example executable.' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'shared_example:	$(OBJ1)' 							>> $DOCS_DIR/examples/Makefile
echo '		$(CC) -o shared_example $(OBJ1) $(FLAGS) -L$(LIB_PATH) -llip -L$(GLPK_STATIC_PATH) -lglpk -lm' >> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# static_example target links liblipex.o to the liblip static library. To make' 	>> $DOCS_DIR/examples/Makefile
echo '# up static_example executable.' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'static_example: $(OBJ1)' 								>> $DOCS_DIR/examples/Makefile
echo '		$(CC) -o static_example -non_shared  $(OBJ1) $(FLAGS) -L$(LIB_PATH) -llip -L$(GLPK_STATIC_PATH) -lglpk -lm' >> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#################################################################################' >> $DOCS_DIR/examples/Makefile
echo '# linking examplelintprocedural' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# static_example target links exampleprocedural.o to the lip static library. To make' >> $DOCS_DIR/examples/Makefile
echo '# up static_example executable.' 							>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'static_example2:$(OBJ2)' 								>> $DOCS_DIR/examples/Makefile
echo '		$(CC) -o static_example2 -static $(OBJ2) $(FLAGS) $(LIB_PATH)liblip.a  $(GLPK_STATIC_PATH)libglpk.a -lm' >> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '#################################################################################' >> $DOCS_DIR/examples/Makefile
echo '# compiling examples to objectfiles.' 						>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'liblipex.o:			liblipex.cpp' 					>> $DOCS_DIR/examples/Makefile
echo '				$(CC) -c liblipex.cpp $(FLAGS) $(INCLUDE)' 		>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '# compiling proccedual example using C compiler' 					>> $DOCS_DIR/examples/Makefile
echo '#' 										>> $DOCS_DIR/examples/Makefile
echo 'exampleprocedural.o:	exampleprocedural.c' 					>> $DOCS_DIR/examples/Makefile
echo '				$(GCC) -c exampleprocedural.c $(FLAGS) $(INCLUDE)' 	>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo '.PHONY: clean all' 								>> $DOCS_DIR/examples/Makefile
echo '' 										>> $DOCS_DIR/examples/Makefile
echo 'clean:' 										>> $DOCS_DIR/examples/Makefile
echo '		rm -f $(OBJ1) shared_example static_example' 				>> $DOCS_DIR/examples/Makefile
echo '		rm -f $(OBJ2) static_example2' 						>> $DOCS_DIR/examples/Makefile


