#! /bin/sh

if [ "$1" != configure ]
then
    exit 0
fi

# Echo without starting a new line.
    
if [ "`echo -n a`" = "-na" ]
then     
    echon() {
        echo "$@\c"       
    }
else                     
    echon() {    
        echo -n "$@"
    }
fi                 

# Ask a simple question.

yorn() {
    _answers='y or n'

    if [ "$1" = -h ]
    then
	_help="$2"
	shift 2
	_answers='y, n, or either h or ? for help'
    fi

    _def="[$1]"

    _prompt="$2"
    _again="$3"

    : ${_again:="$_prompt"}

    boolean_answer=

    until [ ! -z "$boolean_answer" ]
    do
	echon "$_prompt" "$_def "
	read _ans
	case "$_ans" in
	    "")
		boolean_answer="$1"
		;;
	    y|Y)
		boolean_answer=y
		;;
	    n|N)
		boolean_answer=n
		;;
	    h|H|\?)
		if [ ! -z "$_help" ]
		then
		    boolean_answer=h
		    if [ -r "$_help" ]
		    then
		    	sed 's/^/    /' "$_help" | $PAGER
		    else
			echo 'Sorry, no help is available...'
		    fi
		fi
		;;
	esac
	if [ -z "$boolean_answer" ]
	then
	    echo "Please answer $_answers."
	    _prompt="$_again"
	else
	    if [ "$boolean_answer" = h ]
	    then
		boolean_answer=
	    fi
	fi
    done

    unset _def _prompt _again _help _answers
}

cat <<EOF
The Common Gateway Interface scripts will now be installed on your system.
The default way of installation is to copy the scripts to their final
location, but you may have a setup that allows symbolic links to be used
instead, in which case you may prefer to use that.
EOF
yorn n 'Install by creating symbolic links?'
if [ $boolean_answer = y ]
then
    symlinks="--symlinks"
fi

cgiscriptsconfig $symlinks

cat <<EOF

You can move the Common Gateway Interface scripts to their correct location
or to any place you choose at any time by calling cgiscriptsconfig again.
EOF
if [ ! -z "$symlinks" ]
then
    echo "(Use the --symlinks arguments to create symbolic links again.)"
fi

if [ -x /usr/sbin/apachechkperms ]
then
    echo
    echo Fixing scripts permissions for Apache...
    echo
    /usr/sbin/apachechkperms `2>/dev/null cat /var/lib/cgi-scripts`
fi

