#!/bin/sh

MYDIR=`echo $0 | sed -e "s/[^\/]*$//g;"`
ADIR=$1

if [ -z "$ADIR" ]; then
  echo "precompile <apache-src-tre>";
  echo "example: precompile ../apache_1.3.6"
  exit
fi

(cd "$MYDIR./"; ./configure)

if [ -d "$ADIR" ]; then
  if [ -d "$ADIR/src/modules" ]; then
    if [ ! -d "$ADIR/src/modules/backhand" ]; then
      mkdir "$ADIR/src/modules/backhand"
    fi
    echo "Copying source into apache tree..."
    (cd "$MYDIR./"; tar cf - .) | (cd "$ADIR/src/modules/backhand"; tar xf - )
    echo "Copying sample cgi script and logo into htdocs directory..."
    cp "$MYDIR./backhand.jpg" "$ADIR/icons"
    chmod 600 "$ADIR/icons/backhand.jpg"
    cp "$MYDIR./cndsbar_mini.jpg" "$ADIR/icons"
    chmod 600 "$ADIR/icons/cndsbar_mini.jpg"
    cp "$MYDIR./mod_backhand_mini.jpg" "$ADIR/icons"
    chmod 600 "$ADIR/icons/mod_backhand_mini.jpg"
    cp "$MYDIR./backhand-test" "$ADIR/cgi-bin"
    chmod 700 "$ADIR/cgi-bin/backhand-test"
  fi
  CONF="$ADIR/configure"
  if [ -f "$CONF" ]; then
    FOS=`grep -n 'eval "module_${apc_optarg}=yes"' $CONF |\
	head -1`
    echo "Adding to auto mod_so inclusion to configure..."
    # Add it in just after mod_cgi, that is where we want it (???)
    if [ -z "`grep mod_backhand $CONF`" ]; then
      FO=`echo $FOS | cut -f1 -d:`
      FO=`echo "$FO 1 - p" | dc`
      FC=`cat $CONF | wc -l | sed -e 's/ //g;'`
      mv $CONF "$CONF.bak"
      (head -$FO "$CONF.bak";\
       echo '                  if [ ${apc_optarg} = "backhand" ]; then'; \
       echo '                     echo " + enabling mod_so for mod_backhand"'; \
       echo '                     some_shares=1'; \
       echo '                     module_so=yes'; \
       echo '                  fi'; \
	tail -`echo "$FC $FO - p" | dc` "$CONF.bak") > $CONF
      rm "$CONF".bak
      chmod 755 $CONF
    else
      echo "	already there!?  (you do this already?)"
    fi
  fi
  CONF="$ADIR/src/Configure"
  if [ -f "$CONF" ]; then
    FOS=`grep -n 'SOLARIS2' $CONF |\
	head -1`
    echo "Adding libs to Apache's Configure..."
    if [ -z "`grep kstat $CONF`" ]; then
      FO=`echo $FOS | cut -f1 -d:`
      FC=`cat $CONF | wc -l | sed -e 's/ //g;'`
      mv $CONF "$CONF.bak"
      (head -$FO "$CONF.bak";\
	echo "	LIBS=\"\$LIBS -lkstat -ldl -lm\"";\
	tail -`echo "$FC $FO - p" | dc` "$CONF.bak") > $CONF
      rm "$CONF".bak
      chmod 755 "$CONF"
    else
      echo "	already there!?  (you do this already?)"
    fi
  fi
  TMPL="$ADIR/src/Configuration.tmpl"
  if [ -f "$TMPL" ]; then
    FOS=`egrep -n '^[#	 ]*(Add|Shared)Module .*/mod_cgi.o' $TMPL |\
	head -1`
    echo "Adding to Apache's Configuration.tmpl..."
    # Add it in just after mod_cgi, that is where we want it (???)
    if [ -z "`grep mod_backhand.o $TMPL`" ]; then
      FO=`echo $FOS | cut -f1 -d:`
      FC=`cat $TMPL | wc -l | sed -e 's/ //g;'`
      mv $TMPL "$TMPL.bak"
      (head -$FO "$TMPL.bak";\
	echo "# AddModule modules/backhand/mod_backhand.o";\
	tail -`echo "$FC $FO - p" | dc` "$TMPL.bak") > $TMPL
      rm "$TMPL".bak
    else
      echo "	already there!?  (you do this already?)"
    fi
  fi
  if [ "SunOS" = "`uname`" ]; then
    TMPL="$ADIR/src/modules/backhand/Makefile.tmpl"
    mv "$TMPL" "$TMPL".bak
    echo "Setting extra shared libraries for Solaris (-lkstat -lm)"
    sed -e 's/STAT_LIBS=.*$/STAT_LIBS=-lkstat -lm/g;' < "$TMPL".bak > $TMPL
    rm "$TMPL".bak
  elif [ "FreeBSD" = "`uname`" ]; then
    TMPL="$ADIR/src/modules/backhand/Makefile.tmpl"
    mv "$TMPL" "$TMPL".bak
    echo "Setting extra shared libraries for FreeBSD (-lm)"
    sed -e 's/STAT_LIBS=.*$/STAT_LIBS=-lm/g;' < "$TMPL".bak > $TMPL
    rm "$TMPL".bak
  elif [ "Linux" = "`uname`" ]; then
    TMPL="$ADIR/src/modules/backhand/Makefile.tmpl"
    mv "$TMPL" "$TMPL".bak
    echo "Nullifying extra shared libraries for Linux"
    sed -e 's/STAT_LIBS=.*$/STAT_LIBS=/g;' < "$TMPL".bak > $TMPL
    rm "$TMPL".bak
  else
    echo "Your platform `uname` is not supported... good luck."
  fi
  DIST="$ADIR/conf/httpd.conf-dist"
  echo "Modifying httpd.conf-dist..."
  if [ -f "$DIST" -a -z "`grep MulticastStats $DIST`" ]; then
    DISTMOD="$ADIR/conf/.mod_backhand_mods"
    echo "	creating insertion...."
    cat << EOF > "$DISTMOD"


