# Declare directorys where we can publish the files
HTMLDIR=$HOME/www/html
CGIBIN=$HOME/www/cgi-bin
# What programs are used....
MAINFILE="cgiprg2.cbl"
CBLFILES="cgiprg2.cbl"
HTMLFILES="cgiform2.htm"
MAINHTML="cgiform2.htm"
CGIBINHTML="outform2.htm outform4.htm"

WEBSERVER='-C webserver"cgi"'

# No parameters... go set the default
if test $# -eq 0
then
	set -- int
fi

while test $# -gt 0
do
	case $1 in
		cleanup)
			echo $0: Cleaning up...
			rm -f *.gnt *.int *.idy *.o
			rm -f `basename $MAINFILE .cbl`.so
			rm -f `basename $MAINFILE .cbl`
			exit
			;;

		threaded)
			THREADED="-t"
			;;

		noanim)	ANIM="no"
			GLNK=
			;;

		anim) ANIM="yes"
			;;

		int) MAINOPTS="-vi"
		     ANIMOPT="-a"
		     OPTS='-vi'
		     PUB_EXT="int"
			;;
		gnt) MAINOPTS="-vu"
		     ANIMOPT="-ag"
		     OPTS="-vu"
		     PUB_EXT="gnt"
			;;
		exe) MAINOPTS="-vxc"
		     OPTS="-vxc"
		     ANIMOPT="-ag"
		     GLNK=-g
		     LNK=-vxU
		     LNKEXT=
		     PUB_EXT=
			;;
		so) MAINOPTS="-zvc"
		     OPTS="-zvcU"
		     ANIMOPT="-ag"
		     GLNK=-g
		     LNK=-vzU
		     LNKEXT=".so"
		     PUB_EXT=
			;;
		*) echo $0: '[cleanup] [threaded] [int|gnt|exe|so] [noanim]'
		   exit 255
	esac

	shift
done

case .$ANIM in
	.no) ANIMOPT=
		;;
esac



if test ! -d $HTMLDIR
then
	echo $0: Directory to publish html files does not exist
	exit 255
fi

if test ! -d $CGIBIN
then
	echo $0: Directory to publish html files does not exist
	exit 255
fi

if test ! -f $CGIBIN/.mfcgienv
then
	if test ! -f $HOME/.mfcgienv
	then
		echo $0: .mfcgienv does not exist, this demos used the cgi triggers
		exit 255
	fi
fi

#
# Let us build the programs
#
for i in $CBLFILES
do
	case $i in
		$MAINFILE)	echo 'Compiling main program : '$i
				cob $MAINOPTS $ANIMOPT $WEBSERVER $THREADED $i
				if [ $? -ne 0 ]
				then
					echo $0: failed to compile $i
					exit 255
				fi
				;;
		*)	        echo 'Compiling program      : '$i
			        cob $OPTS $ANIMOPT $THREADED $i 
				if [ $? -ne 0 ]
				then
					echo $0: failed to compile $i
					exit 255
				fi
				
				;;
	esac
	echo
done

#
# This assumes the exe name or shared object name is the same as the
# basename of exe or shared object
#
case .$LNK in
	.) ;;
	*) OBJS=""
	   for i in $CBLFILES
	   do
		O=`basename $i .cbl`.o
		OBJS="$OBJS $O"
           done
	cob $LNK $GLNK $OBJS -o `basename $MAINFILE .cbl`$LNKEXT
	if [ $? -ne 0 ]
	then
		echo $0: failed to compile $i
		exit 255
	fi
	PUBLISH_BINS="`basename $MAINFILE .cbl`$LNKEXT $PUBLISH_BINS"
	;;
esac

echo 
echo Publishing files....
echo

#
# Now publish the files...
#
for i in $HTMLFILES
do
	if test -f $HTMLDIR/$i
	then
		chmod +w $HTMLDIR/$i
	fi
        sed s/\<USERID\>/$LOGNAME/ $i >$HTMLDIR/$i
	echo Updated: html-dir: $i in $HTMLDIR
	chmod 444 $HTMLDIR/$i
done	


#
# Now publish the files...
#
for i in $HTMLBINFILES
do
	if test -f $HTMLDIR/$i
	then
		chmod +w $HTMLDIR/$i
	fi
	cp $i $HTMLDIR/$i
	chmod 444 $HTMLDIR/$i
done	

#
# What extra files need to be published eg: *.int...
#
case .$PUB_EXT in
	.) ;;
	*) PUBLISH_BINS="*.$PUB_EXT $PUBLISH_BINS"
	   ;;
esac

#
# Are we compling for animatiom?  yes... the copy the .idys...
case .$ANIMOPT in
	.)  ;;
	*) PUBLISH_BINS="*.idy $PUBLISH_BINS"
esac

# Publish cgi-bin, htmls
for i in $CGIBINHTML
do
	if test -f $CGIBIN/$i
	then
		chmod +w $CGIBIN/$i
	fi
        sed s/\<USERID\>/$LOGNAME/ $i >$CGIBIN/$i
	echo Updated: cgi-bin: $i in $CGIBIN
done	

# Publish cgi-bin, raw files
for i in $PUBLISH_BINS $CBLFILES
do
	if test -f $CGIBIN/$i
	then
		chmod +w $CGIBIN/$i
	fi
	cp $i $CGIBIN/$i
	echo Updated: cgi-bin: $i in $CGIBIN 
done	
