#!/bin/sh
#
# chkconfig: 345 20 80
# description: Starts and stops CAXOSoft ARCserveRHA replication software
#
# Copyright (c) 2011 CA, Inc.
# All rights reserved.
#
### BEGIN INIT INFO
# Provides:          CA, Inc.
# Required-Start:    
# Required-Stop:     
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Description:        Starts and stops CAXOSoft ARCserveRHA replication software
### END INIT INFO

REPL_HOME=/opt/CA/ARCserveRHA

# XOUSER_PATTERN should be changed to real
# user name during installation
#user=XOUSER_PATTERN
oracle_home=XOORA_HOME
oclient_home=XOOC_HOME
lc_all=XO_LC_ALL
oracle_base=XOORA_BASE
oracle_owner=XOORA_OWNER

user=root

export user

drv_name=""
os_vendor=""
os_ver=""
os_cpu=""
mod_params=""



set_os_vendor()
{
    if [ -f /etc/redhat-release ] ; then
        os_vendor="REDHAT"
    fi

    if [ -f /etc/SuSE-release ] ; then
        os_vendor="SUSE"
    fi

    if [ "$os_vendor" = "REDHAT" ] ; then

        TMP=`cat /etc/redhat-release | grep "Red Hat Enterprise Linux" | grep "release 4"  > /dev/null && echo "1"` 
        if [ "$TMP" =  "1" ] ; then
             os_name="rhel4"
        fi

        TMP=`cat /etc/redhat-release |grep "Red Hat Enterprise Linux Server release 5" > /dev/null && echo "1"`
        if [ "$TMP" = "1" ] ; then
             os_name="rhel5"
        fi

        TMP=`cat /etc/redhat-release |grep "Red Hat Enterprise Linux Server release 6" > /dev/null && echo "1"`
        if [ "$TMP" = "1" ] ; then
             os_name="rhel6"
        fi
		
		# CentOS
		TMP=`cat /etc/redhat-release | grep "CentOS"| grep "release 4" >/dev/null && echo "1"`
		if [ "$TMP" == "1" ] ;
		then 
			os_name=rhel4 
		fi 

		TMP=`cat /etc/redhat-release | grep "CentOS"| grep "release 5" >/dev/null && echo "1"`
		if [ "$TMP" == 1 ] ;
		then
			os_name=rhel5
		fi
		
		TMP=`cat /etc/redhat-release | grep "CentOS Linux"| grep "release 6" >/dev/null && echo "1"`
		if [ "$TMP" == 1 ] ;
		then
			os_name=rhel6
		fi

    fi

    if [ "$os_vendor" = "SUSE" ] ; then
        TMP=`cat /etc/SuSE-release | grep "SUSE LINUX Enterprise Server 9"  > /dev/null && echo "1"` 
        if [ "$TMP" = "1" ] ; then
            os_name="sles9"
        fi

        TMP=`cat /etc/SuSE-release | grep "SUSE Linux Enterprise Server 10"  > /dev/null && echo "1"` 
        if [ "$TMP" =  "1" ] ; then
             os_name="sles10"
        fi

        TMP=`cat /etc/SuSE-release | grep "SUSE Linux Enterprise Server 11"  > /dev/null && echo "1"` 
        if [ "$TMP" =  "1" ] ; then
             os_name="sles11"
        fi
    fi
 
}

set_os_ver()
{
    os_ver=`uname -r | cut -d'.' -f 1,2`
    os_cpu=`uname -i`
}


echo_error()
{
	echo
        echo "ERROR:"
        echo
	echo "Can not load XOFS module: ARCserveRHA can not find $1 entry in ${sysmap_file} file."
        echo "Please contact support!"
        echo
        echo
        exit -1;
}

