#!/bin/sh
#
# clpfunctions
#
#

logpath=""

#####################################################################
#
# clp_status()
#

clp_status()
{
        local base=${1##*/}
        local pid

        # Test syntax.
        if [ "$#" = 0 ]; then
                echo $"Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`/opt/nec/clusterpro/bin/clppidof ${base}`
        if [ -n "$pid" ]; then
                echo $"${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ]; then
                read pid < /var/run/${base}.pid
                if [ -n "$pid" ]; then
                        echo $"${base} dead but pid file exists"
                        return 1
                fi
        fi
        # See if /var/run/${base} exists
        if [ -f /var/run/${base} ]; then
                echo $"${base} dead but subsys locked"
                return 2
        fi
        echo $"${base} is stopped"
        return 3
}

#####################################################################
#
# clp_status_s()
#

clp_status_s()
{
	base=$1

        # Test syntax.
        if [ "$#" = 0 ]; then
                echo "Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`/opt/nec/clusterpro/bin/clppidof ${base}`
        if [ -n "$pid" ]; then
                echo "${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ]; then
                read pid < /var/run/${base}.pid
                if [ -n "$pid" ]; then
                        echo "${base} dead but pid file exists"
                        return 1
                fi
        fi
        # See if /var/run/${base} exists
        if [ -f /var/run/${base} ]; then
                echo "${base} dead but subsys locked"
                return 2
        fi
        echo "${base} is stopped"
        return 3
}

#####################################################################
#
# clp_logwrite()
#

clp_logwrite()
{
	echo `date '+%Y/%m/%d %H:%M:%S'` $2 >> $logpath$3.$1.cur
}

#####################################################################
#
# clp_filedel()
#

clp_filedel()
{
	# execute logcf
	logpath=`/opt/nec/clusterpro/bin/clplogcf -p`"/functions/"
	if [ $? -ne 0 ]; then
		logpath="/opt/nec/clusterpro/log/functions/"
	fi

	if [ ! -e ${logpath} ]; then
		mkdir ${logpath} >/dev/null 2>&1
	fi

case "$3" in
  SunOS)
	if [ -s $logpath$2.$1.cur ]; then
		mv $logpath$2.$1.cur $logpath$2.$1.pre
	fi

	echo "" >  $logpath$2.$1.cur
	;;
  *)
	count=10
	while [ ${count} -gt 0 ]
	do
		fcnt=`expr ${count} - 2`
		precnt=`expr ${fcnt} - 1`

		if [ ${count} = 1 ]
		then
			if [ -e $logpath$2.$1.cur ]
			then
				rm -f "$logpath$2.$1.cur" >/dev/null 2>&1
			fi
		elif [ ${count} = 2 ]
		then
			if [ -e $logpath$2.$1.cur ]
			then
				mv $logpath$2.$1.cur $logpath$2.$1.pre
			fi
		elif [ ${count} = 3 ]
		then
			if [ -e $logpath$2.$1.pre ]
			then
				mv $logpath$2.$1.pre $logpath$2.$1.pre${fcnt}
			fi
		else
			if [ -e $logpath$2.$1.pre${precnt} ]
			then
				mv $logpath$2.$1.pre${precnt} $logpath$2.$1.pre${fcnt}
			fi
		fi

		count=`expr ${count} - 1`
	done

	echo "" >  $logpath$2.$1.cur
	;;
esac
}

#####################################################################
#
# clp_success()
#

clp_success()
{
case "$1" in
  SunOS)
	echo "[OK]"
	;;
  *)
	if [ -f /etc/rc.d/init.d/functions ]; then
		echo_success
		echo
	elif [ -f /etc/rc.status ]; then
		rc_status -v
	fi
	;;
esac
}

#####################################################################
#
# clp_failed()
#

clp_failed()
{
case "$1" in
  SunOS)
	echo "[FAILED]"
	;;
  *)
	if [ -f /etc/rc.d/init.d/functions ]; then
		echo_failure
		echo
	elif [ -f /etc/rc.status ]; then
		rc_failed 1
		rc_status -v
	fi
	;;
esac
}

#####################################################################
#
# clp_checkerr()
#

clp_checkerr()
{
	if [ "$1" = "0" ]; then
		clp_success $2
	else
		clp_failed $2
		return 1
	fi
	
	return 0
}

#####################################################################
#
# the following function to check the machine's architecture
# if ia64 or ppc64,don't start Mirror Agent 
#
# clp_checkarch()
#

clp_checkarch()
{
	var_arch=`arch`

	if [ "$1" = "start" -o "$1" = "stop" ]; then
		 clp_logwrite "$1" "Architecture:"$var_arch"" $2
		 
		 case $var_arch in
		 ia64)
		  clp_logwrite "Mirror Agent will not start." $2
		  return 1
		  ;;
		 ppc64)
		  clp_logwrite "Mirror Agent will not start." $2
		  return 1
		  ;;
		esac
	fi
	
	return 0
}

#####################################################################

