#######################Added by Jim###########################

NVM_ROOT_FOLDER="/marvell/tel/NVM"
COMCFG_1="/marvell/tel/NVM/COMCfg_1.csv"
COMCFG_125="/marvell/tel/NVM/COMCfg_125.csv"
GSM_CAL_DATA_1="/marvell/tel/NVM/GsmCalData_1.nvm"
WB_CAL_DATA_1="/marvell/tel/NVM/WbCalData_1.nvm"
GSM_CAL_DATA_125="/marvell/tel/NVM/GsmCalData_125.nvm"
WB_CAL_DATA_125="/marvell/tel/NVM/WbCalData_125.nvm"
SDCARD_DEBUG_FLAG="/marvell/tel/NVM/yuhuatel/sdcard_debug"

Boardversion=0
Boardversion=`cat /sys/devices/virtual/yh_nodes/misc/hardware_version` 

if [ $Boardversion -ge 9 ]; then
    if [ -e $COMCFG_125 ]; then
        busybox rm -f    $NVM_ROOT_FOLDER/COMCfg.csv
        busybox cp -p   $COMCFG_125    $NVM_ROOT_FOLDER/COMCfg.csv
        if [ -e $GSM_CAL_DATA_125 ] &&  [ ! -e  $NVM_ROOT_FOLDER/GsmCalData.nvm ]; then
            busybox cp -p    $GSM_CAL_DATA_125    $NVM_ROOT_FOLDER/GsmCalData.nvm
        fi
        if [ -e $WB_CAL_DATA_125 ] &&  [ ! -e  $NVM_ROOT_FOLDER/WbCalData.nvm ]; then
            busybox cp -p    $WB_CAL_DATA_125    $NVM_ROOT_FOLDER/WbCalData.nvm
        fi
    fi
else
    if [ -e $COMCFG_1 ]; then
        busybox rm -f    $NVM_ROOT_FOLDER/COMCfg.csv
        busybox cp -p    $COMCFG_1    $NVM_ROOT_FOLDER/COMCfg.csv
        if [ -e $GSM_CAL_DATA_1 ] &&  [ ! -e  $NVM_ROOT_FOLDER/GsmCalData.nvm ]; then
            busybox cp -p    $GSM_CAL_DATA_1    $NVM_ROOT_FOLDER/GsmCalData.nvm
        fi
        if [ -e $WB_CAL_DATA_1 ] && [ ! -e  $NVM_ROOT_FOLDER/WbCalData.nvm ]; then
            busybox cp -p    $WB_CAL_DATA_1    $NVM_ROOT_FOLDER/WbCalData.nvm
        fi
    fi
fi

## Jim: check whether debug flag is marked
if [ -e $SDCARD_DEBUG_FLAG ]; then
    busybox rm /marvell/etc/diag_bsp.cfg
    busybox cp /marvell/etc/diag_bsp_sd.cfg /marvell/etc/diag_bsp.cfg
    chmod 0666 /dev/log/main
    chmod 0666 /dev/log/radio
    chmod 0666 /dev/log/events
    chmod 0666 /dev/log/system    
else
    busybox rm /marvell/etc/diag_bsp.cfg
    busybox cp /marvell/etc/diag_bsp_usb.cfg /marvell/etc/diag_bsp.cfg
fi    

#######################Added by Jim End###########################


## hw.sh - apply debug service first
insmod ./hwmap.ko
mknod /dev/hwmap c 237 0
chmod 660 /dev/hwmap

# Run the upgrade check script to detect and fix NVM contents produced by another version
./busybox sh /marvell/tel/nvm_upgrade.sh

# Release COMM from reset
./hwacc w 0x40F5001C   1
echo 1 > /sys/power/cp

# Uncomment the next line in order to disable Suspend and Freezer
#echo "Marvell" > "/sys/power/wake_lock"
# Uncomment the next line in order to enable Suspend and disable Freezer
#echo 1 > /sys/power/android_freezer_disable
## YH debug.
export USB_ETHERNET_FILE="/marvell/tel/run_ether_usb.sh"
export USB_ANDROID_FILE="/marvell/tel/run_android_usb.sh"
export NVM_ROOT_DIR="/marvell/tel/NVM"

## YH busybox
#if [ ! -e $NVM_ROOT_DIR ]; then
	busybox mkdir -p $NVM_ROOT_DIR