set_mod_params()
{
    if [ "2.6" = "$os_ver" ] ; then
        sysmap_file=/boot/System.map-`uname -r`

        if [ ! -f ${sysmap_file} ] ; then
             echo
             echo "ERROR:"
             echo
             echo "Can not load XOFS module: ARCserveRHA can not find ${sysmap_file} file."
             echo "Please contact support!"
             echo
             echo
             exit -1;
       fi

       sys_call_table_adrr=`cat ${sysmap_file} | grep -w sys_call_table | cut -d' ' -f 1`
       if [ "" = "${sys_call_table_adrr}" ] ; then
            echo
            echo "ERROR:"
            echo
            echo "Can not load XOFS module: ARCserveRHA can not find sys_call_table entry in ${sysmap_file} file."
            echo "Please contact support!"
            echo
            echo
            exit -1;
       fi

       files_lock_addr=`cat ${sysmap_file} | grep -w files_lock | cut -d' ' -f 1`
       if [ "" = "${files_lock_addr}" ] ; then
            echo
            echo "ERROR:"
            echo
            echo "Can not load XOFS module: ARCserveRHA can not find files_lock entry in ${sysmap_file} file."
            echo "Please contact support!"
            echo
            echo
            exit -1;
       fi

       get_empty_filp=`cat ${sysmap_file} | grep -w get_empty_filp | cut -d' ' -f 1`
       if [ "" = "${get_empty_filp}" ] ; then
            echo
            echo "ERROR:"
            echo
            echo "Can not load XOFS module: ARCserveRHA can not find files_lock entry in ${sysmap_file} file."
            echo "Please contact support!"
            echo
            echo
            exit -1;
       fi

       publish_ver=`uname -r | cut -d'.' -f 3 | cut -d'-' -f 1`
       if [ $publish_ver -lt 27 ] ; then
	        invalidate_mapping_pages=`cat ${sysmap_file} | grep -w invalidate_mapping_pages | cut -d' ' -f 1`
            if [ "" = "${invalidate_mapping_pages}" ] ; then
                echo
                echo "ERROR:"
                echo
                echo "Can not load XOFS module: ARCserveRHA can not find invalidate_mapping_pages entry in ${sysmap_file} file."
                echo "Please contact support!"
                echo
                echo
                exit -1;
            fi
       fi
	
       symbol_get_func_addr=`cat ${sysmap_file} | grep -w __symbol_get | cut -d' ' -f 1`
       if [ "" = "${symbol_get_func_addr}" ] ; then
            echo
            echo "ERROR:"
            echo
            echo "Can not load XOFS module: ARCserveRHA can not find __symbol_get entry in ${sysmap_file} file."
            echo "Please contact support!"
            echo
            echo
            exit -1;
       fi

       file_lock_list_addr=`cat ${sysmap_file} | grep -w file_lock_list | cut -d' ' -f 1`
       if [ "" = "${file_lock_list_addr}" ] ; then
            echo
            echo "ERROR:"
            echo
            echo "Can not load XOFS module: ARCserveRHA can not find file_lock_list entry in ${sysmap_file} file."
            echo "Please contact support!"
            echo
            echo
            exit -1;
       fi

       if [ "rhel5" = "${os_name}" -a "x86_64" = "${os_cpu}" ] ; then		
            phys_base_addr=`cat ${sysmap_file} | grep -w phys_base | cut -d' ' -f 1`		
       fi
	
       if [ "sles11" = "${os_name}" -a "x86_64" = "${os_cpu}" ] ; then		
            phys_base_addr=`cat ${sysmap_file} | grep -w phys_base | cut -d' ' -f 1`		
       fi

       if [ "x86_64" = "${os_cpu}" ] ; then
            ia32_sct_addr=`cat ${sysmap_file} | grep -w ia32_sys_call_table | cut -d' ' -f 1`
            if [ "" = "${ia32_sct_addr}" ]; then
                echo
                echo "ERROR:"
                echo
                echo "Can not load XOFS module: ARCserveRHA can not find ia32_sys_call_table entry in ${sysmap_file} file."
                echo "Please contact support!"
                echo
                echo
                exit -1;
            fi
       fi

       if [ "sles11" = "${os_name}" ] ; then
		next_mnt_func_addr=""
       elif [ "rhel6" = "${os_name}" ] ; then
		next_mnt_func_addr=""
       else
       		next_mnt_func_addr=`cat ${sysmap_file} | grep -w next_mnt | cut -d' ' -f 1`
      		if [ "" = "${next_mnt_func_addr}" ] ; then
			echo_error "next_mnt"
       		fi
       fi	

       set_fs_root_func_addr=`cat ${sysmap_file} | grep -w  set_fs_root| cut -d' ' -f 1`
       if [ "" = "${set_fs_root_func_addr}" ] ; then
                echo_error "set_fs_root"
       fi
		
       set_fs_pwd_func_addr=`cat ${sysmap_file} | grep -w  set_fs_pwd| cut -d' ' -f 1`
       if [ "" = "${set_fs_pwd_func_addr}" ] ; then
                echo_error "set_fs_pwd"
       fi
	
       mod_params=" sct_addr=${sys_call_table_adrr} fsl_addr=${files_lock_addr} symbol_get_addr=${symbol_get_func_addr} fll_addr=${file_lock_list_addr} 
		    next_mnt_addr=${next_mnt_func_addr} set_fs_root_addr=${set_fs_root_func_addr} set_fs_pwd_addr=${set_fs_pwd_func_addr}"
       
       if [ "rhel5" = "${os_name}" -a "x86_64" = "${os_cpu}"  -a "" != "${phys_base_addr}" ] ; then	 	
            mod_params="${mod_params} phys_base_addr=${phys_base_addr}"  	
       fi
       
       if [ "rhel6" = "${os_name}" -a "" != "${get_empty_filp}" ] ; then	 	
       
       	     tasklist_lock_addr=`cat ${sysmap_file} | grep -w tasklist_lock | cut -d' ' -f 1`

	     find_task_by_vpid_addr=`cat ${sysmap_file} | grep -w  find_task_by_vpid | cut -d' ' -f 1`
     	     set_pages_rw_force_addr=`cat ${sysmap_file} | grep -w  set_pages_rw | cut -d' ' -f 1`
             
	     mod_params="${mod_params} find_task_by_vpid_addr=${find_task_by_vpid_addr}"
	     mod_params="${mod_params} get_empty_filp_addr=${get_empty_filp}"  	
             mod_params="${mod_params} tll_addr=${tasklist_lock_addr} sprw_addr=${set_pages_rw_force_addr}"
       fi

	   if [ "" != "${invalidate_mapping_pages}" ] ; then	
	     	mod_params="${mod_params} invalidate_mapping_pages_addr=${invalidate_mapping_pages}"  	
	   fi
	
       if [ "sles11" = "${os_name}" -a "x86_64" = "${os_cpu}"  -a "" != "${phys_base_addr}" ] ; then	 	
            mod_params="${mod_params} phys_base_addr=${phys_base_addr}"  	
       fi

       if [ "sles11" = "${os_name}" ] ; then
       		tasklist_lock_addr=`cat ${sysmap_file} | grep -w tasklist_lock | cut -d' ' -f 1`
                if [ "" = "${tasklist_lock_addr}" ] ; then
            		echo_error "tasklist_lock"
       		fi

		set_pages_rw_force_addr=`cat ${sysmap_file} | grep -w  set_pages_rw_force | cut -d' ' -f 1`
                if [ "" = "${set_pages_rw_force_addr}" ] ; then
                        echo_error "set_pages_rw_force"
                fi
                mod_params="${mod_params} tll_addr=${tasklist_lock_addr} sprw_addr=${set_pages_rw_force_addr}"

		publish_ver=`uname -r | cut -d'.' -f 3 | cut -d'-' -f 1`
            	if [ $publish_ver -ge 32 ]; then
			find_task_by_vpid_addr=`cat ${sysmap_file} | grep -w  find_task_by_vpid | cut -d' ' -f 1`
                	if [ "" = "${find_task_by_vpid_addr}" ] ; then
                        	echo_error "find_task_by_vpid"
                	fi
                	mod_params="${mod_params} find_task_by_vpid_addr=${find_task_by_vpid_addr}"
		fi
	fi

       if [ "x86_64" = "${os_cpu}" ] ; then
            mod_params="${mod_params} ia32_sct_addr=${ia32_sct_addr}"
       fi

   fi
}

