#!/bin/bash
#
# This script will attempt to install SSL Explorer as a Linux 
# service. The method of doing this differs between distributions
# so an attempt is made to guess the O/S

# Display user
usage() {
   echo "usage: $0 [-o <os>] [-j <java-home>] [-n|-u] [-c <wrapper-conf>]"
}

set_wrapper_property() {
  CONFFILE="${WRAPPER_CONF:-$SSLEXPLORER_HOME/conf/wrapper.conf}"
  NAME="$1"
  shift
  VAL="$*"
  FOUND=n
	while read line 
	do
	    case "${line}" in
    	  "${NAME}="*) echo "${NAME}=${VAL}"
                       FOUND=y ;;
	    	    *) echo "${line}" ;;
	    esac
	done < "${CONFFILE}" > /tmp/wrapper.conf
	if [ "${FOUND}" = n ]
        then echo "${NAME}=${VAL}" >> /tmp/wrapper.conf
        fi
	cat /tmp/wrapper.conf > "${CONFFILE}" 2>/dev/null 
	rm -f /tmp/wrapper.conf
}

help() {
   usage 
   echo 
   echo "Option"
   echo "   -o <os>             Force operating system. Can currently"
   echo "                       either be redhat,slackware,smoothwall,suse,debian,conary."
   echo "   -n                  Install the service NOT to start at boot." 
   echo "   -u                  Uninstall service."
   echo "   -c <wrapper-conf>   Location of wrapper configuration."   
}

install_slackware_service() {
   RC_INSTALLED=n
   if [ -f /etc/rc.d/rc.sslexplorer ]
   then line=$(grep "exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer" /etc/rc.d/rc.sslexplorer)
        if [ -n "${line}" ]
        then RC_INSTALLED=y
        fi
   fi
   if [ "${RC_INSTALLED}" = "n" ]
   then echo "#!/bin/bash
#
# Init file for SSL Explorer daemon
#

if [ -f /etc/sslexplorer/service.conf ]
then source /etc/sslexplorer/service.conf
fi

exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/rc.d/rc.sslexplorer
   fi
   chmod a+rx /etc/rc.d/rc.sslexplorer
	 set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
	 set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME}
	 set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux

   if [ -f /etc/rc.d/rc.local ]
   then grep -v "\# Added by SSL Explorer install-service" /etc/rc.d/rc.local > /tmp/rc.local.tmp 
   else echo "#!/bin/bash" > /etc/rc.d/rc.local.tmp
   fi
   echo "if [ -x /etc/rc.d/rc.sslexplorer ]     # Added by SSL Explorer install-service" >> /tmp/rc.local.tmp
   echo "then sh /etc/rc.d/rc.sslexplorer start # Added by SSL Explorer install-service" >> /tmp/rc.local.tmp
   echo "fi                                     # Added by SSL Explorer install-service" >> /tmp/rc.local.tmp
   mv /tmp/rc.local.tmp /etc/rc.d/rc.local   
   if [ "${START_ON_BOOT}" = y ] 
   then chmod a+rx /etc/rc.d/rc.local
   else chmod a-rx /etc/rc.d/rc.local
   fi
}

uninstall_slackware_service() {
    if [ -f /etc/rc.d/rc.local ] 
    then grep -v "\# Added by SSL Explorer install-service" /etc/rc.d/rc.local > /tmp/rc.local.tmp 
         mv /tmp/rc.local.tmp /etc/rc.d/rc.local
    fi
    rm -f /etc/rc.d/rc.sslexplorer
}

install_suse_service() { 
    echo "#!/bin/bash 
# 
### BEGIN INIT INFO 
# Provides: SSL Explorer daemon 
# Required-Start: $syslog $remote_fs $network 
# X-UnitedLinux-Should-Start: $named $syslog $time 
# Required-Stop: $syslog $remote_fs $network 
# X-UnitedLinux-Should-Stop: $named $syslog 
# Default-Start: 3 5 
# Default-Stop: 0 1 2 6 
# Short-Description: SSL Explorer 
# Description: Start SSL Explorer to activate client's VPN access 
#  
# pidfile: /var/lock/subsys/sslexplorer.pid 
# processname: wrapper 
### END INIT INFO 
 
exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/init.d/sslexplorer 
    chmod a+rx /etc/init.d/sslexplorer 
    if [ "${START_ON_BOOT}" = y ]  
    then if ! chkconfig -a sslexplorer 
         then echo "Failed to install service." >&2 
              exit 1 
         fi 
    else if ! chkconfig -d sslexplorer 
         then echo "Failed to uninstall service." >&2 
              exit 1 
         fi 
    fi  
    set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
    set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME} 
    set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux 
    echo "Service installed" 
} 
 
