#! /bin/sh

##
# File:         post-actions
# Description:  Handle those actions of post install or remove
#               for use by tarball or packaged installations
# Author:       Bryan Gartner   < bryan.gartner@hp.com >
##

# © Copyright 2000-2007 Hewlett-Packard Development Company, L.P

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id: post-actions.in,v 1.9 2007/09/10 04:28:07 bryang Exp $

# Usage

  # check invocation
    my_opts=$(getopt iuvD:U:G:C:S: "$@") || {
    cat <<- EOU

  Usage: $0
         [ -i ]                 perform install types of tasks
         [ -u ]                 perform uninstall types of tasks
         [ -v ]                 verbosely list actions
         [ -D ]                 define where the DocumentRoot is located
         [ -U ]                 define what the Apache user is set to
         [ -G ]                 define what the Apache group is set to
         [ -C ]                 define where the Apache configuration directory is located
         [ -S ]                 define where the sudoers configuration file is located

EOU
    exit 1
    }

    eval set -- "$my_opts"

    INSTALL=""
    UNINSTALL=""
    VERBOSE=""

    # carry across values from install
    # they will eventually get overriden by runtime options
    prefix="/usr/local/systemdesigner/4"
    test -f "${prefix}/etc/includes/config.state" && . ${prefix}/etc/includes/config.state


    while test "$1" != "--"; do
      case "$1" in
        -i) INSTALL=1
            shift
            ;;
        -u) UNINSTALL=1
            shift
            ;;
        -v) VERBOSE=1
            shift
            ;;
        -D) docrootdir=$2
	    shift 2
	    ;;
        -U) httpd_user=$2
	    shift 2
	    ;;
        -G) httpd_group=$2
	    shift 2
	    ;;
        -C) httpdcfgdir=$2
	    shift 2
	    ;;
        -S) sudoers_cfg=$2
	    shift 2
	    ;;
        --) shift
            ;;
        esac
    done

    # get rid of --
    shift

##
# General actions
##

  test -n "$VERBOSE" && {
    test -n "$INSTALL" && echo "LinuxCOE SystemDesigner integration tasks"
    test -n "$UNINSTALL" && echo "LinuxCOE SystemDesigner removal tasks"
  }

  # manage global configuration link
  my_sh="/usr/local/systemdesigner/4/linuxcoe.rc"
  if test -n "$INSTALL" ; then
    test -n "$VERBOSE" && echo "  creating link : $my_sh"
    ln -sf ${prefix}/etc/linuxcoe.rc $my_sh
  else
    test -n "$UNINSTALL" && {
      test -n "$VERBOSE" && echo "  removing link : $my_sh"
      rm -f $my_sh
    }
  fi

  # manage permissions on writable directories
  # (done here instead of Makefile, so that packages can be done w/o uid=0)
  if test -n "$INSTALL" ; then
    for d in \
	/var/cache/systemdesigner \
	/var/lib/systemdesigner \
	/var/lib/systemdesigner/profiles \
	/var/log/systemdesigner ; do
      chmod 2775 $d
      if test -n "$httpd_user" -a -n "$httpd_group" ; then
        chown $httpd_user:$httpd_group $d
      fi
    done
  fi

  # manage dynamic content links
    my_sh="${prefix}/var/logs"
    if test -n "$INSTALL" ; then
      test -n "$VERBOSE" && echo "  creating link : $my_sh"
      ln -sf /var/log/systemdesigner $my_sh
    else
      test -n "$UNINSTALL" && {
        test -n "$VERBOSE" && echo "  removing link : $my_sh"
        rm -f $my_sh
      }
    fi

    my_sh="${prefix}/var/profiles"
    if test -n "$INSTALL" ; then
      test -n "$VERBOSE" && echo "  creating link : $my_sh"
      ln -sf /var/lib/systemdesigner/profiles $my_sh
    else
      test -n "$UNINSTALL" && {
        test -n "$VERBOSE" && echo "  removing link : $my_sh"
        rm -f $my_sh
      }
    fi

##
# Apache integration tasks
##

  if test "x${APACHECTL}" = "xNONE" && test "x${APACHE2CTL}" = "xNONE"; then
    test -n "$VERBOSE" && {
      echo "NOTE : The auto-integratation with the Apache HTTP server"
      echo "NOTE : was skipped, so please refer to the documentation in"
      echo "NOTE :     /usr/local/systemdesigner/4/html/docs"
      echo "NOTE : to manually complete these tasks !"
    }
  else

    test -n "$VERBOSE" && {
      test -n "$INSTALL" && echo "Apache-specific integration tasks"
      test -n "$UNINSTALL" && echo "Apache-specific removal tasks"
    }

    # download and work area directories
      for d in \
          ${docrootdir}/scratch_monkey \
          ${docrootdir}/scratch_monkey/systemdesigner
        do
          if test -n "$INSTALL" ; then
            test -n "$VERBOSE" && echo "  creating directory : $d"
              mkdir -p $d
              chmod 2775 $d
              chown ${httpd_user}:${httpd_group} $d
          else
            test -n "$UNINSTALL" && {
              test -n "$VERBOSE" && echo "  removing directory : $d"
              rm -rf $d
            }
          fi
       done

    # apache "include" configuration
      my_sh="LinuxCOE-SystemDesigner.conf"
      if test -n "$INSTALL" ; then
        test -n "$VERBOSE" && echo "  creating link : ${httpdcfgdir}/$my_sh"
        ln -sf ${prefix}/etc/includes/$my_sh ${httpdcfgdir}/$my_sh
      else
        test -n "$UNINSTALL" && {
          test -n "$VERBOSE" && echo "  removing link : ${httpdcfgdir}/$my_sh"
          rm -f ${httpdcfgdir}/$my_sh
        }
      fi

    # restart apache, for either install/uninstall cases, trying
    # to pick the relevant one based on ${httpdcfgdir} for apache version
      test -n "$VERBOSE" && {
        echo "NOTE : Apache web server should now be restarted"
        echo "NOTE : for this modified configuration to take effect."
      }
      # can't seem to get this to work across all test cases :(
      #if echo ${httpdcfgdir} | grep 2 >/dev/null 2>&1; then
        #test "x${APACHE2CTL}" = "xNONE" || ${APACHE2CTL} restart 
      #else
        #test "x${APACHECTL}" = "xNONE" || ${APACHECTL} restart
      #fi

  fi
 
##
# sudo integration tasks
##

  if test "x${SUDO}" = "xNONE"; then
    test -n "$VERBOSE" && {
      echo "NOTE : The auto-integratation with ${sudoers_cfg}"
      echo "NOTE : was skipped, so please refer to the documentation in"
      echo "NOTE :     /usr/local/systemdesigner/4/html/docs"
      echo "NOTE : to manually complete these tasks !"
    }
  else

    test -n "$VERBOSE" && {
      test -n "$INSTALL" && echo "sudo-specific integration tasks"
      test -n "$UNINSTALL" && echo "sudo-specific removal tasks"
    }

    my_sh="sudoers"
    if test -e ${sudoers_cfg}; then
      if grep "^${httpd_user}" ${sudoers_cfg} >/dev/null 2>&1; then
        continue
      else
        test -n "$INSTALL" && {
          test -n "$VERBOSE" && echo "  augmenting ${sudoers_cfg}"
          cat ${prefix}/etc/includes/$my_sh >> ${sudoers_cfg}
        }
      fi
      test -n "$UNINSTALL" && {
        test -n "$VERBOSE" && echo "  should un-augment ${sudoers_cfg}, but not implemented yet"
      }
    fi
  fi

exit 0

