#!/usr/bin/ksh
#
# make script for mysql
#
# History
#   17.01.2006 v1.0.0 /bs - initial release
#

# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END

# --------------------------------------
# constants

typeset -r TRUE=0
typeset -r FALSE=1
typeset -r THISSCRIPT=$( basename $0 )

# --------------------------------------
# variables

#LOGFILE=""
VERBOSE=${FALSE}

# --------------------------------------
# subroutines

logMsg() {
  typeset THISMSG="$*"
  echo "${THISMSG}"
  [ "${THISMSG}"x != ""x -a  "${LOGFILE}"x != ""x  ] && echo "${THISMSG}" >>"${LOGFILE}"
}

logError() {
  logMsg  "ERROR: $*" >&2
}

logWarning() {
  logMsg "WARNING: $*"
}

logInfo() {
  [[ ${VERBOSE} = ${TRUE} ]] && logMsg "INFO: $*"
}

cleanup() {
  logInfo "Housekeeping is running ..."
}

die() {
  typeset THISRC=$1
  shift
  typeset THISMSG="$*"
  if [[ -n ${THISMSG} ]] ; then
    [ ${THISRC} = 0 ] && logMsg "${THISMSG}" || logError "${THISMSG}"
  fi
  cleanup
  exit ${THISRC}
} 

trapRoutine() {
  die 255 "${THISCRIPT} aborted by an external signal"
  
}

# --------------------------------------

# 
# usage: get_service_status service_name
#
# returns:
#   0 - current status is written to STDOUT
#   1 - service not found
# 
get_service_status() {
  svcs -H -o STATE "$1" 2>/dev/null
}


#
# Usage: enable_serivce service_name
#
# returns:
#   0 - current status is written to STDOUT
#   1 - service not found
# 
enable_service() {
  svcadm enable "$1"
}

#
# Usage: enable_serivce service_name
#
# returns:
#   0 - current status is written to STDOUT
#   1 - service not found
# 
# 
disable_service() {
  svcadm disable "$1"
  get_service_status
}



# --------------------------------------

usage() {
  logMsg "Usage: ${THISSCRIPT} [install|deinstall|preinstall|postinstall|preremove|postremove] [configdir]  "
  die 1
}

init_variables() {
  logMsg "Initializing variables ..."
  MAIN_RC=0

  MY_SCRIPTDIR="/etc/my_smf_scripts"
  SERVICE_NAME="svc:/application/mysql:default"
  
  STARTSTOP_SCRIPT="mysql.server"
  MANIFEST="mysql_manifest.xml"
  
  [ ! -d "${MY_SCRIPTDIR}" ] && die 4 "Directory \"${MY_SCRIPTDIR}\" does not exist"
  [ ! -f "${CONFIG_DIR}/${STARTSTOP_SCRIPT}" ] && die 5 "Start/Stop Script \"${CONFIG_DIR}/${STARTSTOP_SCRIPT}\" does not exist"
  [ ! -f "${CONFIG_DIR}/${MANIFEST}" ] && die 6 "Manifest \"${CONFIG_DIR}/${MANIFEST}\" does not exist"
}

  
preinstall() {
  die 2 "PreInstall not implemented yet"
}

install() {
  if [ -f "${MY_SCRIPTDIR}/${STARTSTOP_SCRIPT}" ] ; then
    logMsg "The start/stop script \"${MY_SCRIPTDIR}/${STARTSTOP_SCRIPT}\" already exists"
  else
    logMsg "Copying start/stop script ..."
    cp "${CONFIG_DIR}/${STARTSTOP_SCRIPT}" "${MY_SCRIPTDIR}" || die 7 "Error copying the start/stop script"
  fi

  CUR_STATUS=$( get_service_status "${SERVICE_NAME}" )
  if [ $? -eq 0 ] ; then
    logMsg "The manifest for \"${SERVICE_NAME}\" is already imported."
  else    
    logMsg "Importing the manifest for \"${SERVICE_NAME}\"  ..."
    svccfg  -v import "${CONFIG_DIR}/${MANIFEST}" || die 8 "Error importing the manifest"
  fi
  
  if [ "${CUR_STATUS}"x = "online"x ] ; then
    logMsg "The service \"${SERVICE_NAME}\" is already started."
  else
    logMsg "Starting the service \"${SERVICE_NAME}\"..."
    svcadm enable "${SERVICE_NAME}"
  fi

  CUR_STATUS=$( get_service_status "${SERVICE_NAME}" )
  [ "${CUR_STATUS}"x != "online"x ] && die 9 "Could not start the service \"${SERVICE_NAME}\""

    
}

postinstall() {
  die 2 "PostInstall not implemented yet"
}


preremove() {
  die 2 "Preremove not implemented yet"
}


uninstall() {
  die 2 "Uninstalling not implemented yet"
}


postremove() {
  die 2 "Postremove not implemented yet"
}

# --------------------------------------
# main routine

trap trapRoutine 1 2 3 6

if [ -n "${LOGFILE}" ] ; then
  touch "${LOGFILE}" 2>/dev/null
  if [ $? -ne 0 ] ; then
    logError "Can not write to the log file \""${LOGFILE}"\"."
    logError "Using of the logfile is disabled now"
    LOGFILE=""
  fi
fi


CONFIG_DIR="$( pwd )"
[ "$2"x != ""x ] && CONFIG_DIR="$2"


case $1 in

  "preinstall" ) 
     init_variables
     preinstall
     ;;

  "install" ) 
     init_variables
     install
     ;;

  "postinstall" ) 
     init_variables
     postinstall
     ;;


  "preremove" ) 
     init_variables
     preremove
     ;;

  "deinstall" | "uninstall" | "remove" ) 
     init_variables
     uninstall
     ;;

  "postremove" ) 
     init_variables
     postremove
     ;;


  * ) 
     usage
     ;;

esac

die ${MAIN_RC} 
 
