# /etc/profile

strstr() {
	[ "${1#*$2*}" = "$1" ] && return 1
		return 0
}

echo "/etc/profile start"

export PATH="/usr/sbin:/usr/bin:/bin:/sbin:/etc/Scripts:/util"
export LD_LIBRARY_PATH="/lib"
export MODULES_DIR=/lib/modules
export MICOM_BASE=/sbin
export MALLOC_CHECK_=1

############## Flash Driver ###########
# insmod $MODULES_DIR/rfs_glue.ko
# insmod $MODULES_DIR/rfs_fat.ko

############## Partition Information ##############
export MTD_ONBOOT=/dev/mmcblk0p0
export MTD_SERET_0=/dev/mmcblk0p1
export MTD_SERET_1=/dev/mmcblk0p2
export MTD_SECOS_0=/dev/mmcblk0p3
export EX_PARTITION=/dev/mmcblk0p4
export MTD_SECOS_1=/dev/mmcblk0p5
export MTD_KERNEL_0=/dev/mmcblk0p6
export MTD_ROOTFS_0=/dev/mmcblk0p7
export MTD_KERNEL_1=/dev/mmcblk0p8
export MTD_ROOTFS_1=/dev/mmcblk0p9
export SECUREMAC0=/dev/mmcblk0p10
export SECUREMAC1=/dev/mmcblk0p11
export SECUREMAC2=/dev/mmcblk0p12
export MTD_DRMREGION_A=/dev/mmcblk0p13
export MTD_DRMREGION_B=/dev/mmcblk0p14
export MTD_RWAREA=/dev/mmcblk0p15
export MTD_UNIRO=/dev/mmcblk0p16
export MTD_UNIRW=/dev/mmcblk0p17

############## SET Writable Partition ##############
echo 13 > /proc/flash_writable_parts
echo 14 > /proc/flash_writable_parts
echo 15 > /proc/flash_writable_parts
echo 17 > /proc/flash_writable_parts

############## SWU Parameter Init ##############
export PARTITION_FLAG00=/mtd_rwarea/PartitionSwitch_0_0
export PARTITION_FLAG10=/mtd_rwarea/PartitionSwitch_1_0
export UPGRADE_FLAG=/mtd_rwarea/UPGRADE_FLAG
export UPGRADE_FLAG_ER=/mtd_rwarea/UPGRADE_FLAG_ER

############## BSP Parameter Init ##############
export ROCOMMON_LINK_1=/mtd_uniro/rocommon_link_1
export ROCOMMON_LINK_2=/mtd_uniro/rocommon_link_2

ROCOMMON_INSTALL_1=/mtd_uniro/rocommon1
ROCOMMON_INSTALL_2=/mtd_uniro/rocommon2
ROCOMMON_IMG_1=/mtd_uniro/rocommon1.img
ROCOMMON_IMG_2=/mtd_uniro/rocommon2.img

EXE_INSTALL_1=/mtd_uniro/exe1
EXE_INSTALL_2=/mtd_uniro/exe2
UNCOMP_EXE_FILE_1=/mtd_uniro/uncompexe1.img
UNCOMP_EXE_FILE_2=/mtd_uniro/uncompexe2.img

############## Parameter Init ##############
SUCCESS_MOUNT="true"
ulimit -c unlimited
umask 0022

#### Execution Cgroup Init Script ####
/etc/cgroup_init &
######################################


############## Boot partition select ##############
PART_FLAG=$(cat /proc/cmdline | cut -d'/' -f3 | cut -d' ' -f1)

echo "PART_FLAG=$PART_FLAG"

echo "mount mtd_uniro"
echo 1 > /proc/flash_writable
mount -t vdfs -o noatime $MTD_UNIRO /mtd_uniro
if [ $? != 0 ]
then
	echo "mount mtd_uniro fail"
	SUCCESS_MOUNT="false"
fi

echo "strstr check"
if  strstr "$PART_FLAG" "mmcblk0p7" ;
then
	echo "1st Partition is selected"
	PartitionFlag="1st"
	EXE_INSTALL_NEEDED=$EXE_INSTALL_1
	UNCOMPEXE_MOUNT=$UNCOMP_EXE_FILE_1
else
	echo "2nd Partition is selected"
	PartitionFlag="2nd"
	EXE_INSTALL_NEEDED=$EXE_INSTALL_2
	UNCOMPEXE_MOUNT=$UNCOMP_EXE_FILE_2
fi

echo "mount mtd_rwarea"
mount -t vdfs -o noatime $MTD_RWAREA /mtd_rwarea
if [ $? != 0 ]
then
	umount /mtd_rwarea 2> /dev/null
	mkfs.vdfs $MTD_RWAREA
	mount -t vdfs -o noatime $MTD_RWAREA /mtd_rwarea
	if [ $? != 0 ]
	then
		echo "mount mtd_rwarea fail"
		SUCCESS_MOUNT="false"
	else
		chmod 777 /mtd_rwarea
	fi
else
	chmod 777 /mtd_rwarea
fi

echo "mount mtd_uncompexe"
mount -t vdfs -o noatime -o loop,stripped,ro $UNCOMPEXE_MOUNT /mtd_uncompexe
if [ $? != 0 ]
then
	echo "uncompexe mount fail"
	SUCCESS_MOUNT="false"
	#Not right after SWU
	if [ -e $UPGRADE_FLAG  ]
	then
		echo "toggle by unkown issue"
		#if 2nd partition
		if [ "$PartitionFlag" = "2nd" ]
		then 
			#toggle to 1st partition
		        $MICOM_BASE/micom 1 0
		else
			#toggle to 2st partition
			$MICOM_BASE/micom 1 1
		fi
		$MICOM_BASE/micom reboot
	fi

