#!/bin/sh
#
# Postinstallscript written by Ron Record (rr@sco.com) 
# 

scriptname="$0"
step="$1"
keywords="$2"
pkglist="$3"

# Source in the standard functions library, ccsSetup.sh
. ccsSetup.sh

ccs_return_value=0

#=============================================================================
#
# getyn
#
# Prompt for yes or no answer - returns non-zero for no
#
# Usage: getyn "question"
# Argument is a quoted question
# Notes: getyn returns 0 if the answer is y or Y
#        and 1 if the answer is n or N
#        and 2 if the answer is q or Q
#=============================================================================
getyn() {
        while   echo "$* (y/n/q) \c"
        do      read yn rest
                case $yn in
                [yY])   return 0                                ;;
                [nN])   return 1                                ;;
                [qQ])   return 2                                ;;
                *)      echo "Please Make a selection\n"          ;;
                esac
        done
}

DOCLINK=/usr/local/lib/apache/htdocs/docs

MakeDocLink()
{
    rm -f $DOCLINK
    ln -s /usr/local/man/html $DOCLINK
}

DisableStop()
{
    /etc/apache disable > /dev/null 2>&1
    /etc/apache stop > /dev/null 2>&1
}

CleanUpLinks()
{
    rm -f /etc/rc2.d/S91apache
    rm -f $DOCLINK
}

FixUpHost()
{
  HOSTPROG=/usr/bin/hostname
  if [ -x $HOSTPROG ]
  then
    HOSTNAME=`$HOSTPROG`
  else
    HOSTNAME=localhost
  fi
  [ "$HOSTNAME" ] || HOSTNAME=localhost
  HOST_FILES="/usr/local/lib/apache/conf/httpd.conf \
              /usr/local/lib/apache/conf/httpd.conf.default \
              usr/local/lib/apache/conf/jserv/jserv.conf"
  for i in $HOST_FILES
  do
    [ -f $i ] && {
        sed -e "s/disco.pdev.sco.com/$HOSTNAME/g" < $i > /tmp/_ccs$$
        mv /tmp/_ccs$$ $i
        rm -f /tmp/_ccs$$
        chown root:sys $i
    }
  done
}

DisableNetscape() 
{
  FTRC="S90fasttrack S90atlas S91manahttp"

  echo "The Apache web server is preconfigured to run on port 80."
  echo "In order to do so, you must first disable any existing server"
  echo "running on port 80. Often this is the Netscape FastTrack server."
  getyn "\nDo you wish to disable the Netscape FastTrack web server? "

  if [ $? -eq 0 ]
  then
    [ -x /usr/internet/ns_httpd/httpd-80/stop ] && {
      /usr/internet/ns_httpd/httpd-80/stop
    }
    [ -x /usr/internet/ns_httpd/stop-admin ] && {
      /usr/internet/ns_httpd/stop-admin
    }
    for i in $FTRC
    do
      grep "SCO Skunkware Apache" $i > /dev/null 2>&1 || {
        echo "exit 0 # Placed here by SCO Skunkware Apache" > /tmp/$i
        cat /etc/rc2.d/$i >> /tmp/$i
        cp /tmp/$i /etc/rc2.d/$i
        rm -f /tmp/$i
      }
    done
  fi
}

EnableNetscape() 
{
    FTRC="S90fasttrack S90atlas S91manahttp"

    for i in $FTRC
    do
      grep "SCO Skunkware Apache" $i > /dev/null 2>&1 && {
        grep -v "SCO Skunkware Apache" /etc/rc2.d/$i > /tmp/$i
        cp /tmp/$i /etc/rc2.d/$i
        rm -f /tmp/$i
      }
    done
}

case "$step" in
        POST_EXPORT) MakeDocLink
                     FixUpHost
                     DisableNetscape ;;
        PRE_UNEXPORT) DisableStop ;;
        POST_UNEXPORT) CleanUpLinks
                     EnableNetscape ;;
esac

exit $ccs_return_value

