#!/bin/csh -f
#
#	to specify port info on the command line, use
#
#		newport TREE PORT OBJDIR
#
#	where TREE = root of postgres tree, PORT = name of port (sun4, dec,
#	etc), and OBJDIR = name of object directory (relative to TREE).
#	if any of these is the null string you will be prompted for that
#	item.
#
set PORT = ""
set OD = ""
set TREE = ""

if ($1 == "-q") then
	set quiet = 1
	shift
else
	set quiet = 0
endif

if ( $#argv >= 1 ) then
	set TREE = $argv[1]
	if ( $#argv >= 2 ) then
		set PORT = $argv[2]
		if ( $#argv == 3 ) then
			set OD = $argv[3]
		endif
	endif
endif

if ( $TREE == "" ) then
	set confdir = `pwd`
	chdir ..
	set TREE = `pwd`
else
	set confdir = $TREE/newconf
	chdir $TREE
endif

if ( $PORT == "" ) then
    top:
	chdir $confdir/../src/port
	echo "ports available are: " *
	echo -n "please choose a port: "
	set dir = $<
	if ( $dir == "" ) goto top
	if ( ! -d $dir ) then
	    echo "port does not exist, try again"
	    goto top
	endif
	set PORT = $dir
endif

if ($quiet == 0) echo ""

chdir $TREE

if ( $OD == "" ) then
	set OD = obj.$PORT
	echo -n "Object directory name ($OD) ? "
	set od = $<
	if ( $od != "" ) set OD = $od
endif

#
# Make config.mk
#
rm -f $TREE/newconf/config.mk
echo "TREE=	$TREE" > $TREE/newconf/config.mk
echo "OD=	$TREE/$OD" >> $TREE/newconf/config.mk
echo "SD=	$TREE/src" >> $TREE/newconf/config.mk
cat $TREE/newconf/CONFIG/config.mk.$PORT >> $TREE/newconf/config.mk
chmod 444 $TREE/newconf/config.mk

if ( ! -d $OD ) then
	if ($quiet == 0) echo creating object directory $OD
	mkdir $OD
endif

# now create directories in objdir
foreach i (`cat newconf/dirs.mk`)
	if ($quiet == 0) echo creating $OD/$i
	mkdir $OD/$i
end

cat > $OD/xmake << EOF
#!/bin/csh -f
#
# local compilation version (see dxmake for usage)
#
set OBJDIR = \$cwd
set PORT = $PORT
set GCFLAGS = -g
chdir ../newconf
exec Make "-dGCFLAGS=\$GCFLAGS" -dOD=\$OBJDIR -dPORTNAME=$PORT \$argv
EOF
chmod +x $OD/xmake

cat > $OD/lib/H/installinfo.h << EOF
#ifndef INSTALLINFO_H
#define INSTALLINFO_H

#define	DATAHOME "$TREE"			/* loc of data/ dir */
#define	AMIINT "$TREE/$OD/support/backend"	/* for vacuumd */
#define	SRCDIR "$TREE/src/support"		/* source directory */
#define	SCRDIR "$TREE/$OD/support"		/* script directory */

#endif
EOF
chmod 444 $TREE/$OD/lib/H/installinfo.h

if ($quiet == 0) echo linking source files...
chdir $TREE/newconf
if ($quiet == 0) then
	csh MakeLinks $TREE/$OD $TREE/src
else
	csh MakeLinks -q $TREE/$OD $TREE/src
endif

if (! -e $TREE/newconf/newport.stat) then
	touch $TREE/newconf/newport.stat
	chmod 444 $TREE/newconf/newport.stat
endif
