#!/bin/sh
# Written by Morgoth DBMA, morgothdbma@o2.pl
# This is part of PgXexplorer software, Open Source
# on BSD licence, Libraries(interaces) used:
# GNU GCC, AS (all stuff needed to compile C source into executable binary)
# LibPQ-FE from PostgreSQL, GTK (GIMP Toolkit)
# written in VIM editor, ctags used, CVS used
# Currently only one author: MOrgoth DBMA
# FILE: configure
echo "PgXexplorer configure, by MorgothDBMA >> morgothdbma@o2.pl"
echo 'Checking for existing installation...'
if [ -f "./Makefile" ]
then
	make clean
	rm -f ./Makefile
fi
echo -n "Getting OS name... "
OSNAME=$(uname)
echo "$OSNAME"
echo -n "Checking for GCC... "
if [ -f "/usr/bin/gcc" ] || [ -f "/usr/local/bin/gcc" ] || [ -f "/bin/gcc" ]
then
	echo "gcc"
	GCC="gcc"
else
	echo "no - configure failed."
	exit 1
fi
if [ "$1" = "--help" ] || [ "$1" = "-help" ]
then
	echo "Configure for pgxexplorer, no options are available"
	exit 0
fi
echo 'Now directory where postges: bin/, lib/ are:'
echo -n 'PostgreSQL directory [/usr/local/pgsql]: '
read PGDIR
if [ "$PGDIR" = "" ]
then
	PGDIR="/usr/local/pgsql"
fi
echo -n "Checking for PostgreSQL in $PGDIR/bin..."
if [ -f "$PGDIR/bin/postmaster" ] && [ -f "$PGDIR/lib/libpq.so" ]
then
	echo " ok"
	PGLIBS="$PGDIR/lib"
	PGINCL="$PGDIR/include"
	echo "Postges libraries in $PGLIBS"
	echo "Postgres includes in $PGINCL"
	if [ "$PGDATA" = "" ]
	then
		echo "--- warning: no PGDATA is set, but this will not make things fail"
		echo "--- you shold set your PGDATA variable to $PGDIR/data or somewhere else"
	else
		echo "Postgres data in $PGDATA"
	fi
	echo -n "Checking for GTK..."
	GTKLIBS=$(gtk-config --libs)
	GTKINCL=$(gtk-config --cflags)
	if [ "$GTKLIBS" = "" ]
	then
		echo 'GtkConfig not found...'
		exit 1
	fi
	echo "gtk-config"
	echo "GTK LINK flags: $GTKLIBS"
	echo "GTK CFLAGS: $GTKINCL"
	echo "Requirements met, generating GNU Makefile"
	rm -f ./Makefile
	echo -n "Do You want debug (press d) or release (press r): "
	read RELTYPE
	echo '# Written by Morgoth DBMA, morgothdbma@o2.pl' > Makefile
	echo '# This is part of PgXexplorer software, Open Source' >> Makefile
	echo '# on BSD licence, Libraries(interaces) used:' >> Makefile
	echo '# GNU GCC, AS (all stuff needed to compile C source into executable binary)' >> Makefile
	echo '# LibPQ-FE from PostgreSQL, GTK (GIMP Toolkit)' >> Makefile
	echo '# written in VIM editor, ctags used, CVS used' >> Makefile
	echo '# Currently only one author: MOrgoth DBMA' >> Makefile
	echo '# FILE: Makefile (automatically generated)' >> Makefile
	echo "all: pgxtest pgxexplorer" >>  Makefile
if [ "$RELTYPE" = "d" ]
then
	echo "CFLAGS = -Wall -ansi -pedantic -g3 $GTKINCL -I$PGINCL" >> Makefile 
	echo "CFLESS = -Wall -ansi -pedantic -g3" >> Makefile 
	echo "CLINK  = $GTKLIBS -L$PGLIBS -lpq -g3" >> Makefile
elif [ "$RELTYPE" = "r" ]
then
	echo "CFLAGS = -Wall -ansi -pedantic -O3 $GTKINCL -I$PGINCL" >> Makefile 
	echo "CFLESS = -Wall -ansi -pedantic -O3" >> Makefile 
	echo "CLINK  = -O3 $GTKLIBS -L$PGLIBS -lpq" >> Makefile
else
	echo "Error, press d or r here."
	exit 1
fi
	echo "GCC = gcc" >> Makefile
	
	echo "main.o: main.c main.h gtkincl.h consoleout.h gtkdialogs.h pgxexplorer.h signals.h" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c main.c -o main.o" >> Makefile
	
	echo "pgxexplorer: main.o consoleout.o gtkdialogs.o query.o signals.o toolbar.o" >> Makefile
	echo "	\$(GCC) main.o consoleout.o gtkdialogs.o query.o signals.o toolbar.o -o pgxexplorer \$(CLINK)" >> Makefile
	
	echo "pgxtest: pgxtest.o consoleout.o query.o gtktest.o signals.o gtkdialogs.o" >> Makefile
	echo "	\$(GCC) -o pgxtest pgxtest.o consoleout.o query.o gtktest.o signals.o gtkdialogs.o \$(CLINK)" >> Makefile
	
	echo "pgxtest.o: pgxtest.c consoleout.h query.h" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c pgxtest.c" >> Makefile
	
	echo "consoleout.o: consoleout.c consoleout.h" >> Makefile
	echo "	\$(GCC) \$(CFLESS) -c consoleout.c" >> Makefile
	
	echo "query.o: query.c consoleout.h query.h signals.h signals.c" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c query.c" >> Makefile
	
	echo "toolbar.o: toolbar.h toolbar.c main.c main.h" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c toolbar.c" >> Makefile
	
	echo "signals.o: signals.h signals.c gtkdialogs.h gtkdialogs.c" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c signals.c" >> Makefile
	
	echo "gtktest.o: gtktest.c query.h gtktest.h gtkincl.h" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c gtktest.c" >> Makefile
	
	echo "gtkdialogs.o: gtkdialogs.c gtkdialogs.h gtkincl.h query.h" >> Makefile
	echo "	\$(GCC) \$(CFLAGS) -c gtkdialogs.c" >> Makefile
	
	echo "clean:" >> Makefile
	echo "	-rm -f *.o pgxexplorer pgxtest" >> Makefile
	echo "install: pgxexplorer" >> Makefile
	echo "	cp ./pgxexplorer $PGDIR/bin/" >> Makefile
	echo "package:" >> Makefile
	echo "	mkdir PgXexplorer-0.14-alpha" >> Makefile
	echo "	cp *.c *.h Makefile tags configure INSTALL README PgXexplorer-0.14-alpha/" >> Makefile
	echo "	tar jcvf PgXexplorer-0.14-alpha.tar.bz2 PgXexplorer-0.14-alpha/" >> Makefile
	echo "	rm -rf PgXexplorer-0.14-alpha/" >> Makefile
	echo "Done."
	echo "Make targets are: (none), all, pgxexplorer, pgxtest, *.o, clean, package, install"
	echo "Now run GNU make to compile pgxexplorer."
else
	echo ".no - configure failed."
	exit 1
fi