#fi
ln -s /system/busybox busybox
chmod 777 busybox
if [ ! -e /data/log/java_exec ]
then
        busybox touch /data/log/java_exec
        busybox touch /data/log/java_exec_id
fi

chmod 766 /marvell/etc/diag_bsp.cfg
mkdir /data/etc

# Set error log location to /data volume, default is NVM
# Should be before mtsd starts as diag should be able to override the setting
if [ ! -e /data/log ]; then mkdir /data/log; fi # do not use mkdir -p as it fails due to ro fs (busybox mkdir -p is ok)
echo "/data/log" > /tmp/diag_log_path

busybox cp /marvell/etc/asound.conf /data/etc/asound.conf
echo 0 > /proc/sys/kernel/hung_task_timeout_secs
#busybox sh rtc_offset.sh
busybox sh diag_port_conf.sh
insmod osadev.ko
insmod seh.ko
## control qos mode - default off
insmod acipcdev.ko	69	## create /marvell/tel/qos_mode_on to enable on boot
if [ -e /marvell/tel/qos_mode_on ]; then
insmod acipcdev.ko param_qos_mode=1
else
insmod acipcdev.ko param_qos_mode=0
fi
insmod cci_datastub.ko
sleep 1
insmod citty.ko
insmod ccinetdev.ko
insmod cidatatty.ko

# Remove following remark to enable Bluetooth DUN support
insmod bt_tty.ko

# Enable GPS support
insmod gps_sirf.ko
chown system.system /dev/gps_sirf
chown system.system /dev/ttyS2


#The Diag driver works with /mnt/mmc
#Therefore it is linked to /mnt/SD1
#ln -s /mnt/SD1 /mnt/mmc

# delay 1 second to get udevd notified about the devices creation
# in the modules above, before atcmdsrv and audioserver start
sleep 1
./mtsd &
chmod 660 /dev/seh
chmod 666 /dev/cidatatty0
chmod 666 /dev/cctdatadev0
./mtilatcmd &

#For CTS, delete
#chmod 777 /sys/devices/platform/pxa-u2o/composite 

## YH gadget method
if [ -f $USB_ETHERNET_FILE ]; then
     busybox sh $USB_ETHERNET_FILE
else
if [ -f $USB_ANDROID_FILE ]; then
     setprop ctl.stop adbd
     busybox sh $USB_ANDROID_FILE
     adbd &
else
port=`cat /tmp/at_port.txt`
if [ $port == 'UART' ]; then
	#Set AT and Diag over UART

    ./mtilatcmd  -u 1&
else
    busybox ln -s /dev/ttyDIAG0 /dev/ttygserial
    busybox ln -s /dev/ttyGS0 /dev/ttymodem
    ## YH do it
    ## insmod ppp.ko
    ## set default usb composite configuration, triggers usb enumeration
    

# To enable Bluetooth DUN support, remove the remark from the line with /dev/btduntty,
# and add a remark to the line with /dev/ttymodem
## YH do it
##./mtilatcmd -m /dev/ttymodem&
##./mtilatcmd -m /dev/btduntty1&

fi
fi
fi

## YH gadget.
if [ -e  /data/data/com.android.contacts/usbacat ]; then
     echo acm,diag > /sys/devices/platform/pxa-u2o/composite
else
if [ `grep -c ACAT=USB /proc/cmdline` == 1 ]; then
     echo acm,diag > /sys/devices/platform/pxa-u2o/composite
## can't use following code. marked by zenith.
## else
##      echo adb,usb_mass_storage > /sys/devices/platform/pxa-u2o/composite
fi
fi

## attention. replace following to bootcompleted.sh
## if [ -e  /data/data/com.android.contacts/serial_debug ]; then
##     busybox dmesg -n 7
## else
##     busybox dmesg -n 1
## fi

## If we use bt DUN, we need this
## Remove following remark to enable Bluetooth DUN support
## insmod bt_tty.ko

./audioserver -D yes&
./eeh -D yes -M yes&

./validationif &

#set shell as non freezable
echo "sh 1" > /sys/power/freeze_process/frz_process

