#!/bin/bash
# chkconfig: 2345 99 99
# description: Initial System, Please Do Not Delete This Service
function start()
{
    mount /dev/sr0 /mnt
	if [ $? -ne 0 ];then
        echo "mount /dev/sr0 failed!" >> /tmp/initial.log
		exit 1
	fi
	if [ -f /mnt/meta-data ];then
		instance_id=`cat /mnt/meta-data |awk '{print $2}'`
		if [ -f /opt/instance_id ];then
			pre_instance=`cat /opt/instance_id`
			if [ "$pre_instance" == "$instance_id" ];then
				echo "same instance_id" >> /tmp/initial.log
				umount /mnt
				bash -x /boot_scripts >> /tmp/initial.log 2>&1
				rm -f /boot_scripts
			else
				echo $instance_id > /opt/instance_id
				if [ -f /mnt/network_script ];then
					/bin/bash -x /mnt/network_script >> /tmp/initial.log 2>&1
				else
					echo "no network_script exists!" >> /tmp/initial.log
				fi
				umount /mnt
			fi
		else
			echo $instance_id > /opt/instance_id
			if [ -f /mnt/network_script ];then
				/bin/bash -x /mnt/network_script >> /tmp/initial.log 2>&1
			else
				echo "no network_script exists!" >> /tmp/initial.log
			fi
			umount /mnt
		fi
	else
        echo "no meta-data exists!" >> /tmp/initial.log
		umount /mnt
		exit 2
	fi
}

function stop()
{
    exit 0
}

function restart()
{
    exit 0
}

function status()
{
    exit 0
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
       echo $"Usage: $0 {start|stop|restart}"
esac
