:
# CGI trigger installation
#
# @(#)cobrun.inst	7.1

if test ! -w /etc/passwd
then
        echo "$0: You must be root to run this script"
        exit 1
fi

case .$COBDIR in
	.) echo $0: COBDIR not set ;;
esac

NL='\n'
FIRST_DEF_CGI_BIN="$*"
DEF_CGI_BIN="$*"


# Search multi-path cobdir for COBDIR_BIN, COBDIR_LIB and COBDIR_ETC
OLDIFS=$IFS
OLDCMDLINE=$*
IFS=:
set -- $COBDIR
for i in $*
do
        if test -d $i/bin
        then
                COBDIR_BIN=$i/bin
        fi

        if test -d $i/lib
        then
                COBDIR_LIB=$i/lib
        fi

        if test -d $i/etc
        then
                COBDIR_ETC=$i/etc
        fi
done
IFS=$OLDIFS
set -- $OLDCMDLINE

# add cgibin to cobdir... 
PATH=$COBDIR_BIN/cgibin:$PATH
export PATH

echo -------------------------------------------------------------------------
echo  Micro Focus Installation - CGI-BIN Installation of cobrun, cobrun_t
echo -------------------------------------------------------------------------

echo
if test ! -f usercgi.cfg
then
	 mfcgicfg create
         if test ! -f usercgi.cfg
         then
              echo $0: failed to create usercgi.cfg - aborting...
              exit 255
         fi
	echo '*** usercgi.cfg created... ALL users set to access=denied'
	echo
else
	echo '*** usercgi.cfg already exists. usercgi.cfg preserved.'
fi


echo
echo '     To manipulate usercgi.cfg use mfcgicfg or a text editor'
echo
echo '        For example to add a user....'
echo '         ./mfcgicfg: add <name> <directory <options>'
echo
echo '     For more information type mfcgicfg after installation'
echo
echo Press return to continue
read a

#
# On AIX v4.3 onwards, /etc/environment defines where a default CGI-BIN is..
#
if test -f /etc/environment
then
	DEF_CGI_BIN="`cat /etc/environment | fgrep CGI_DIRECTORY | sed 's/=/ /' | awk '{ print $2 }'`$NL$DEF_CGI_BIN"
fi

#
# Lotus Domino and iccs install a default cgi-bin directory
# /usr/lpp/internet/server_root/cgi-bin
#
if test -d /usr/lpp/internet/server_root/cgi-bin
then
	DEF_CGI_BIN="/usr/lpp/internet/server_root/cgi-bin$NL$DEF_CGI_BIN"
fi

#
# Netscape fasttrack server installs the cgi-bin directory into:
# /usr/ns-home/cgi-bin
#
if test -d /usr/ns-home/cgi-bin
then
	DEF_CGI_BIN="/usr/ns-home/cgi-bin$NL$DEF_CGI_BIN"
fi


#
# Netscape Enterprise server installs the cgi-bin directory...
# /usr/ns-home/cgi-bin
if test -d /usr/netscape/suitespot
then
	EVERY_CGI_BIN=`find /usr/netscape/suitespot -name 'cgi-bin' -print`
	case .$EVERY_CGI_BIN in
		.) ;;
		*)
			for i in $EVERY_CGI_BIN
			do
				DEF_CGI_BIN="$i$NL$DEF_CGI_BIN"
			done
			;;
	esac
fi

#
# apache is often installed into /etc/httpd or /usr/local/apache
#
# The line in srm.conf that we are interested in is....
# 	ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/
#
if test -f /etc/httpd/conf/srm.conf 
then
	TMPLINE=`egrep "^ScriptAlias" /etc/httpd/conf/srm.conf | fgrep '/cgi-bin/'`
	case .$TMPLINE in
		.) ;;
		*) 	OLDCMDLINE=$*
			set -- $TMPLINE
			DEF_CGI_BIN="$3$NL$DEF_CGI_BIN"
			set -- $OLDCMDLINE
			;;
	esac
fi

if test -f /usr/local/apache/conf/httpd.conf 
then
	TMPLINE=`egrep "^ScriptAlias" /usr/local/apache/conf/httpd.conf | fgrep '/cgi-bin/' | sed 's/"//g'`
	case .$TMPLINE in
		.) ;;
		*) 	OLDCMDLINE=$*
			set -- $TMPLINE
			DEF_CGI_BIN="$3$NL$DEF_CGI_BIN"
			set -- $OLDCMDLINE
			;;
	esac
fi

#
# remove any duplicates
#
# sorry this will not work with spacey filenames....
#
echo $DEF_CGI_BIN >$$.tmp
touch $$.ok
uniq $$.tmp | while read CGI_BIN
do
	if test -d "$CGI_BIN"
	then
		echo CGI-BIN Found @ $CGI_BIN
		echo $CGI_BIN >>$$.ok
	fi
done
rm $$.tmp
CGI_DIRS=`cat $$.ok`
rm $$.ok

case .$CGI_DIRS in
	.) echo $0: No default CGI-BIN directory found... execute install script
           echo Use: $0 my-cgi-bin-directory
	   exit ;;
	*) ;;
esac

# TODO: this should be multi-cobdir enabled
COBRUN=$COBDIR_BIN/cobrun
if test ! -f $COBRUN
then
	echo $0: unable to find cobrun on '$COBDIR'
	exit 255
fi

for i in $CGI_DIRS
do
	echo 
	CGI_BIN=$i
	if test -d $CGI_BIN
	then
		echo "Would you like to install the cgi enabled 'cobrun' triggers into directory:"
		echo "   -> $CGI_BIN"
		echo
		echo "? (yes/no/y/n)"
		read answer
		case .$answer in
			.n|.no|.N|.NO|.) echo $i - not installed ;;
			.y|.yes|.Y|.YES)
				cp $COBRUN $CGI_BIN/cobrun
				test $? -ne 0 && "echo $0: failed to copy cobrun;exit 1"

				cp $COBRUN $CGI_BIN/cobrun_t
				test $? -ne 0 && "echo $0: failed to copy cobrun_t;exit 1"
				cp usercgi.cfg $CGI_BIN/usercgi.cfg
				test $? -ne 0 && "echo $0: failed to copy usercgi.cfg;exit 1"

				chown root $CGI_BIN/cobrun $CGI_BIN/cobrun_t $CGI_BIN/usercgi.cfg
				test $? -ne 0 && "echo $0: failed to setid cobrun or usercgi.cfg;exit 1"

				chmod 4755 $CGI_BIN/cobrun_t
				chmod 4755 $CGI_BIN/cobrun
				chmod 400 $CGI_BIN/usercgi.cfg
				test $? -ne 0 && "echo $0: permissions error;exit 1"
				echo CGI enabled cobrun: installed into $CGI_BIN
				;;
			*)	echo $answer - unknown reply... assuming NO...
				;;
		esac
	else
		echo ?? $CGI_BIN
	fi

done

echo 

mfcgicfg check
