#!/bin/bash

# exit, unless the command is 'start'
# NB: no re-init on re-start, supporting this will require invasion into
# the httpd init script, or replacing it altogether. OK for now, since we
# don't support re-parameterizing a running appliance, anyway.
if [[ $1 != 'start' ]] ; then
   exit 0
fi

# check appliance configuration; exit without error, if not present
if ! [[ -f /etc/applogic.sh ]] ; then
   echo "Warning: HTTPD appliance pre-run script called, but no applogic config found"
   exit 0
fi

# get config data
cd /appliance || exit $?
. /etc/applogic.sh

# -----------------------------------------------------------------------------
# resolve a terminal name from a 'hosts' file (given as stdin to this function)
function get_trm ()
   {
   local i=
   local a=
   local t=$1
   while read i ; do
      [[ -n "$i" ]]     || continue
      set $i >/dev/null || continue
      [[ ${1:0:1} == "#" ]] && continue
      a=$1
      shift
      for j in "$@" ; do
         if [[ "$j" == "$t" ]] ; then echo "$a" ; return 0 ; fi
      done
   done
   }

# ensure that a file and its parent sub-directories exist
function d_touch ()
   {
   local d=${1%/*}
   mkdir -p "$d" || return $?
   touch "$1"
   }

function b_die ()
   {
   /appliance/vme id=start_failed msg="ERROR!!: $1"
   echo "ERROR!!: $1" >> /appliance/log
   exit 4
   }

# -----------------------------------------------------------------------------
fs=`get_trm fs </etc/hosts`

# Check if we have proper content storage
if [ "$_content_on_fs" == off ]; then
   # We should have local fs
   if [ ! -b /dev/hda3 ]; then
      b_die "content_on_fs is 'off' but the local content volume is missing."
   fi
else
   [ "$fs" = "0.255.255.255" ] && b_die "content_on_fs is 'on' but the fs terminal is not connected."
fi

# clean up name cache
rm -f /var/cache/samba/gencache.tdb

# mount 'fs', if connected
# $fs == "" is a fatal error, actually (mis-configured /etc/hosts file)
if [[ "$fs" != "0.255.255.255" ]] ; then
   umount /mnt/fs 2>/dev/null # to allow re-starts
   mkdir -p /mnt/fs || exit $?
   for ((i=0; i<18; i=i+1))
   do
      mount /mnt/fs
      _ret=$?
      if [ $_ret -eq 0 ]; then
         break
      fi
      sleep 10
   done
   if [ $_ret -ne 0 ]; then
      b_die "Failed to mount fs share."
      exit $_ret
   fi
fi

# set up content
[ -d /mnt/content ] && rmdir /mnt/content 2>/dev/null
[ -L /mnt/content ] && rm /mnt/content 2>/dev/null
umount /mnt/content 2>/dev/null # to allow re-starts
if [ "$_content_on_fs" == off ]; then
   mkdir -p /mnt/content || exit $?
   mount /mnt/content || b_die "Failed to mount content volume."
else
   (cd /mnt && ln -s /mnt/fs content) || b_die "Failed to set up content on fs."
fi

# verify docs and scripts directory exist
if [ ! -d /mnt/content/$_docs_dir ]; then
   b_die "docs directory '$_docs_dir' does not exist on content volume"
fi
if [ ! -d /mnt/content/$_scripts_dir ]; then
   b_die "scripts directory '$_scripts_dir' does not exist on content volume"
fi

# mount 'log', if connected & logs enabled
if [[ "$_logs_enabled" == "on" ]] ; then
   logs=`get_trm log </etc/hosts`
   if [[ "$logs" != "0.255.255.255" ]] ; then
      umount /mnt/log 2>/dev/null # to allow re-starts
      mkdir -p /mnt/log || exit $?
      for ((i=0; i<18; i=i+1))
      do
         mount /mnt/log
         _ret=$?
         if [ $_ret -eq 0 ]; then
            break
         fi
         sleep 10
      done
      if [ $_ret -ne 0 ]; then
         b_die "Failed to mount log share."
         exit $_ret
      fi
   else
      rmdir /mnt/log 2>/dev/null
      b_die "Logs are enabled but the 'log' terminal is not connected."
   fi
  [[ -n "$_access_log_filename" ]] && ( d_touch "/mnt/log/$_logs_base_dir/$_access_log_filename" || exit $? )
  [[ -n "$_error_log_filename" ]] && ( d_touch "/mnt/log/$_logs_base_dir/$_error_log_filename" || exit $? )
else
   rmdir /mnt/log 2>/dev/null
fi

# -----------------------------------------------------------------------------
# update computed config file
in=`get_trm in </etc/hosts`
perl appl_dyn.pl $in:80 >/etc/httpd/conf.d/appl_dyn.conf || exit $?
# -----------------------------------------------------------------------------

/sbin/ldconfig

