#!/bin/sh
# Initial script by Micahel Brown
# Added new fi_error function:2002-08-26 by Rez Kabir

#This file contains common functions that should be used by all installation scripts

#Global Variables
	FOUT=/tmp/fist/fist.log
	FERR=/tmp/fist/fist.err
	RP=/mnt/RP/

	#define this, the parent is supposed to define $DIR, but 
	# we need to use this so that we don't mess up if the parent
	# forgets to define it.
	if [ -z "$DIR" ]; then
		echo "Parent process: $0 forgot to define the \$DIR variable... exiting..."
		exit 1
	fi

#fi_error() {
#	echo "RETMSG='$*'" > $RP/$FERR
#}


#Log error messages from factory process.
#Should be called like this:
#	fi_error "msg"
# example:
# 	fi_error "bad rpm format"

fi_error() {
   
[ ! -f $FERR ] && mkdir -p $( dirname $FERR )
 echo "Error:in $0]:	$*" >> $FERR 

}

fi_status() {
	local RETCODE=0
	if [ -e $RP/$FERR ]; then
		. $RP/$FERR
	fi
	return $RETCODE
}

fi_errmsg() {
	#sets env var globally
	FI_ERRMSG=""
	if [ -e $RP/$FERR ]; then
		. $RP/$FERR
                FI_ERRMSG=$RETMSG
	fi
}

logecho () {
  	# The "\r" in the lines below are to work around broken 
  	#  DOS linebreak convention (ugh)
	if [ ! -d $( dirname $FOUT ) ]; then
		mkdir -p $( dirname $FOUT )
	fi
  	echo -e $* "\r" >>$FOUT
  	echo -e $* "\r"

}

do_rpm() {
	local FLAG=$1
	local DIR=$2
	local RPMLIST
	local NODEPS="--nodeps"
	local FORCE="--force"
        local k
	for k in $DIR/*.[rR][Pp][mM]; do
		if [ -f $k ]; then
			RPMLIST="$RPMLIST $k"
		fi
	done
 	if [ ! -e $DIR/nodeps-flag ]; then
		NODEPS=
	fi
 	if [ ! -e $DIR/force-flag ]; then
		FORCE=
	fi
	if [ ! -z "$RPMLIST" ]; then
		logecho "  rpm $FLAG $RPMLIST $NODEPS $FORCE"
		rpm $FLAG $RPMLIST $NODEPS $FORCE
	fi
}

platform() {
	PLATFORM=$( $DIR/../sysinfo | grep -i ^product | cut -d: -f2 | sed -e 's/\/.*//' | perl -pi -e 's/\s*$//')
	#remove leading whitespace
	PLATFORM=${PLATFORM## }
	#remove trailing whitespace
	PLATFORM=${PLATFORM%% }
}