else
	SUCCESS_MOUNT="true"
	sync
fi

echo "rbind exe"
mount --rbind $EXE_INSTALL_NEEDED /mtd_exe
if [ $? != 0 ]
then
	echo "rbind exe fail"
	SUCCESS_MOUNT="false"
fi 


if [ "$SUCCESS_MOUNT" = "true" ]
then
	echo "insmod sfd.ko"
	insmod /mtd_exe/Comp_LIB/NetSec/sfd.ko

	insmod /mtd_exe/lib/PreInit.ko

	if [ -e /mtd_exe/DeviceInit ]
	then
		echo "Device init"
		/mtd_exe/DeviceInit&
	fi
fi

echo "mount debugfs"
mount -t debugfs -o mode=0777 none /sys/kernel/debug
if [ $? != 0 ]
then
        echo "mount debugfs fail"
fi

if [ "$PartitionFlag" = "1st" ]
then
	ROCOMMON_LINK_FILE=$(ls -al $ROCOMMON_LINK_1 | cut -d'>' -f2 | cut -d' ' -f2)
	if [ ! -f $PARTITION_FLAG00 ]
	then
		rm -f /mtd_rwarea/PartitionSwitch*
		touch $PARTITION_FLAG00
	fi
else
	ROCOMMON_LINK_FILE=$(ls -al $ROCOMMON_LINK_2 | cut -d'>' -f2 | cut -d' ' -f2)
	if [ ! -f $PARTITION_FLAG10 ]
	then
		rm -f /mtd_rwarea/PartitionSwitch*
		touch $PARTITION_FLAG10
	fi
fi

if [ "$ROCOMMON_LINK_FILE" = "" ]
then
	echo "make rocommon link file"
	if [ "$PartitionFlag" = "1st" ]
	then
		ln -s $ROCOMMON_IMG_1 $ROCOMMON_LINK_1
		if [ $? != 0 ]
		then
			echo "cannot make rocommon link"
			SUCCESS_MOUNT="false"
		fi
		sync
		ROCOMMON_LINK_FILE=$(ls -al $ROCOMMON_LINK_1 | cut -d'>' -f2 | cut -d' ' -f2)
	else
		ln -s $ROCOMMON_IMG_2 $ROCOMMON_LINK_2
		if [ $? != 0 ]
		then
			echo "cannot make rocommon link"
			SUCCESS_MOUNT="false"
		fi
		sync
		ROCOMMON_LINK_FILE=$(ls -al $ROCOMMON_LINK_2 | cut -d'>' -f2 | cut -d' ' -f2)
	fi
fi

echo "save what rocommon had installed"
if [ "$ROCOMMON_LINK_FILE" = "$ROCOMMON_IMG_1" ]
then
	ROCOMMON_INSTALLED=$ROCOMMON_INSTALL_1
else
	ROCOMMON_INSTALLED=$ROCOMMON_INSTALL_2
fi

echo "rbind rocommon"
mount --rbind $ROCOMMON_INSTALLED /mtd_rocommon
if [ $? != 0 ]
then
	echo "rbind rocommon failed"
	SUCCESS_MOUNT="false"
fi
echo 0 > /proc/flash_writable

echo "mount mtd_unirw"
mount -t vdfs -o noatime $MTD_UNIRW /mtd_unirw
if [ $? != 0 ]
then
   umount /mtd_unirw 2> /dev/null
   mkfs.vdfs $MTD_UNIRW
   mount -t vdfs -o noatime $MTD_UNIRW /mtd_unirw
   if [ $? != 0 ]
   then
       echo "mount mtd_unirw fail"
       SUCCESS_MOUNT="false"
   else
       chmod 777 /mtd_unirw
   fi
else
   chmod 777 /mtd_unirw
fi

if [ "$SUCCESS_MOUNT" = "true" ]
then
	echo "== Start exeDSP =="
	authuld 2
fi

$MICOM_BASE/micom 2

# save emergency log dump
echo "Saving emergency log dump"
/mtd_exe/SLauncher app /util/save_error_log

if [ -e $UPGRADE_FLAG  ]
then
	echo "******************************************************"
	echo "Application running is failed"
	echo "SW image is stopped"
	echo "******************************************************"
else
	echo "Application running is failed..try to do emergency state handling"
	echo "wait 5 seconds"
	x=0
	while [ $x -lt 5 ]
	do
		x=$(($x + 1))
		sleep 1
		echo $x
	done

        ###########################
        # Emergency rollback case #
        ###########################
        if [ "$PartitionFlag" = "2nd" ]
        then
              if [ -e $UPGRADE_FLAG_ER ]
              then
                      echo "no sub SW image...going shutdown : DEV"
                      $MICOM_BASE/micom shutdown
              else
                      echo "current partition 2nd is corrupted. roll back to sub partition 1st"
                      touch $UPGRADE_FLAG_ER
                      sync
                      $MICOM_BASE/micom 1 0
                      touch $UPGRADE_FLAG
                      sync
                      echo "Reboot system"
                      $MICOM_BASE/micom reboot
              fi
        else
              if [ -e $UPGRADE_FLAG_ER ]
              then
                      echo "no sub SW image...going shutdown : DEV"
                      $MICOM_BASE/micom shutdown
              else
                      echo "current partition 1st is corrupted. roll back to sub partition 2nd"
                      touch $UPGRADE_FLAG_ER
                      sync
                      $MICOM_BASE/micom 1 1
                      touch $UPGRADE_FLAG
                      sync
                      echo "Reboot system"
                      $MICOM_BASE/micom reboot
              fi
        fi
fi