uninstall_suse_service() { 
    if ! chkconfig -d sslexplorer 
    then echo "Failed to uninstall service." >&2 
         exit 1 
    fi 
    rm -f /etc/init.d/sslexplorer 
    echo "Service uninstalled" 
} 


install_debian_service() { 
    mkdir -p /var/lock/subsys
    echo "#!/bin/bash 
# 
 
exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/init.d/sslexplorer 
    chmod a+rx /etc/init.d/sslexplorer 
    if [ "${START_ON_BOOT}" = y ]  
    then if ! update-rc.d sslexplorer defaults
         then echo "Failed to install service." >&2 
              exit 1 
         fi 
    else if ! update-rc.d -f sslexplorer remove
         then echo "Failed to uninstall service." >&2 
              exit 1 
         fi 
    fi  
    set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
    set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME} 
    set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux 
    echo "Service installed" 
} 
 
uninstall_debian_service() { 
    if ! update-rc.d -f sslexplorer remove
    then echo "Failed to uninstall service." >&2 
         exit 1 
    fi 
    rm -f /etc/init.d/sslexplorer 
    echo "Service uninstalled" 
} 
 
install_smoothwall_service() {
# Hack to make Install4J scripts works
   ln -sf /bin/grep /bin/egrep
   mkdir -p /var/lock/subsys
   echo "#!/bin/bash
#
# Init file for SSL Explorer daemon
#

exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/rc.d/rc.sslexplorer
   set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
   set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME}
   set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux
   
   if [ -f /etc/rc.d/rc.sysinit ]
   then grep -v "\# Added by SSL Explorer install-service" /etc/rc.d/rc.sysinit > /tmp/rc.sysinit.tmp 
   else echo "#!/bin/bash" > /etc/rc.d/rc.sysinit.tmp
   fi
   echo "if [ -x /etc/rc.d/rc.sslexplorer ]     # Added by SSL Explorer install-service" >> /tmp/rc.sysinit.tmp
   echo "then sh /etc/rc.d/rc.sslexplorer start # Added by SSL Explorer install-service" >> /tmp/rc.sysinit.tmp
   echo "fi                                     # Added by SSL Explorer install-service" >> /tmp/rc.sysinit.tmp
   mv /tmp/rc.sysinit.tmp /etc/rc.d/rc.sysinit
   chmod a+rx /etc/rc.d/rc.sysinit
   
   if [ "${START_ON_BOOT}" = y ] 
   then chmod a+rx /etc/rc.d/rc.sslexplorer
   else chmod a-rx /etc/rc.d/rc.sslexplorer
   fi
}

uninstall_smoothwall_service() {
    if [ -f /etc/rc.d/rc.sysinit ] 
    then grep -v "\# Added by SSL Explorer install-service" /etc/rc.d/rc.sysinit > /tmp/rc.sysinit.tmp 
         mv /tmp/rc.sysinit.tmp /etc/rc.d/rc.sysinit
    fi
    rm -f /etc/rc.d/rc.sslexplorer
}

install_rh_service() {
   echo "#!/bin/bash
#
# Init file for SSL Explorer daemon
#
# chkconfig: 345 56 26
# description: SSL Explorer daemon
#
# pidfile: /var/lock/subsys/sslexplorer.pid
# processname: wrapper

exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/rc.d/init.d/sslexplorer
    chmod a+rx /etc/rc.d/init.d/sslexplorer
    if [ "${START_ON_BOOT}" = y ] 
    then if ! chkconfig --add sslexplorer --levels 345
         then echo "Failed to install service." >&2
              exit 1
         fi
    else if ! chkconfig --del sslexplorer
         then echo "Failed to uninstall service." >&2
              exit 1
         fi
    fi   
    set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
	set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME}
	set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux
    echo "Service installed"
}

uninstall_rh_service() {
    if ! chkconfig --del sslexplorer
    then echo "Failed to uninstall service." >&2
         exit 1
    fi
    rm -f /etc/rc.d/init.d/sslexplorer
    echo "Service uninstalled"
}

