#
#	Set these variables if they're not already set...
#

prefix=/usr
exec_prefix=/usr
: ${HA_DIR=/etc/ha.d}
: ${HA_RCDIR=$HA_DIR/rc.d}
: ${HA_CONFDIR=$HA_DIR/conf}
: ${HA_CF=$HA_DIR/ha.cf}
: ${HA_VARLIB=/var/lib/heartbeat}
: ${HA_RSCTMP=/var/lib/heartbeat/rsctmp}
: ${HA_FIFO=/var/lib/heartbeat/fifo}
: ${HA_BIN=/usr/lib64/heartbeat}
: ${HA_DATEFMT="%Y/%m/%d_%T "}
: ${HA_RESOURCEDIR=$HA_DIR/resource.d}
: ${HA_DOCDIR=/usr/share/doc/heartbeat}
: ${HA_LOGTAG=heartbeat}
: ${HA_VARRUN=/var/run/}
: ${HA_VARLOCK=/var/lock/subsys/}

export HA_DIR HA_RCDIR HA_FIFO HA_BIN 
export HA_DEBUGLOG HA_LOGFILE HA_LOGFACILITY
export HA_DATEFMT HA_RESOURCEDIR HA_DOCDIR

#	A suitable echo command
Echo() {
  echo "$@"
}

#	Echo without putting a newline on the end
EchoNoNl() {
    Echo  "$@"
}

#	Echo with escapes enabled...
EchoEsc() {
    Echo  "$@"
}

hadate() {
  date "+${HA_DATEFMT}"
}

ha_log() {
	if
	  [ -n "$HA_LOGFACILITY" ]
        then
	  : logging through syslog
	  # loglevel is unknown, use 'notice' for now
          loglevel=notice
          case "${*}" in
            *ERROR*)	loglevel=err;;
            *WARN*)	loglevel=warning;;
	  esac
	  logger -t $HA_LOGTAG -p ${HA_LOGFACILITY}.${loglevel} "${*}"
        fi	
        if
	  [ -n "$HA_LOGFILE" ]
	then
	  : appending to $HA_LOGFILE
	  Echo "$HA_LOGTAG: "`hadate`"${*}" >> $HA_LOGFILE
	else
	  Echo `hadate`"${*}" >&2
	fi
}

ha_debug() {
	if
	  [ -n "$HA_LOGFACILITY" ]
	then
	  : logging through syslog
	  logger -t $HA_LOGTAG -p ${HA_LOGFACILITY}.debug "${*}"
	fi
        if
	  [ -n "$HA_DEBUGLOG" ]
	then
	  : appending to $HA_DEBUGLOG
	  Echo "$HA_LOGTAG: "`hadate`"${*}" >> $HA_DEBUGLOG
	else
	  Echo `hadate`"${*}" .$HA_LOGFACILITY. >&2
	fi
}

ha_clustermsg() {
	cat $* <<-!MSG >> $HA_FIFO
>>>
`cat`
<<<
!MSG
}

ha_parameter() {
  VALUE=`sed -e 's%[ 	][ 	]*% %' -e 's%^ %%' -e 's%#.*%%'   $HA_CF |
  grep -i "^$1 " | sed 's%[^ ]* %%'`
  if
    [ "X$VALUE" = X ]
  then
    
    case $1 in
      keepalive)	VALUE=2;;
      deadtime)
			ka=`ha_parameter keepalive`
			VALUE=`expr $ka '*' 2 '+' 1`;;
    esac
  fi
  Echo $VALUE
}

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

  ret_status=`/bin/ps -ao pid,command | grep $base | sed 's/ .*//'`

  if 
    [ "$ret_status" != "" ]
  then
    echo "${base} is running..."
  return 0
  fi

  if 
    [ -f $HA_VARRUN/${base}.pid ] 
  then
    echo "${base} dead but pid file exists"
    return 1
  fi

  if 
    [ -f /var/run/${base}.pid ] 
  then
    echo "${base} dead but pid file exists"
    return 1
  fi

  if 
    [ -f $HA_VARLOCK/var/lock/subsys/${base}.pid ] 
  then
    echo "${base} dead but lock file exists"
    return 2
  fi

  if 
    [ -f /var/spool/lock/${base} ] 
  then
    echo "${base} dead but lock file exists"
    return 2
  fi
}
#

#
# pseudo_resource status tracking function...
#
# This allows pseudo resources to give correct status information.  As we add
# resource monitoring, and better resource tracking in general, this will
# become essential.
#
# These scripts work because ${HA_RSCTMP} is cleaned out every time
# heartbeat is started.
#
# We create "resource-string" tracking files under ${HA_RSCTMP} in a
# very simple way:
#
#	Existence of "${HA_RSCTMP}/resource-string" means that we consider
#	the resource named by "resource-string" to be running.
#
# Note that "resource-string" needs to be unique.  Using the resource type
# plus the resource instance arguments to make up the resource string
# is probably sufficient...
#
# usage: ha_pseudo_resource resource-string {start|stop|status|restart|reload}
#	Note that all operations are silent...
#
ha_pseudo_resource()
{
  ha_resource_tracking_file="${HA_RSCTMP}/$1"
  case $2 in
    start|restart|reload)  touch "$ha_resource_tracking_file";;
    stop) rm -f "$ha_resource_tracking_file";;
    status) test -f "$ha_resource_tracking_file";;
    *)	return 3;;
  esac
}
