#!/bin/sh

#CHANGELOG
#===========
# 2012/12/12    PR368931      Qianjun Zhang
#               Creation for feature "reset to factory"
# 2013/02/19    CRMS00416857      Qianjun Zhang
#               reset all the log level to default value
#################################################################################
source /etc/functions
resetflash="/usr/sbin/debug/reset"
upgd_workdir="/config/upgrade"
UPGD_STATUS=/usr/sbin/upgd_status
UPGD_CONFIG_STATUS=/config/upgrade/status
owner=                                                                          
reason=                                                                         
                                                                                
while [[ $# -gt 0 ]]; do                                                       
    case "$1" in                                                               
        --reason) :                                                            
            reason=$2                                                          
            shift                                                              
            ;;                                                                 
        --owner) :                                                             
            owner=$2                                                           
            shift                                                              
            ;;                                                                 
        *)                                                                     
            ;;                                                                 
    esac                                                                       
    shift                                                                      
done     

logger -t reset__ -p local6.emerg "reset to factory invoked by $owner,reason: $reason."

#20130115 move to Platform.cpp
#if step >3 return without doing anything
#step=`cat $stepFile`
#if [[ $step -gt 3 ]];then
#  echo "can not reset to factory after initialization step 3!"
#  exit    
#fi


displaypopup "Reset to Factory, Waiting for reboot"

#if in upgrade process.reset status of upgrade,go on
#and this can make /usr/sbin/debug/reset won't give up from reset when upgrade.
if [ -x $UPGD_STATUS ]; then
   $UPGD_STATUS >/dev/null 2>&1
   if [ $? -eq 1 ]; then
     #the same with "dwl rststatus"
     echo "STEP=pre0" > $UPGD_CONFIG_STATUS
     rm $upgd_workdir/*.sum
     rm $upgd_workdir/.serversip.bak
     echo "in upgrade,upgrade status is resetted"
      
   fi
fi

#delete all the External rpms
Externalrpms=` rpm -qg E:External >/dev/null 2>&1` 
 
for i in ${Externalfiles:-}; do
   echo "=============start to  erase $i======================" 
   rpm -e --nodeps $i  
   echo "============= erase $i done======================="
done

#delete all the Data rpms
Datarpms=`rpm -qg D:Data >/dev/null 2>&1`

for i in ${Datarpms:-}; do
   echo "=============start to  erase $i======================"  
   rpm -e --nodeps $i  
   echo "============= erase $i done======================="  
done      

#crms00416857
level all init 

if [ -x $resetflash ]; then
   echo "running $resetflash..."
   $resetflash flash &
fi


