#!/bin/sh

BASENAME=`basename $0`
VERSION="0.1"

THIS_SCRIPT=`readlink -e $0`

DEF_FB_DIR="/usr/var/adm/first-boot"

FB_SH="first-boot.sh"		# name of "first-boot" script
FB_ID="fbt"			# ID of our entry in inittab (1-4 characters)


usage()
{
	cat <<_EOF

Usage: $BASENAME <OPTIONS> [DIR]

Run or control first-boot operations of system. The first-boot operations
consist of shell scripts which purpose is to complete the configuration or
installation of certain facilities or software packages.
Enabling the first-boot procedure causes an entry to be added to the system's
"inittab" file. On the next reboot of the system, the first-boot procedure is
run using the directory DIR to find the first-boot scripts. If DIR is not
specified, the directory $DEF_FB_DIR is used as the default.
After the procedure has run, it is automatically disabled by removing the
previously added entry from the "inittab" file.

OPTIONS

  -f        run first-boot scripts
  -i FILE   install first-boot entry in inittab FILE
  -r FILE   remove first-boot entry from inittab FILE
  -h        show this help and exit
  -V        show version and exit
_EOF

	exit 1
}

version()
{
	cat <<_EOF
$BASENAME $VERSION
_EOF

	exit 1
}

run_firstboot()
{
	fb_dir=$1
	pkg_dirs=""
	pkg=""
	task=""
	ret=0

	[ -d "$fb_dir" ] || return 1

	echo "Checking for first-boot scripts..."

	pkg_dirs=`find $fb_dir -maxdepth 1 -type d -printf "%P\n" \
		  2>/dev/null | grep '^[[:alnum:]_]\+' | sort`
	if test -n "$pkg_dirs"; then
		for pkg in $pkg_dirs; do
			task="$fb_dir/$pkg/$FB_SH"
			if test ! -f "$task"; then
				echo "No task script found for \`$pkg'."
				ret=1; continue
			fi
			
			echo "Running task(s) for \`$pkg'..."
			/bin/sh -c "$task"
			if test $? -ne 0; then
				ret=1
			fi
		done
	else
		echo "No integration tasks defined."
	fi

	return $ret
}

enable_firstboot()
{
	inittab=$1
	fb_dir=$2
	fb_sh="$THIS_SCRIPT"
	fb_opt="-f"

	[ -f "$inittab" ] || return 1

	if test -n "$fb_dir"; then
		if test ! -d "$fb_dir"; then
		  echo "$BASENAME: warning: $fb_dir: directory does not exist"
		fi
		fb_opt="$fb_opt $fb_dir"
	fi
	sed -e "/^[[:blank:]]*.*:[Ss]\+:.*/i\\$FB_ID:Ss:wait:$fb_sh $fb_opt" \
	  -i~ $inittab
}

disable_firstboot()
{
	inittab=$1

	[ -f "$inittab" ] || return 1

	sed -e "/^[[:blank:]]*$FB_ID:[Ss]\+:.*/d" -i~ $inittab
}


FB_DIR="$DEF_FB_DIR"
INITTAB=""
OP=""		# operation: run, enable or disable first-boot procedure

while getopts :fi:r:hV OPT
do
	case $OPT in
	f) if test -z "$OP" -o "$OP" = "run"; then
	     OP="run"
	   else
	     echo "only one operation at a time" >/dev/stderr; usage
	   fi
	   ;;
	i) if test -z "$OP" -o "$OP" = "enable"; then
	     INITTAB=$OPTARG
	     OP="enable"
	   else
	     echo "only one operation at a time" >/dev/stderr; usage
	   fi
	   ;;
	r) if test -z "$OP" -o "$OP" = "disable"; then
	     INITTAB=$OPTARG
	     OP="disable"
	   else
	     echo "only one operation at a time" >/dev/stderr; usage
	   fi
	   ;;
	h) usage
	   ;;
	V) version
	   ;;
	?) echo "$OPTARG -- unknown option" 2>&1
	   usage
	   ;;
	:) echo "$OPTARG -- missing argument" 2>&1
	   usage
	   ;;
	esac
done

eval dir_arg=\${$OPTIND}
if test -n "$dir_arg"; then
	FB_DIR=$dir_arg
fi

case $OP in
run)     run_firstboot     "$FB_DIR"
	 disable_firstboot "$INITTAB"
	 ;;
enable)  enable_firstboot  "$INITTAB" "$dir_arg" # only use dir. if overwritten
	 ;;
disable) disable_firstboot "$INITTAB"
	 ;;
*)       echo "$OP: unsupported operation"
	 exit 1
	 ;;
esac