#disable D0CS set user constrain on OP 0
#echo  0, 0  > /sys/devices/system/cpu/cpu0/enable_op
#echo 0, 0  > /sys/devices/system/cpu/cpu0/control
###################################################################
# Not required any more for since 8787 A1 fixes the uplink issue
#insmod /system/lib/modules/mlan.ko
#insmod /system/lib/modules/sd8787.ko
#insmod /system/lib/modules/bt8787.ko
#echo 1 > /sys/class/rfkill/rfkill0/state
###################################################################

## YH do it. 
## Uncomment some of the following lines to create logcat files on target (for debug)
LOGCAT_DIR="/data/log/terminal_log"
RAMDUMP_FILE="/sdcard/RAMDUMP0000.gz"
RAMDUMP_TEXT_FILE="/sdcard/RAMDUMP0000.txt"

if [ -e $SDCARD_DEBUG_FLAG ]; then

    while [ `grep -c sdcard /proc/mounts`  == 0 ]
    do
        echo "Wait for SD card mounted"
        sleep 2
    done

    echo "SD Card mounted"
    sleep 2    

    TIME=`date "+%Y-%m-%d-%H-%M-%S"`
    RAMDUMP_SAVED_DIR="RAMDUMP-LOG-$TIME"
    LAST_REBOOT_SAVED_DIR="LAST_REBOOT-LOG-$TIME"
    
    if [ -e $RAMDUMP_FILE ] || [ -e $RAMDUMP_TEXT_FILE ]; then
        busybox mkdir /sdcard/$RAMDUMP_SAVED_DIR
        mv $RAMDUMP_FILE /sdcard/$RAMDUMP_SAVED_DIR
        mv $RAMDUMP_TEXT_FILE /sdcard/$RAMDUMP_SAVED_DIR
        busybox chmod -R 777 /data/log
	   busybox chown -R 1000:1000 /data/log
        busybox cp -rf /data/log /sdcard/$RAMDUMP_SAVED_DIR
        echo "Ramdump files saved successfully"
    fi
    
    busybox mkdir /sdcard/$LAST_REBOOT_SAVED_DIR
    busybox cp -rf /data/log /sdcard/$LAST_REBOOT_SAVED_DIR
    
    busybox rm -rf /data/log
    busybox mkdir -p $LOGCAT_DIR
        
    echo 1 > /sys/class/yh_nodes/misc/cp_reset
    /marvell/tel/diag_mmi start
        
    chmod 777 $LOGCAT_DIR	
    logcat -b main -f $LOGCAT_DIR/logcat.log -r 512 -n 4 -v time &
    logcat -b radio -f $LOGCAT_DIR/logcat-radio.log -r 512 -n 16 -v time &
    logcat -b events -f $LOGCAT_DIR/logcat-events.log -r 512 -n 4 -v time &
    cat /proc/kmsg > $LOGCAT_DIR/kernel.txt &
    chmod 777 $LOGCAT_DIR/*    
	    
    kernel_log_size=0
    const_number=0
	while [ $const_number == 0 ]
     do
	       kernel_log_size=`ls -l /data/log/terminal_log/kernel.txt | awk '{print $4}'`
	       if [ $kernel_log_size -ge 1048576 ]; then
	            killall cat
	            busybox cp -f $LOGCAT_DIR/kernel.txt $LOGCAT_DIR/kernel.txt.1
	            cat /dev/null > $LOGCAT_DIR/kernel.txt
                cat /proc/kmsg > $LOGCAT_DIR/kernel.txt &  
	       else
	           sleep 60
	       fi    
     done
fi
## end of not do it.

# Uncomment one of the following lines to change the USB default configuration after reboot (for debug)
# 1. ADB ACM USB_MASS_STORAGE DIAG (default)
# echo adb,acm,usb_mass_storage,diag > /sys/devices/platform/pxa-u2o/composite
# 2. ACM DIAG
# echo acm,diag > /sys/devices/platform/pxa-u2o/composite
# 3. ADB ACM USB_MASS_STORAGE
# echo adb,acm,usb_mass_storage > /sys/devices/platform/pxa-u2o/composite
# 4. ACM USB_MASS_STORAGE
# echo acm,usb_mass_storage > /sys/devices/platform/pxa-u2o/composite
# 5. USB_MASS_STORAGE
# echo usb_mass_storage > /sys/devices/platform/pxa-u2o/composite
# 6. RNDIS
# echo rndis > /sys/devices/platform/pxa-u2o/composite
