#!/bin/sh

ERR=0;
case "$1" in
    "start")
        # Mount /proc
        if [ "X$(grep -c /proc /etc/fstab)" != "X0" ]; then
            /bin/mount /proc;
            if [ "X$?" != "X0" ]; then ERR=2; fi
        fi            
        # remount the root filesystem as rw
        /bin/mount / -o remount,rw,noatime;
        if [ "X$?" != "X0" ]; then ERR=1; fi
        # Set proper access permissions on root directory
        /bin/chmod 755 /;
        if [ "X$?" != "X0" ]; then ERR=2; fi
        # Mount /sys
        if [ "X$(grep -c /sys /etc/fstab)" != "X0" ]; then
            /bin/mount /sys;
            if [ "X$?" != "X0" ]; then ERR=3; fi
        fi            
        # Mount tempFS disk
        if [ "X$(grep -c /tmpmnt /etc/fstab)" != "X0" ]; then
            /bin/mount /tmpmnt;
            if [ "X$?" != "X0" ]; then ERR=4; fi
        fi
        # Make sure the symbolic links directories are created
        if [ -d /tmpmnt ]; then
            # /tmp
            /bin/mkdir -p /tmpmnt/tmp
            if [ "X$?" != "X0" ]; then ERR=5; fi
            chmod 777 /tmpmnt/tmp;
            # /var/log
            /bin/mkdir -p /tmpmnt/var_log;
            if [ "X$?" != "X0" ]; then ERR=6; fi
			if [ -d /var/log ]; then
				mv /var/log /var/base_log;
				if [ "X$?" != "X0" ]; then ERR=7; fi
			fi
            if [ ! -L /var/log ]; then
                ln -s /tmpmnt/var_log /var/log;
                if [ "X$?" != "X0" ]; then ERR=8; fi
            fi
            if [ -d /var/base_log ]; then
				cnt=`ls -1 /var/base_log/ |wc -l`
				if [ $cnt -ne 0 ]
				then
					cp -ra /var/base_log/* /tmpmnt/var_log;
					if [ "X$?" != "X0" ]; then ERR=9; fi
				fi
            fi
            # /var/run
            /bin/mkdir -p /tmpmnt/var_run
            if [ "X$?" != "X0" ]; then ERR=10; fi
        fi
        ;;
    "stop")
        /bin/umount -a -r -f;
        if [ "X$?" != "X0" ]; then ERR=50; fi
        ;;
    *)
        echo "Syntax is $0 start | stop"
        exit 255;
        ;;
esac

exit ${ERR};
