#!/bin/sh

# (c) Zeus Technology Limited 2000-2001.  All rights reserved.
# 
# Copyright in the source code ("the Source Code") listed below in whatever
# form, whether printed electronic compiled or otherwise, belongs to Zeus
# Technology Limited ("we"/"us").
# 
# If you have entered into a written agreement with us under which the Source
# Code is licensed to you ("Licence"),  you may not use, sell, license,
# transfer, copy or reproduce the Source Code in whole or in part or in any
# manner or form other than in accordance with your Licence.  To do so is
# strictly prohibited and may be unlawful and a serious criminal offence.
# 
# If you have not entered into a Licence, disclosure of the Source Code is
# made "as is".   You may use the Source Code for non-commercial purposes
# only.  You may distribute the Source Code to individual third parties for
# their non-commercial purposes only, but only if (1) you acknowledge our web
# site as the source of the Source Code and include such acknowledgement and
# our web address (www.zeus.com) in any copy of the Source Code; (2) the
# Source Code you distribute is complete and not modified in any way and
# includes this notice; and (3) you inform such third parties that these
# conditions apply to them and that they must comply with them.
# 
# If you have not entered into a Licence, all express and implied warranties,
# conditions, terms, undertakings and representations, including without
# limitation as to quality, performance, fitness for purpose, or
# non-infringement, are excluded to the fullest extent permitted by law.
# Neither we nor any other person involved in disclosing the Source Code shall
# have any liabilities whatsoever to you, howsoever arising, in connection
# with the Source Code, or its use by you.
# 
# If you have not entered into a Licence but would like to do so,  please
# contact us at pepp@zeus.com.

if [ "x$ZEUSHOME" = "x" ]
then
    echo "Please set ZEUSHOME variable to where Zeus installation lives"
    exit 1
fi

echo "ZEUSHOME set to $ZEUSHOME"

if [ $# -eq 1 ]
then
    INSTALLDIR=$1
else
    echo "Where is your SPEC docroot? [/space/specweb] "
    read INSTALLDIR
    [ -z "$INSTALLDIR" ] && INSTALLDIR="/space/specweb"
fi

if [ ! -d $INSTALLDIR/cgi-bin ]
then
    mkdir $INSTALLDIR/cgi-bin
fi

if [ ! -d $INSTALLDIR/isapi-bin ]
then
    mkdir $INSTALLDIR/isapi-bin
fi

cat <<EOF > /tmp/spec_install.$$
#!$ZEUSHOME/perl/miniperl

\$infile = "";
open( INFILE, "\$ARGV[0]" );
while( <INFILE> ) { \$infile .= \$_; }
close( INFILE );

\$oldpathstr = "Zz" x 40; \$newpathstr = \$oldpathstr;
substr( \$newpathstr, 0, length("$INSTALLDIR\0") ) = "$INSTALLDIR\0";

\$oldhomestr = "Zh" x 40; \$newhomestr = \$oldhomestr;
substr( \$newhomestr, 0, length("$ZEUSHOME\0") ) = "$ZEUSHOME\0";

\$infile =~ s/\$oldpathstr/\$newpathstr/g;
\$infile =~ s/\$oldhomestr/\$newhomestr/g;

open( OUTFILE, "> $INSTALLDIR/\$ARGV[1]/\$ARGV[0]" );
print OUTFILE \$infile;
close( OUTFILE );
chmod 0755, "$INSTALLDIR/\$ARGV[1]/\$ARGV[0]";

EOF

for i in zeus-get.api zeus-cad.api zeus-post.api ; do
    echo Installing $i in $INSTALLDIR/isapi-bin ...
    [ -f $INSTALLDIR/isapi-bin/$i ] && rm -f $INSTALLDIR/isapi-bin/$i
    $ZEUSHOME/perl/miniperl /tmp/spec_install.$$ $i isapi-bin
done

for i in zeus-house.cgi nph-zeus.cgi ; do
    echo Installing $i in $INSTALLDIR/cgi-bin ...
    [ -f $INSTALLDIR/cgi-bin/$i ] && rm -f $INSTALLDIR/cgi-bin/$i
    $ZEUSHOME/perl/miniperl /tmp/spec_install.$$ $i cgi-bin
done

rm -f /tmp/spec_install.$$

# If we aren't root, assume that the web server child will be running as
# this user, and that therefore we don't need to be setuid.
if id | grep "uid=0" >/dev/null; then
    chown root $INSTALLDIR/cgi-bin/zeus-house.cgi
    chmod u+s  $INSTALLDIR/cgi-bin/zeus-house.cgi
fi