install_conary_service() {
   echo "#!/bin/bash
#
# Init file for SSL Explorer daemon
#
# chkconfig: 345 56 26
# description: SSL Explorer daemon
#
# pidfile: /var/lock/subsys/sslexplorer.pid
# processname: wrapper

exec ${SSLEXPLORER_HOME}/install/platforms/linux/sslexplorer \$*" > /etc/rc.d/init.d/sslexplorer
    chmod a+rx /etc/rc.d/init.d/sslexplorer
    if [ "${START_ON_BOOT}" = y ] 
    then if ! chkconfig --add sslexplorer --levels 345
         then echo "Failed to install service." >&2
              exit 1
         fi
    else if ! chkconfig --del sslexplorer
         then echo "Failed to uninstall service." >&2
              exit 1
         fi
    fi   
    set_wrapper_property "wrapper.java.command" "${JAVA_HOME}/bin/java"
	set_wrapper_property "wrapper.working.dir" ${SSLEXPLORER_HOME}
	set_wrapper_property "wrapper.java.library.path.1" ${SSLEXPLORER_HOME}/install/platforms/linux
    echo "Service installed"
}

uninstall_conary_service() {
    if ! chkconfig --del sslexplorer
    then echo "Failed to uninstall service." >&2
         exit 1
    fi
    rm -f /etc/rc.d/init.d/sslexplorer
    echo "Service uninstalled"
}

# Determine the installed location of SSL Explorer
export DIR=$(dirname ${0})
SSLEXPLORER_HOME=$(cd ${DIR}; cd ../../.. ; pwd)

# Initialise other variables
OS=
START_ON_BOOT=y
UNINSTALL=n

# Determine the O/S

if [ -f /etc/SuSE-release ]
then OS=suse
elif [ -f /etc/redhat-release ]
then OS=redhat
elif [ -f /etc/slackware-version ]
then OS=slackware
elif [ -f /usr/local/bin/smoothiedeath ]
then OS=smoothwall
elif [ -f /etc/debian_version ] 
then OS=debian
elif [ -d /etc/conary ] 
then OS=conary
fi  
   
# Parse the command line
while [ $# -gt 0 ]
do
   case "$1" in
       "-j") JAVA_HOME="$2" ; shift ;;
       "-o") OS="$2" l shift ; ;;
       "-u") UNINSTALL="y" ;;
       "-n") START_ON_BOOT="n" ;;
       "-c") WRAPPER_CONF="$2" ; shift ;;
       "-h") help
             exit 0 ;;
          *) help 
             exit 1 ;;
   esac
   shift
done

if [ "${UNINSTALL}" = "n" ]
then echo "Detecting Java"
     if [ -n "${JAVA_HOME}" ]
     then if [ ! -d "${JAVA_HOME}" -o ! -f "${JAVA_HOME}/bin/java" ]
          then echo "   JAVA_HOME points to a missing or invalid Java runtime,"
               echo "   please set JAVA_HOME environment variable or provide"
               echo "   -j <java-home> argument."
               exit 1
          fi
     else echo "    JAVA_HOME has not been set. Please set JAVA_HOME environment"
          echo "    variable or provide -j <java-home> argument."
          exit 1
     fi
     echo "     Using ${JAVA_HOME}"
fi


echo "Detect OS ${OS}"

#
case "${OS}" in
      suse) if [ "${UNINSTALL}" = y ]
            then uninstall_suse_service
            else install_suse_service 
            fi ;; 
    redhat) if [ "${UNINSTALL}" = y ]
            then uninstall_rh_service
            else install_rh_service 
            fi ;; 
 slackware) if [ "${UNINSTALL}" = y ]
            then uninstall_slackware_service
            else install_slackware_service 
            fi ;;
    debian) if [ "${UNINSTALL}" = y ]
            then uninstall_debian_service
            else install_debian_service 
            fi ;;  
smoothwall) if [ "${UNINSTALL}" = y ]
            then uninstall_smoothwall_service
            else install_smoothwall_service 
            fi ;; 
    conary) if [ "${UNINSTALL}" = y ]
            then uninstall_conary_service
            else install_conary_service 
            fi ;; 
         *) echo "OS could not be detected and no valid one specified as an argument." >&2
            help ;;
esac

# Clear out the temporary directory
if [ -f "${WRAPPER_CONF}" ]
then temp=$(grep "=--temp=" "${WRAPPER_CONF}"|awk -F= '{ print $3 }')
     if [ -z "${temp}" ]
     then temp="${SSLEXPLORER_HOME}/tmp"
     fi
     if [ -d "${temp}" ]
     then rm -fr "${temp}/*"
     fi
fi