#!/bin/sh 
# $Id: setup-service.sh,v 1.1 2009/11/12 11:08:37 rajis Exp $
INSTOPT=$1
CURRENTDIR=`pwd`
ABSDIRPATH=`dirname $0`


printusage()
{
    echo "Usage: $0 [ argument ]
    install   - to install the VQManager server as a service so that it 
                gets started automatically during system startup.
                Operation can be performed only by SuperUser.

    uninstall - to uninstall a previously installed VQManager service from
                the system and the VQManager server will no longer be 
                started during system startup. Operation can be 
                performed only by SuperUser."
    exit
}



if [ "X$INSTOPT" = "X" ]
then 
printusage
fi

if [ "$ABSDIRPATH" = "." ]
then
	ABSDIRPATH=`pwd`
else
	cd $ABSDIRPATH
	ABSDIRPATH=`pwd`
	cd $CURRENTDIR
fi

INIT_DIR=/etc/init.d
rootuser="false"
APP_NAME="vqmanager-service"

uid=`id -u`
if [ ${uid} = 0 ]
then
  rootuser="true"
else
  rootuser="false"
fi

if [ "${rootuser}" = "false" ]
then
 echo " "
 echo "install/uninstall operations can be performed only by SuperUser"
 echo " "
 exit 1
fi

doinstall()
{
    dir=$ABSDIRPATH

    if [ -f $INIT_DIR/$APP_NAME ]
    then
        echo "VQManager Service already installed !"
    else
        cd /etc/init.d
        ln -sf $dir/$APP_NAME $INIT_DIR/$APP_NAME
        chmod a+x $INIT_DIR/$APP_NAME
        chmod a+x $dir/$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc2.d/S20$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc3.d/S20$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc4.d/S20$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc5.d/S20$APP_NAME
        
        ln -sf $INIT_DIR/$APP_NAME /etc/rc0.d/K20$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc1.d/K20$APP_NAME
        ln -sf $INIT_DIR/$APP_NAME /etc/rc6.d/K20$APP_NAME

        chkconfig --add $APP_NAME
        echo "VQManager Service installed successfully !"
    fi
}

douninstall()
{
    dir=$ABSDIRPATH
    if [ -f $INIT_DIR/$APP_NAME ]
    then
        chkconfig --del $APP_NAME

        rm -f $INIT_DIR/$APP_NAME
        rm -f /etc/rc2.d/S20$APP_NAME
        rm -f /etc/rc3.d/S20$APP_NAME
        rm -f /etc/rc4.d/S20$APP_NAME
        rm -f /etc/rc5.d/S20$APP_NAME

        rm -f /etc/rc0.d/K20$APP_NAME
        rm -f /etc/rc1.d/K20$APP_NAME
        rm -f /etc/rc6.d/K20$APP_NAME
        echo "VQManager Service uninstalled successfully !"
    else
        echo "VQManager Service not installed currently"
    fi
}


if [ "${INSTOPT}" = "uninstall" ]
  then
    douninstall
  else
  if [ "${INSTOPT}" = "install" ]
  then
    doinstall
  else
  printusage
  fi
 fi