#  The following are the Directives necessary to get mod_backhand operational
#  You wll need to add Backhand directives to any directories that you wish
#  to balance..  For example, you could put the following inside a
#  ScriptAliased /cgi-cpu/ directory (for scripts that are cpu hogs)

# <Directory ...>
# ... stuff ...
# Backhand byAge
# Backhand byRandom
# Backhand byLogWindow
# Backhand byCPU
# ... stuff ...
# </Directory>

# This will elminate servers you haven't heard from in a while
# Then randomize those remaining
# Then take the first log(n)  (log base 2 of course)
# Then use the one with the highest idle CPU

<IfModule mod_backhand.c>
# UnixSocketDir is were the mod_backhand-Arriba file is stored
#   (how fast your machine is) This directory must be readable and writable
#   by euid of apache (nobody)  Becuase the children have dropped privledges
#   before they connect to UNIX domain socket in this directory.
# This directive is singular.
#
# MulticastStats of the form [<IP ADDR>] <BROADCAST ADDR>:<PORT> will set
#   mod_backhand to broadcast server statistics on that address advertising
#   for a server on <IP ADDR> or gethostbyname(gethostname())
# MulticastStats of the form [<IP ADDR>] <MULTICAST ADDR>:<PORT>,<ttl> will
#   set mod_backhand to multicast server statistics on that address
#   advertising for a server on <IP ADDR> or gethostbyname(gethostname())
# This directive is singular.
#
# AcceptStats <a.b.c.d>[/<mask>] (like 10.0.0.4 or 10.0.0.0/24) will accept
#   statistics originating from that IP or IP network.
# This option can be cascaded.

  UnixSocketDir @@ServerRoot@@/backhand
# MulticastStats 128.220.221.255:4445
  MulticastStats 225.220.221.20:4445,1
  AcceptStats 128.220.221.0/24

