#!/bin/sh
#
# Copyright (c) 2000-2003 The Apache Software Foundation.
# See license at the end of this file.

if test "x$1" = "x-r"; then
  REMOVE=1
  shift
else
  REMOVE=0
fi

if test $# != 1; then
  echo Usage: $0 [-r] server_install_dir >&2
  exit 1
fi

SR=$1

if test -f "$SR/bin/httpd"; then
  :
else
  echo "$SR is not a complete web server installation"
  exit 1
fi

SYS=`uname -s`

## generally we expect that sed is found in PATH,
## but that has not always been the case

if test -x /bin/sed; then
  SED=/bin/sed
else
  if test -x /usr/bin/sed; then
    SED=/usr/bin/sed
  else
    SED=sed
  fi
fi

## remove symlink files for install or uninstall

rm -f $SR/lib/libapr-0.so $SR/lib/libapr-0.so.0 \
      $SR/lib/libaprutil-0.so $SR/lib/libaprutil-0.so.0 \
      $SR/lib/libexpat.so $SR/lib/libexpat.so.0 \
      $SR/lib/libapr.so $SR/lib/libaprutil.so \
      $SR/lib/libapr-0.sl $SR/lib/libapr-0.sl.9 \
      $SR/lib/libaprutil-0.sl $SR/lib/libaprutil-0.sl.9 \
      $SR/lib/libexpat.sl $SR/lib/libexpat.sl.1

if test $REMOVE = 1; then
  exit 0
fi

##
## find built-in serverroot for this platform
##
DEFAULT_SR=`grep '^exp_exec_prefix =' $SR/build/config_vars.mk`
DEFAULT_SR=`echo $DEFAULT_SR | $SED -e "s;exp_exec_prefix = ;;"`

##
##  determine path to (optional) Perl interpreter
##
PERL=no-perl5-on-this-system
perls='perl5 perl'
path=`echo $PATH | $SED -e 's/:/ /g'`
found_perl=0
 
for dir in ${path} ;  do
  for pperl in ${perls} ; do
    if test -f "${dir}/${pperl}" ; then
      if `${dir}/${pperl} -v >/dev/null 2>&1` ; then
        PERL="${dir}/${pperl}"
        found_perl=1
        break
      fi
    fi
  done
  if test $found_perl = 1 ; then
    break
  fi
done

## fix path to perl in apxs and dbmmanage

for F in apxs dbmmanage ; do
  $SED -e "s;#!/[^ ]*;#!$PERL;" \
    $SR/bin/$F \
    > $SR/bin/$F.new
  if test "x$UIP_DEBUG" = "x"; then
    mv $SR/bin/$F.new $SR/bin/$F
  fi
done

## fix path to install in various files

for F in build/config_vars.mk bin/apxs bin/apachectl bin/envvars \
bin/envvars-std lib/libapr-0.la lib/libaprutil-0.la lib/libexpat.la; do
  $SED -e "s;$DEFAULT_SR;$SR;g" \
    $SR/$F \
    > $SR/$F.new
  if test "x$UIP_DEBUG" = "x"; then
    mv $SR/$F.new $SR/$F
  fi
done

## fix install paths in apr-config and apu-config

for F in apr-config apu-config ; do
  $SED -e "s;^prefix=.*;prefix=\"$SR\";" \
      -e "s;^exec_prefix=.*;exec_prefix=\"$SR\";" \
      -e "s;^libdir=.*;libdir=\"$SR/lib\";" \
      -e "s;^datadir=.*;datadir=\"$SR\";" \
      -e "s;^includedir=.*;includedir=\"$SR/include\";" \
    $SR/bin/$F \
    > $SR/bin/$F.new
  if test "x$UIP_DEBUG" = "x"; then
    mv $SR/bin/$F.new $SR/bin/$F
  fi
done

## fix IHS build lib location in envvars, envvars-std, and .la files

case $SYS in
  OS/390)
    ;;
  *)
for F in bin/envvars bin/envvars-std lib/libapr-0.la lib/libaprutil-0.la lib/libexpat.la; do
  $SED -e "s;/IBMIHSBUILD;$SR;g" \
    $SR/$F \
    > $SR/$F.new
  if test "x$UIP_DEBUG" = "x"; then
    mv $SR/$F.new $SR/$F
  fi
