#! /bin/sh

# cgiscriptsconfig, by Yves Arrouye <arrouye@debian.org>

usage() {
    >&2 echo "usage: `basename $0` [ --force ] [ --overwrite ] [ --symlinks ] [ --printdir ] [ --cgidir directory ]"
    >&2 echo "       `basename $0` --remove"
    exit 1
}

cmd="install -m 750"
del=0
inst=0

force=0
overwrite=0
print=0

exec 3>&1 1>/dev/tty

while [ $# -ne 0 ]
do
    case "$1" in
	--force)
	    if [ $del -eq 1 ]
	    then
		usage
	    fi
	    force=1
	    inst=1
	    ;;
	--overwrite)
	    if [ $del -eq 1 ]
	    then
		usage
	    fi
	    overwrite=1
	    inst=1
	    ;;
	--symlinks)
	    if [ $del -eq 1 ]
	    then
		usage
	    fi
	    cmd="ln -s"
	    inst=1
	    ;;
	--printdir)
	    print=1
	    ;;
	--cgidir)
	    test $# -gt 1 || usage
	    cgi_home="$2"
	    shift
	    inst=1
	    ;;
	--remove)
	    if [ $inst -eq 1 ]
	    then
		usage
	    fi
	    del=1
	    ;;
	*)
	    usage
	    ;;
    esac
    shift
done

# Delete old files.

delfile() {
    if [ "`md5sum $1 | sed 's/ .*//'`" = \
	"`2>/dev/null md5sum $2 | sed 's/ .*//'`" ]
    then
	rm -f "$1"
    fi
}

if [ -f /var/lib/cgi-scripts ]
then
    old_home=`cat /var/lib/cgi-scripts`
    if [ $old_home != /usr/lib/httpd/cgi-bin ]
    then
        for f in `2>/dev/null find $old_home ! -type d -print`
        do
	    bf=`basename $f`
	    delfile $f /usr/local/lib/httpd/cgi-bin/$bf
	    delfile $f /usr/lib/httpd/cgi-bin/$bf
        done
    fi
fi

if [ $del -eq 1 ]
then
    rm -f /var/lib/cgi-scripts
    exit 0
fi

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

# Try to determine where to put the scripts by looking at configuration
# files.

if [ -z "$cgi_home" ]
then
    apacheconfig=/usr/lib/apache/config/scripts

    if [ -f /etc/httpd/srm.conf ]
    then
	srm=/etc/httpd/srm.conf
    fi

    if [ -r ${apacheconfig}/apache.sh ]
    then
    	. ${apacheconfig}/apache.sh
    	_apache_sh_testloaded
    fi

    if [ -r "${srm}" ]
    then
	cgi_home=`grep ScriptAlias ${srm} | grep -v '#' | head -1 |
	    awk '{ print $3; }'`
	if [ ! -z "$cgi_home" ]
	then
	    cgi_home=`serverfilename "$cgi_home"`
	fi
    fi
fi

if [ -z "$cgi_home" ]
then
    wwwhome=`2>/dev/null ypcat passwd | grep ^www-data: | cut -d: -f6`
    : ${wwwhome:=`2>/dev/null grep ^www-data: /etc/passwd | cut -d: -f6`}
    : ${wwwhome:=/home/www-data}
    cgi_home=$wwwhome/cgi-bin

    # No, use the default for Apache.

    cgi_home=/usr/lib/httpd/cgi-bin

    # Ask if the user really wants this directory, because it was
    # guessed.

    force=1

    cat <<EOF
No existing Web configuration has been recognized: the scripts will be
installed in a guessed location, which you may want to override.
EOF
fi

cgi_home=`echo $cgi_home | sed 's,/*$,,'`
: ${cgi_home:=/}

if [ $force -eq 1 ]
then
    echon "Scripts location? [$cgi_home] "
    read loc
    : ${loc:=$cgi_home}
    while ! mkdir -p $loc 2>/dev/null
    do
	echo "The directory \`$loc' cannot be created."
        echon "Scripts location? [$cgi_home] "
        read loc
    done
    cgi_home="$loc"
fi

# Link or copy the files.

cgi_home=`echo $cgi_home | sed 's,/*$,,'`
: ${cgi_home:=/}

if [ $cgi_home != /usr/lib/httpd/cgi-bin ]
then

mkdir -p $cgi_home
cd $cgi_home

for f in `2>/dev/null find /usr/local/lib/httpd/cgi-bin \
    /usr/lib/httpd/cgi-bin ! -type d -print`
do
    if [ $overwrite -eq 1 -o ! -e `basename $f` ]
    then
	bf=`basename $f`
	rm -f $bf
    	$cmd $f .
	2>/dev/null chown www-data.www-data $bf
    fi
done

fi

echo $cgi_home >/var/lib/cgi-scripts

