#!/bin/bash
#
# 	DESCRIPTION: chroot $TARGET excute script under /var/finish_install/.
#				copy 99finish_install to /var/finish_install/, 	then one 
#				by one copy the script in /var/finish_install/ to $TARGET
#				with sequence, 	and then chroot $TARGET to execte it.
#
#	SCRIPT NAME:	exec_finish_install.sh		
#
#	Input:	stdin
#			1. script name which need run after installation
#
#	Output:	
#			NULL
#	Return value:
#			dependency return value of execute script, if 0 is success.
#
#	AUTHOR:		Qin Bo
#
#	EMAIL:		bqin@linx-info.com
#
#	DATE:		2010-08-27
#
#	HISTORY:
#	REVISOR		DATE			MODIFICATION
#	Qin Bo		2010-08-27		create
#
#
#

source ./functions

prep ()
{
	local mp

	if [ ! -d $FINISH_INSTALL_DIR ];then
		mkdir -p $FINISH_INSTALL_DIR
	fi

	for mp in /dev /proc /sys
	do
		mount --bind -n $mp "$TARGET$mp"
	done
}

clean_mp ()
{
	local mp

	for mp in /dev /proc /sys
	do
		umount $mp
	done
}

main () 
{
	local base
	local script
	local installer_dir
	installer_dir="$TARGET/var/log/installer"

	prep

	# run all the script in /usr/lib/new_install/operation/finish_install,
	# execute sequence through number of script name
	for script in "$FINISH_INSTALL_DIR"/*; do
		base=$(basename $script | sed 's/[0-9]*//')
		if [ -x "$script" ];then
			info "Running $base"
			$script >>$LOG_FILE 2>&1
			erv
		#else
		#	warn "Unable to execute $script"
		fi
	done
	info "Running finish install success"
	[ -d "${installer_dir}" ] || mkdir -p "${installer_dir}"
	[ ! -c "$LOG_FILE" ] && cp $LOG_FILE "${installer_dir}"
	[ ! -c "/var/install/install.xml" ] && cp "/var/install/install.xml" "${installer_dir}"
	[ ! -c "$DEV_LOG" ] && cp $DEV_LOG "${installer_dir}" || true
	
	clean_mp
}

FINISH_INSTALL_DIR="/usr/lib/new_install/operation/finish_install"

main "$@"