set_driver_name()
{
	
	if [ "$os_name" = "sles11" ]; then
	    #publish_ver point 2.6.27.xxx or 2.6.32.xxx
	    publish_ver=`uname -r | cut -d'.' -f 3 | cut -d'-' -f 1`
	    major_ver=`uname -r | cut -d'.' -f 4 | cut -d'-' -f 1`  		
	    flavour=`uname -r| awk -F'-' '{print $NF}'`
	    if [ $publish_ver -eq 27 ]; then
                ker_ver=2.6.27.19-5  #sles11-gm
	    elif [ $publish_ver -eq 32 ]; then
		ker_ver=2.6.32.12-0.7 #sles11-sp1
            else
                echo "Error: This version is not officially supported."
            fi
	
	fi

	if [ "$os_name" = "sles10" ]; then
	    major_ver=`uname -r | cut -d'.' -f 4 | cut -d'-' -f 1`  		
	    flavour=`uname -r| awk -F'-' '{print $NF}'`
	    
	    if [ $major_ver -lt 46 ]; then
		ker_ver=2.6.16.21-0.8
            elif [ $major_ver -lt 60 ]; then
                ker_ver=2.6.16.46-0.12
	    else
		minor_ver=`uname -r | cut -d'-' -f 2| cut -d'.' -f 2`
		if [ $minor_ver -lt 54 ]; then  #<sp3 version
                	ker_ver=2.6.16.60-0.21
		else
			ker_ver=2.6.16.60-0.54.5
		fi

	    fi
	fi    	

	if [ "$os_name" = "sles9" ]; then
	    major_ver=`uname -r | cut -d'.' -f 4 | cut -d'-' -f 1`  		
	    flavour=`uname -r| awk -F'-' '{print $NF}'`
	    
	    if [ $major_ver -lt 139 ]; then
		ker_ver=2.6.5-7.97
            elif [ $major_ver -lt 191 ]; then
                ker_ver=2.6.5-7.139
            elif [ $major_ver -lt 244 ]; then
                ker_ver=2.6.5-7.191
            elif [ $major_ver -lt 308 ]; then
                ker_ver=2.6.5-7.244
	    else
                ker_ver=2.6.5-7.308
	    fi
	fi    	

	if [ "$os_vendor" = "REDHAT" ] ; then
	    flavour=`uname -r| awk -F'.' '{print $NF}'`

	    #For thel4, ELxenU for 32 and 64 bit is involved since rhel4.5;
	    # ELlargesmp for 64 bit is involved since rhel4.3.
   	    if [ "$os_name" = "rhel4" ]; then
		if [ "$flavour" = "ELxenU" ]; then
			ker_ver=2.6.9-55
		elif [ "$flavour" = "ELlargesmp" ]; then
			ker_ver=2.6.9-34
		else
                	ker_ver=2.6.9-5
		fi
	    fi    	

	    if [ "$os_name" = "rhel5" ]; then
		major_ver=`uname -r | cut -d'-' -f 2| cut -d'.' -f 1`
		if [ $major_ver -lt 164 ]; then
                	ker_ver=2.6.18-8
		else
			ker_ver=2.6.18-164  #since rhel5.4
		fi
	    fi
	    
	    if [ "$os_name" = "rhel6" ]; then
		major_ver=`uname -r | cut -d'-' -f 2| cut -d'.' -f 1`
		if [ $major_ver -lt 164 ]; then
                	ker_ver=2.6.32-71
		else
			ker_ver=2.6.32-164  
		fi
	    fi
	fi    	

	if [ -f ${REPL_HOME}/kernel/fs/xofs.o.`uname -r` ]; then
		drv_name=${REPL_HOME}/kernel/fs/xofs.o.`uname -r`
	else
		if [ "$os_vendor" = "SUSE" ]; then
		    drv_name=${REPL_HOME}/kernel/fs/xofs.o.${ker_ver}-${flavour}
		else
		    drv_name=${REPL_HOME}/kernel/fs/xofs.o.${ker_ver}.${flavour}
		fi
	fi

    if [ ! -f  /${drv_name} ] ; then
        echo
        echo "ERROR:"
        echo
        echo "ARCserveRHA package does not have precompiled XOFS driver $drv_name for your system "
        echo "Please contact support!"
        echo
        echo
        exit -1;
    fi
}