done
esac

## fix symlinks in lib directory

case $SYS in
  OS/390)
    ;;
  HP-UX)
    ln -s libapr-0.sl.9.4 $SR/lib/libapr-0.sl
    ln -s libapr-0.sl.9.4 $SR/lib/libapr-0.sl.9
    ln -s libaprutil-0.sl.9.4 $SR/lib/libaprutil-0.sl
    ln -s libaprutil-0.sl.9.4 $SR/lib/libaprutil-0.sl.9
    ln -s libexpat.sl.1.0 $SR/lib/libexpat.sl
    ln -s libexpat.sl.1.0 $SR/lib/libexpat.sl.1
    ;;
  *)
    ln -s libapr-0.so.0.9.4 $SR/lib/libapr-0.so
    ln -s libapr-0.so.0.9.4 $SR/lib/libapr-0.so.0
    ln -s libaprutil-0.so.0.9.4 $SR/lib/libaprutil-0.so
    ln -s libaprutil-0.so.0.9.4 $SR/lib/libaprutil-0.so.0
    case $SYS in
      AIX)
        ln -s libapr-0.so $SR/lib/libapr.so
        ln -s libaprutil-0.so $SR/lib/libaprutil.so
        ;;
      *)
        ln -s libexpat.so.0.1.0 $SR/lib/libexpat.so
        ln -s libexpat.so.0.1.0 $SR/lib/libexpat.so.0
        ;;
    esac
    ;;
esac

## add "-d serverroot" option to HTTPD variable in apachectl

$SED -e "s;HTTPD=.*;HTTPD='$SR/bin/httpd -d $SR';" \
  $SR/bin/apachectl \
  >$SR/bin/apachectl.new
if test "x$UIP_DEBUG" = "x"; then
  mv $SR/bin/apachectl.new $SR/bin/apachectl
fi

## repair executable permissions in various files

## files modified by this script

for F in bin/apxs bin/apachectl bin/dbmmanage bin/apr-config bin/apu-config; do
  chmod 0755 $SR/$F
done

## files modified by the installer

for F in bin/adminctl bin/setupadm bin/ikeyman; do
  if test -f $SR/$F; then
    chmod 0755 $SR/$F
  fi
done

## default User and Group in conf file

case $SYS in
  HP-UX)
    DEFUSER=www
    DEFGROUP=other
    ;;
  *)
    DEFUSER=nobody
    DEFGROUP=nobody
    ;;
esac

for F in conf/httpd.conf.sample conf/httpd.conf.sample.afpa \
         conf/httpd.conf.default conf/admin.conf.default    \
         conf/httpd.conf conf/admin.conf; do
  if test -f $SR/$F; then
    $SED -e "s;@@User@@;$DEFUSER;g" -e "s;@@Group@@;$DEFGROUP;g" \
      $SR/$F \
      > $SR/$F.new
    if test "x$UIP_DEBUG" = "x"; then
      mv $SR/$F.new $SR/$F
    fi
  fi
done

# ====================================================================
# The Apache Software License, Version 1.1
#
# Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
# reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The end-user documentation included with the redistribution,
#    if any, must include the following acknowledgment:
#       "This product includes software developed by the
#        Apache Software Foundation (http://www.apache.org/)."
#    Alternately, this acknowledgment may appear in the software itself,
#    if and wherever such third-party acknowledgments normally appear.
#
# 4. The names "Apache" and "Apache Software Foundation" must
#    not be used to endorse or promote products derived from this
#    software without prior written permission. For written
#    permission, please contact apache@apache.org.
#
# 5. Products derived from this software may not be called "Apache",
#    nor may "Apache" appear in their name, without prior written
#    permission of the Apache Software Foundation.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# ====================================================================
#
# This software consists of voluntary contributions made by many
# individuals on behalf of the Apache Software Foundation.  For more
# information on the Apache Software Foundation, please see
# <http://www.apache.org/>.
#
# Portions of this software are based upon public domain software
# originally written at the National Center for Supercomputing Applications,
# University of Illinois, Urbana-Champaign.
#