# This is a status of sorts.  Visit it and see how valuable it is to you.
  <Location "/backhand/">
    SetHandler backhand-handler
  </Location>
</IfModule>
EOF
    echo "	integrating insertion with original..."
    FOS=`egrep -n '^HostnameLookups (On|Off)' "$DIST" |\
	head -1`
    FO=`echo $FOS | cut -f1 -d:`
    FC=`cat $DIST | wc -l | sed -e 's/ //g;'`
    mv $DIST "$DIST.bak"
      (head -$FO "$DIST.bak";\
	cat "$DISTMOD";\
	tail -`echo "$FC $FO - p" | dc` "$DIST.bak") > $DIST
      rm "$DIST".bak "$DISTMOD"
  else
    echo "	already there!?  (you do this already?)"
  fi
  TMPL="$ADIR/Makefile.tmpl"
  if [ -f "$TMPL" ]; then
    FOS=`egrep -n '<=== \[mktree\]' $TMPL |\
	head -1`
    echo "Updating Makefile.tmpl..."
    mv $TMPL "$TMPL".bak
    grep -v backhand "$TMPL".bak > $TMPL
    rm "$TMPL".bak
    FO=`echo $FOS | cut -f1 -d:`
    FC=`cat $TMPL | wc -l | sed -e 's/ //g;'`
    HC=`echo "$FO 1 - p" | dc`
    APUSER=`egrep '^User (.*)' $DIST | sed -e 's/User //g;' | cut -f1 -d' '`
    REALUSER=`egrep "^$APUSER:" /etc/passwd`
    if [ -n "$REALUSER" ]; then
      mv $TMPL "$TMPL".bak
      (head -$HC "$TMPL".bak;\
	echo "	\$(MKDIR) \$(root)\$(prefix)/backhand";\
	echo "	chown \${conf_user} \$(root)\$(prefix)/backhand";\
	echo "	chgrp \${conf_group} \$(root)\$(prefix)/backhand";\
	echo "	chmod 700 \$(root)\$(prefix)/backhand";\
	tail -`echo "$FC $HC - p" | dc` "$TMPL.bak") > $TMPL
      rm "$TMPL".bak
    else
      echo "User $APUSER isnot in /etc/passwd, so I couldn't tell Apache to chown the"
      echo "Apache to chown the backhand directory to $APUSER.  You will have to chown"
      echo "\$prefix/backhand to the apahce uid."
    fi
  fi
fi


  if [ "SunOS" = "`uname`" ]; then
    cat <<EOF

=========================== SOLARIS NOTE ===============================

   Backhand uses shared memory and the httpd children processes must
attach to that memory.  We choose to allocate that shared memory with
user and group set to the apache user and group.  All shared memory
allocations happen during configuration.
   On Solaris, the User and Group directives in httpd.conf must be
specified before the LoadModule and AddModule Directives for
mod_backhand.
   If you do not do this, backhand will have no resource information for
the cluster and will not operate correctly.  You will see a error in
the apache error logs:

    [error] (22)Invalid argument: shmctl(., IPC_SET, [-1,-1])
	could not set segment #xxxx

   To remedy this, simply move the User and Group directives above the
LoadModule and ShareModule directives

EOF
    /bin/read
  fi
echo
echo
echo "Now change to the apache source directory:"
echo "	$ADIR"
echo "And do a ./configure..."
echo
echo "If you want to enable backhand (why would you have done this is you didn't)"
echo "then add:  --enable-module=backhand --enable-shared=backhand"
echo "to your apache configure command.  For eaxmple, I use:"
echo
cat <<EOF
   ./configure --prefix=/var/backhand --enable-module=so \\
	 --enable-module=rewrite --enable-shared=rewrite \\
	 --enable-module=speling --enable-shared=speling \\
	 --enable-module=info --enable-shared=info \\
	 --enable-module=include --enable-shared=include \\
	 --enable-module=status --enable-shared=status \\
	 --enable-module=backhand --enable-shared=backhand
EOF
