#! /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-2009 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.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

# $Id: post-actions.in,v 1.29 2009/06/15 00:33:34 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

EOU
	# not fully implemented yet
         #[ -W ]                 define what the WebAliase is set to
         #[ -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
    exit 1
    }

    eval set -- "$my_opts"

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

    # carry across values from install
    # they will eventually be allowed to be overriden by runtime options
    prefix="/usr/local/linuxcoe-sd"
    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
	    ;;
        -W) webalias=$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"
  }

  # sanity checks for missing binaries
  # (since could be on different system than build performed on)
  for name in \
	TAR \
        CPIO \
        GZIP \
        GUNZIP \
        DIFF \
        FIND \
        MKISOFS \
        GENISOIMAGE \
        PERL \
        PERLMOD \
        PALO \
        SENDMAIL \
	SUDO
    do
      my_cf="/usr/local/linuxcoe-sd/includes/binaries.pm"
      if type tr >/dev/null 2>&1; then
        [ "$name" = "GENISOIMAGE" ] && my_name=MKISOFS || my_name=$name
        my_name_path=$(grep ${my_name} ${my_cf} | tail -1 | awk -F'=' '{print $NF}')
        cmd=$(echo ${name} | tr "[:upper:]" "[:lower:]")
        if type ${cmd} >/dev/null 2>&1 ; then
          my_cmd_path=$(type ${cmd} 2>&1 | awk '{print $NF}')
          if [ "${my_name_path}" = "\"${my_cmd_path}\";" ]; then
            continue
          else
            if [ "${my_name_path}" = "\"SKIP\";" ]; then
              # hopefully substitute a better guess
              sed -i "s:^\$${my_name}=\"SKIP\";:\$${my_name}=\"${my_cmd_path}\";:" ${my_cf}
            fi
          fi
        fi
      else
        if grep SKIP ${my_cf} >/dev/null 2>&1; then
          test -n "$VERBOSE" && echo "  please adjust the SKIP entries found in ${my_cf}"
          test -n "$VERBOSE" && echo "  to match paths to executables on your local system !"
        fi
      fi
    done

  # valid html snippet for navigation (until doc overlay arrives at least)
  my_sh="/usr/local/linuxcoe-sd/html/themes/default/header5.html"
  if test -n "$INSTALL"; then
    if test -e $my_sh; then
      continue
    else
      test -n "$VERBOSE" && echo "  creating stub html : $my_sh"
      {
      cat << EOF
              </tbody>
            </table>

  </td><!-- End Left Navigation -->

  <!-- Start Content Area "stub" -->
  <td>

<!-- End header5.html content -->
EOF
    } > $my_sh
    fi
  else
    test -n "$UNINSTALL" && {
      test -n "$VERBOSE" && echo "  removing stub html : $my_sh"
      [ -f "$my_sh" ] && rm -f $my_sh
    }
  fi

  # manage global configuration link
  my_sh="/usr/local/linuxcoe-sd/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"
      [ -L "$my_sh" ] && rm -f $my_sh
    }
  fi

  # manage logrotate configuration link
    my_sh="LinuxCOE-SystemDesigner"
    my_dir="/etc/logrotate.d"
    if test -n "$INSTALL" ; then
      test -n "$VERBOSE" && echo "  creating link : ${my_dir}/$my_sh"
      [ -d "${my_dir}" ] &&
        ln -sf ${prefix}/etc/includes/$my_sh ${my_dir}/$my_sh
    else
      test -n "$UNINSTALL" && {
        test -n "$VERBOSE" && echo "  removing link : ${my_dir}/$my_sh"
        [ -L "${my_dir}/$my_sh" ] && rm -f ${my_dir}/$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 \
	${prefix}/var/cache/linuxcoe-sd \
	${prefix}/var/cache/linuxcoe-sd/${webworkdir} \
	${prefix}/var/cache/linuxcoe-sd/${webworkdir}/${webalias} \
	${prefix}/var/lib/linuxcoe-sd \
	${prefix}/var/lib/linuxcoe-sd/profiles \
	${prefix}/var/log/linuxcoe-sd ; do
      chmod 2775 ${DESTDIR}/$d
      if test -n "$httpd_user" -a -n "$httpd_group" ; then
        chown $httpd_user:$httpd_group ${DESTDIR}/$d
      fi
    done
  fi

  # manage configuration content links
    my_sh="${prefix}/etc"
    if test -n "$INSTALL" ; then
      if test -d /usr/local/linuxcoe-sd/etc || test -L /usr/local/linuxcoe-sd/etc; then
        continue
      else
        test -n "$VERBOSE" && echo "  creating link : $my_sh"
        ln -s ${prefix}/etc /usr/local/linuxcoe-sd/etc
      fi
    else
      test -n "$UNINSTALL" && {
        [ -L "/usr/local/linuxcoe-sd/etc" ] && {
          test -n "$VERBOSE" && echo "  removing link : $my_sh"
          rm -f /usr/local/linuxcoe-sd/etc
        }
      }
    fi

  # manage dynamic content links
    for my_sh in cache lib log;
      do
        if test -n "$INSTALL" ; then
          if test -d /usr/local/linuxcoe-sd/var/$my_sh || test -L /usr/local/linuxcoe-sd/var/$my_sh; then
            continue
          else
            [ -d  "/usr/local/linuxcoe-sd/var" ] || mkdir -p /usr/local/linuxcoe-sd/var
            test -n "$VERBOSE" && echo "  creating link : /usr/local/linuxcoe-sd/var/$my_sh"
            ln -s ${prefix}/var/$my_sh/linuxcoe-sd/ /usr/local/linuxcoe-sd/var/$my_sh
          fi
        else
          test -n "$UNINSTALL" && {
            [ -L "/usr/local/linuxcoe-sd/var/$my_sh" ] && {
              test -n "$VERBOSE" && echo "  removing link : /usr/local/linuxcoe-sd/var/$my_sh"
              rm -f /usr/local/linuxcoe-sd/var/$my_sh
            }
          }
        fi
      done
      test -n "$UNINSTALL" && {
        [ -d  "/usr/local/linuxcoe-sd/var" ] && rmdir /usr/local/linuxcoe-sd/var
      }

##
# Apache integration tasks
##

  if test "x${APACHE}" = "xSKIP"; then

    test -n "$VERBOSE" && {
      echo "NOTE : The auto-integratation with the Apache HTTP server"
      echo "NOTE : was skipped, so please refer to the README.last file"
      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"
    }

    if test "x${APACHE}" = "xDOIT"; then

      # download and work area directories
        for d in \
            ${prefix}/var/cache/linuxcoe-sd \
            ${prefix}/var/lib/linuxcoe-sd \
            ${prefix}/var/log/linuxcoe-sd
          do
            if test -n "$INSTALL" ; then
              continue
            else
              test -n "$UNINSTALL" && {
                test -n "$VERBOSE" && echo "  removing directory : $d"
                [ -d "$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"
            [ -L "${httpdcfgdir}/$my_sh" ] && rm -f ${httpdcfgdir}/$my_sh
          }
        fi

      # restart apache, for either install/uninstall cases
        test -n "$VERBOSE" && {
          echo "NOTE : Apache web server should now be restarted"
          echo "NOTE : for this modified configuration to take effect."
        }
        if test "x${APACHE2CTL}" = "xSKIP" -o "x${APACHE2CTL}" = "xNONE"; then
          if type apache2ctl >/dev/null 2>&1; then
            apache2ctl restart
          fi
        else
          ${APACHE2CTL} restart 
        fi
        if test "x${APACHECTL}" = "xSKIP" -o "x${APACHECTL}" = "xNONE"; then
          if type apachectl >/dev/null 2>&1; then
            apachectl restart
          fi
        else
          ${APACHECTL} restart 
        fi
    fi
  fi
 
##
# sudo integration tasks
##

  if test "x${SUDOERS}" = "xSKIP"; then

    test -n "$VERBOSE" && {
      echo "NOTE : The auto-integratation with ${sudoers_cfg}"
      echo "NOTE : was skipped, so please refer to the README.last file"
      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 "x${SUDOERS}" = "xDOIT"; then

      if type sudo >/dev/null 2>&1; then
        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" && {
            sed -i -e"s/^${httpd_user}/#${httpd_user}/" ${sudoers_cfg}
          }
        else
          echo " cannot find ${sudoers_cfg} to augment !"
        fi

      else
        echo "NOTE : cannot find a working sudo environment !"
      fi

    fi

  fi

exit 0

