#!/bin/sh
# Shell script for GPS => Makefile Generation and Librairies checking
# Created by ----------, modified by whitehat

echo -e "\nGPS version 0.6.1 Building and compiling process\n"

LIST='ctype.h stdio.h string.h stdlib.h unistd.h signal.h sys/types.h sys/socket.h libnet.h pcap.h net/if_arp.h netinet/ether.h netinet/ip.h netinet/tcp.h'
INCLUDE=/usr/include

echo -n "Looking for the GNU C Compiler (gcc): " 
if [ -f `which gcc` ]; 
then echo "gcc version `gcc -dumpversion` found"
else echo "gcc not found; aborting.." 
     exit -1
fi

echo -e "\nChecking librairies....." 
for lib in $LIST
do
	echo -n "Checking $lib"
	FILE="$INCLUDE/$lib"
	if [ -f $FILE ];
	then  echo -e "\t.. [ ok ]"
	else echo -e "\n\nMissing file ! \n$lib is needed by GPS\nProcess aborted ! \n"
	     exit -2
	fi
done

echo -en "\nGenerating Makefile .. "

CC='`which gcc`'
CFALGS='-Wall `libnet-config --defines`'
OPT='`libnet-config --libs` -lpcap'
OBJS='gps.c services.c'
TARGET='gps'

cat >Makefile<<EOF
#Generated Makefile for GPS#

all :
	@echo -n "Building process started .. "
EOF
echo -e "\t@$CC $CFALGS $OBJS $OPT -o $TARGET" >> Makefile

cat >>Makefile<<EOF
	@echo -e "done\nCompilation and linking completed => run <<make install && make clean>> to complete installation"

install :
	@cp -i gps /usr/bin

clean :
	@echo -n "Removing building files for GPS .. "
	@rm -f *.c *.h configure Makefile gps README
	@echo "done"

#End of Makefile#

EOF

echo "done"
echo -e "\nRun <<make all>> for compilation and linking process\n"
exit 0
