#!/bin/sh
# chkconfig: 345 30 70
#
# config (old nvdata)
#
# mount /config and if it fails, erase potential corruption (but not bad blocks)
# if the mount fails, we don not want to mount in the main jffs2 partition or
# we cannot switch to the other side after an OTA upgrade.

############
# CHANGELOG
#===========
#
# 2012/12/20	PR412060	zimingxu <Ziming.b.Xu@alcatel-lucent.com>
#				DM URL update using ssh
#
# 2013/01/24    PR261951        Jerry ZHOU
#                               reduce time of unavailabitiy during boot time
#                   
# 2011/07/22    PRID261789      Xiaodong YANG	
#				support backlight TI chip 38 levels
# 2011/03/22	CRMS00250797	Thomas CREMEL
#				modify the security of /config 
#
# 2011/03/04    CRMS00296221    Abdelfattah CHEHAB
#				check config files for curreption
#
# 2010/08/27    CRMS00255786    Abdelfattah CHEHAB
#				updateCurrentConfig
#
# 2010/07/05	CRMSXXXXXXXX	Guillaume CATTO
#				add the remove of data (D) RPMs from database
#				in case of error on DATA partition
#
# 2010/06/03    CRMS00229700    Michel SULYAN
#               add post treatment of reset flash
#
# 2010/03/04    CRMS00217840    Thanh lam NGUYEN
#               add management of missing partitions
################

. /etc/init.d/rc.config

case "$1" in
    start|"")
# CRMS00217840
        ## List of needed partitions
        part="ubi0:FAB-DATA ubi0:CONFIG ubi0:SYSTEM  ubi0:DATA"
        OLDIFS=$IFS
        IFS=$'\n'
        for line in $(grep ubi ${ROOT:-}/proc/mounts); do
            : "${attr}>>>> line=[$line] <<<<${default}"
            unset IFS
            ## split the line in $1 $2 ....
            set ${line:-""}
            ## remove from part, the partitions that are found
            part="${part//$1}"
        done
        IFS=$OLDIFS
        ## check if there is missing partition
        for missing in $part; do
            case "$missing" in
                ubi0:DATA|ubi0:CONFIG) : "${attr}Missing $missing${default}"
                    displaymsg "Missing $missing"
                    sleep 1
                    dev=$(ubinfo -a | awk "/^Volume ID:/{vid=\$3;dev=\$5;sub(/)/,\"\",dev)}\$2==\"${missing#*:}\"{print \"/dev/\"dev\"_\"vid;exit}")
                    if [[ -n "$dev" ]]; then
                        displaymsg "formatting $dev"
                        ubiupdatevol "$dev" -t
                    fi
					# CRMSXXXXXXXX G.Catto
					if [[ "$missing" == "ubi0:DATA" ]]; then
						## check if missing partition is data. In this case, delete all RPMs marked D
						drpms=$(rpm -qa --qf '%{NAME}-%{GROUP}\n' | grep "D:" | cut -d"-" -f1)
						displaymsg "Removing all Data packages entries"
						if [[ ! -z $drpms ]]; then
							: "Removing $drpms"
							rpm -e --nodeps $drpms
							[[ $? -ne 0 ]] && displaymsg "Failed to remove Data packages"
						fi
                    fi
					# CRMSXXXXXXXX G.Catto END
                ;;
                ubi0:SYSTEM|ubi0:FAB-DATA) : "${attr}Missing $missing${default}"
                    ## SYSTEM should never be missing, or we would not be here ....
                    displaymsg "FAB partition is missing. Cannot recover !!!"
                    gotoInfiniteLoop
                ;;
            esac
        done
        if [[ -n "${part//[[:blank:]]}" ]]; then
            reboot --owner config --reason "missing partition(s)"
            gotoInfiniteLoop
        fi
# CRMS00217840 END
        if [ ! -L /nvdata ]; then
            rm -rf /nvdata >/dev/null 2>&1
            ln -s /config /nvdata
        fi

#PR261951 jerryzh+
        # crms00223288: move this from network
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        [[ -f /config/dhcp/enet.cfg ]] && rm -f /config/dhcp/enet.cfg
#PR261951 jerryzh-

        mkdir -p $NVVOIP_ROOT $NVETC_ROOT $NVUBOOT_ROOT 2>/dev/null
        mkdir -p $NVLOCAL_ROOT $NVDHCP_ROOT $NVDM_ROOT $NVLLDP_ROOT /var/run/current /var/run/lldp 2>/dev/null
        [ "$(readlink $NVCURRENT_ROOT)" != "/var/run/current" ] && { rm -rf $NVCURRENT_ROOT; ln -s /var/run/current $NVCURRENT_ROOT; }
        [ "$(readlink $NVLLDP_ROOT)" != "/var/run/lldp" ] && { rm -rf $NVLLDP_ROOT; ln -s /var/run/lldp $NVLLDP_ROOT; }
        [ "$(readlink $NVFAB_ROOT)" != "/config-fab/fabconfig" ] && { rm -rf $NVFAB_ROOT; ln -s /config-fab/fabconfig $NVFAB_ROOT; }
# PR412060 zimingxu+
	rm -rf $NVDHCP_ROOT/enet.cfg
# PR412060 zimingxu-
        /etc/init.d/dmconfig createtmpdir        

# CRMS00250797 START
# disallow write access to config files
	find /config -xdev -follow -perm +go=w -exec chmod og-w {} \;
# only root:root files in /config
	find /config -xdev -follow ! \( -user root -a -group root \) -exec chown 0:0 {} \;
# CRMS00250797 END
#CRMS00296221
	defineConfigFileList
	checkConfigFiles
#CRMS00296221
#CRMS00255786
	updateCurrentConfig
#CRMS00255786 END
#PR261951 jerryzh+
        [[ ! -d /data/core ]] && mkdir /data/core/
#PR261951 jerryzh-
	chmod ug+rwx /data/core
	chown admin:admin /data/core
# CRMS00229700
        # check reset flash requested  (post treatment)
	if [ -f /.reset_flash ]; then
           rm -f /.reset_flash >/dev/null 2>&1
           /sbin/reset_flash post
        fi
# CRMS00229700 END
        # touch screen calibration
        ##if [[ ! -f $NVETC_ROOT/pointercal ]]; then
        ##	if [[ ! -f $NVFAB_ROOT/pointercal ]]; then
        ##	    [[ -x /usr/bin/ts_calibrate ]] && /usr/bin/ts_calibrate
        ##	else
        ##	    cp $NVFAB_ROOT/pointercal $NVETC_ROOT/pointercal
        ##	fi
        ##fi
    ;;
    stop)
        umount $NV_ROOT
        if [ $? == 0 ]; then
            echo "$NV_ROOT un-mounted successfully"
        else
            echo "$NV_ROOT un-mount failed $?"
        fi
    ;;
    *)
        echo "Usage $0 {start|stop}" >&2
        exit 1
    ;;
esac