case "$1" in
start)
	set_os_vendor
	set_os_ver
	set_driver_name
	set_mod_params

	#If the product was reinstalled let us unload previous driver
	/sbin/rmmod `basename ${drv_name}` &> /dev/null

	#Then load driver again
	/sbin/insmod -f ${drv_name} ${mod_params}  &> /dev/null
        if test -f ${REPL_HOME}/bin/ws_rep.pid
        then
                pid=`cat ${REPL_HOME}/bin/ws_rep.pid`
                res=`ps -ae | grep $pid | grep ws_rep`
                if test "$res" != ""
                then echo "ARCserveRHA is already running. Use restart command"; exit 1;
                else echo "Removing stale pid file"; rm ${REPL_HOME}/bin/ws_rep.pid;
                fi
        fi
        echo "Starting ARCserveRHA"
        su $user -c "sh -c 'LC_ALL=$lc_all ORACLE_HOME=$oracle_home  ORACLE_BASE=$oracle_base  ORACLE_OWNER=$oracle_owner LD_LIBRARY_PATH=${oclient_home}:${LD_LIBRARY_PATH}:/${oracle_home}/lib:/opt/CA/ARCserveRHA/bin ${REPL_HOME}/bin/ws_rep -d 2>/dev/null >/dev/null' "
        ;;
