#!/bin/sh
#
# PostgreSQL Build Script
#
BUILDRUN=true

if echo '\c' | grep -s c >/dev/null 2>&1
then
        ECHO_N="echo -n"
        ECHO_C=""
else
        ECHO_N="echo"
        ECHO_C='\c'
fi

ls template
TEMPLATE=generic
#
#     Use the file template/.similar to find an appropriate file
#     We get the architecture from the config.guess script.
#
CONFIG=`./config.guess`
GUESS=`grep $CONFIG template/.similar 2>/dev/null`
if [ $GUESS ]; then
      TEMPLATE=`echo $GUESS | sed 's/.*=//'`
fi

$ECHO_N "Appropriate template file [$TEMPLATE]: $ECHO_C"
read a
if [ "$a." = " ." ]
then
	echo "You must choose an appropriate template file"
	exit
else
	if [ "$a." = "." ]; then 
		TEMPLATE=template/$TEMPLATE
	else
		TEMPLATE=template/$a
	fi
	AROPT=`grep AROPT $TEMPLATE | awk -F: '{print $2}'`
	SHARED_LIB=`grep SHARED_LIB $TEMPLATE | awk -F: '{print $2}'`
	CFLAGS=`grep CFLAGS $TEMPLATE | awk -F: '{print $2}'`
	SRCH_INC=`grep SRCH_INC $TEMPLATE | awk -F: '{print $2}'`
	SRCH_LIB=`grep SRCH_LIB $TEMPLATE | awk -F: '{print $2}'`
	USE_LOCALE=`grep USE_LOCALE $TEMPLATE | awk -F: '{print $2}'`
	DLSUFFIX=`grep DLSUFFIX $TEMPLATE | awk -F: '{print $2}'`
	DL_LIB=`grep DL_LIB $TEMPLATE | awk -F: '{print $2}'`
	YACC=`grep YACC $TEMPLATE | awk -F: '{print $2}'`
	YFLAGS=`grep YFLAGS $TEMPLATE | awk -F: '{print $2}'`
fi

$ECHO_N "Additional directories to search for .h files [$SRCH_INC]: $ECHO_C"
read a
if [ "$a." = " ." ]
then
        CPPFLAGS=
else
	if [ "$a." != "." ]; then
		SRCH_INC = $a
	fi
        CPPFLAGS=`echo "$SRCH_INC" | sed 's@  *@ @g; s@^\([^ ]\)@-I\1@; s@ \([^ ]\)@ -I\1@g'`
fi


$ECHO_N "Additional directories to search for library files [$SRCH_LIB]: $ECHO_C"
read a
if [ "$a." = " ." ]
then
	LDFLAGS=
else
        if [ "$a." != "." ]; then
                SRCH_LIB = $a
        fi
        LDFLAGS=`echo "$SRCH_LIB" | sed 's@  *@ @g; s@^\([^ ]\)@-L\1@; s@ \([^ ]\)@ -L\1@g'`
fi

IDIR=/usr/local/pgsql
$ECHO_N "Installation directory [/usr/local/pgsql]: $ECHO_C"
read a
if [ "$a." != "." ]
then
        IDIR=${a}
fi

echo ""
echo "Define USE_LOCALE to get Postgres work (sort, search)"
$ECHO_N "with national alphabet. [$USE_LOCALE]: $ECHO_C"
read a
if [ "$a." != " ." ]
then
	USE_LOCALE=no
else
        if [ "$a." != "." ]; then
                USE_LOCALE=$a
        fi
fi

DEF_PGPORT=5432
echo ""
echo "DEF_PGPORT is the TCP port number on which the Postmaster listens by"
echo "default.  This can be overriden by command options, environment "
echo "variables, and the postconfig hook."
echo ""

$ECHO_N "Define DEF_PGPORT to [$DEF_PGPORT]: $ECHO_C"
read a
if [ "$a." != "." ]
then
        DEF_PGPORT=${a}
fi

NOHBA=no
echo ""
$ECHO_N "Do you wish to disable Host Based Authentication(HBA) [no]: $ECHO_C"
read a
if [ "$a." != "." ]
then
        NOHBA=${a}
fi

export BUILDRUN USE_LOCALE DEF_PGPORT NOHBA AROPT DLSUFFIX DL_LIB YACC
export SHARED_LIB CFLAGS CPPFLAGS LDFLAGS YFLAGS

./configure --prefix=${IDIR}