restart)
        echo "Restarting ARCserveRHA"
        if test -f ${REPL_HOME}/bin/ws_rep.pid
        then
                pid=`cat ${REPL_HOME}/bin/ws_rep.pid`
                res=`ps -ae | grep $pid | grep ws_rep`
                if test "$res" != ""
                then kill -HUP `cat ${REPL_HOME}/bin/ws_rep.pid`
                else echo "Removing stale pid file"; rm ${REPL_HOME}/bin/ws_rep.pid;
                fi
        fi
        su $user -c "sh -c 'LC_ALL=$lc_all ORACLE_HOME=$oracle_home  ORACLE_BASE=$oracle_base  ORACLE_OWNER=$oracle_owner LD_LIBRARY_PATH=${oclient_home}:${LD_LIBRARY_PATH}:/${oracle_home}/lib:/opt/CA/ARCserveRHA/bin ${REPL_HOME}/bin/ws_rep -d 2>/dev/null >/dev/null' "
	
        ;;
stop)
        echo "Stopping ARCserveRHA"
        if test -f ${REPL_HOME}/bin/ws_rep.pid
        then
                pid=`cat ${REPL_HOME}/bin/ws_rep.pid`
                res=`ps -ae | grep $pid | grep ws_rep`
                if test "$res" != ""
                then kill -HUP `cat ${REPL_HOME}/bin/ws_rep.pid`
                else echo "Removing stale pid file"; rm ${REPL_HOME}/bin/ws_rep.pid;
                fi
        fi
        ;;
*)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0

