#!/bin/busybox sh
TAG=[DATA_BACKUP_RECOVER]
echo "$TAG: === Initial: LYNQ MOUNT Abnomal Judge Start ===" > /dev/kmsg
FilePath=/data/lynq-low-power.sh

detect_read(){
    a=1
    while [ $a -le 3 ]
    do
        if [ -r "$FilePath" ]
        then
            echo "$TAG a=$a can read" > /dev/kmsg
            return 0
        else
            echo "$TAG a=$a can't read" > /dev/kmsg
        fi
        a=$[$a+1]
    done
    return 1
}
detect_write(){
    a=1
    while [ $a -le 3 ]
    do
        if [ -w "$FilePath" ]
        then
            echo "$TAG a=$a can write" > /dev/kmsg
            return 0
        else
        echo "$TAG a=$a can't write" > /dev/kmsg
        fi
            a=$[$a+1]
    done
    return 1
}

format_ubi(){
    ubidetach -m 38
    echo "$TAG detach over" > /dev/kmsg
    echo y | ubiformat /dev/mtd38
    echo "$TAG format over" > /dev/kmsg
    ubiattach /dev/ubi_ctrl -m 38 -d 31
    echo "$TAG attach over" > /dev/kmsg
    ubimkvol /dev/ubi31 -m -N data_recover
    echo "$TAG mkvol over" > /dev/kmsg
    mount -t ubifs /dev/ubi31_0 /STATE
}

mount_read_only(){
    echo "$TAG: umount start" > /dev/kmsg

    umount /usr/lib/tmpfiles.d
    umount /etc/firmware
    umount /etc/syslog
    umount /home
    umount /mnt/sncfg
    umount /mnt/vendor/nvram
    umount /mnt 
    umount /srv 
    umount /usr/local 
    umount /system/etc/firmware 
    umount /custom/etc/firmware 
    umount /var/lib 
    umount /data
    umount /system/etc/tele 
    umount /system/etc/adr
    umount /system/etc/agps 
    umount /system/etc/security 
    umount /etc/adr 
    umount /etc/gnss
    umount /usr/share/gps 
    umount /etc/thermal 
    umount /etc/config
    umount /STATE

    echo "$TAG: after umount" > /dev/kmsg
    mount | grep "ubi1" > /dev/kmsg
    #some directories are not umount
    if [ $(mount | grep "ubi1") != "" ];
        then echo "$TAG: umount Fail!" > /dev/kmsg
        reboot
        exit
    fi
    format_ubi

}

#you.chen@2023.07.11 add for rndis check start
check_copy_file() {
	if [ $# -ne 2 ];then
		return
	fi
	
	diff $1 $2 > /dev/null
	if [ $? -ne 0 ]; then
		echo "cp $1 $2" > /dev/kmsg
		cp $1 $2
	fi
}
#you.chen@2023.07.11 add for rndis check start

recover_tmp_conf()
{
    FIND_FILE="/usr/lib/tmpfiles.d/tmp.conf"
    FIND_STR="x /tmp/ap_speech"

    if [ `grep -c "$FIND_STR" $FIND_FILE` -ne '0' ];then
        echo "This str exit" > /dev/kmsg
    else
        echo "$FIND_STR" >> "$FIND_FILE"
    fi 
    FIND_STR2="x /tmp/audio_ctrl_service"
    if [ `grep -c "$FIND_STR2" $FIND_FILE` -ne '0' ];then
        echo "This str2 exit" > /dev/kmsg
    else
        echo "$FIND_STR2" >> "$FIND_FILE"
    fi 
    FIND_STR3="x /tmp/libmodem-afe-ctrl"
    if [ `grep -c "$FIND_STR3" $FIND_FILE` -ne '0' ];then
        echo "This str3 exit" > /dev/kmsg
    else
        echo "$FIND_STR3" >> "$FIND_FILE"
    fi 
    FIND_STR4="x /tmp/tel_demo_single_proc.lock"
    if [ `grep -c "$FIND_STR4" $FIND_FILE` -ne '0' ];then
        echo "This str4 exit" > /dev/kmsg
    else
        echo "$FIND_STR4" >> "$FIND_FILE"
    fi
}

cd /
#recover some files start
echo "$TAG: Before recover radio_property" > /dev/kmsg
rm -rf /etc/config/.*.uci*
check_copy_file /data_backup/radio_property /etc/config/radio_property
check_copy_file /data_backup/lynq_uci /etc/config/lynq_uci
mkdir -p /data/atsvc
check_copy_file /data_backup/lynq_atsvc_plugin.xml /data/atsvc/lynq_atsvc_plugin.xml
check_copy_file /data_backup/terminal_controller.conf /data/atsvc/terminal_controller.conf
echo "$TAG: After recover radio_property" > /dev/kmsg

recover_tmp_conf
#recover some files end
IFRO="$(cat /sys/class/ubi/ubi1/ro_mode)"
echo "$TAG the ro_mode:$IFRO" > /dev/kmsg

echo "$TAG ubimount:" > /dev/kmsg
echo "$(mount | grep "ubi1")" > /dev/kmsg
#if ro_mode = 1, ubi1 in read-only mode
if [ $IFRO = "1" ];
    then echo "$TAG: [Read-Only]ubi1 ro_mode is readonly" > /dev/kmsg
    mount_read_only
    cd /data_backup
    tar -zxvf userdata.tar.gz -C /STATE/
    reboot
else
    detect_read
    result_read=$?
    detect_write
    result_write=$?
    #can't read the file, the file is lost
    if [ $result_read -eq 1 ];
        then echo "$TAG: File Lost!!!" > /dev/kmsg
        exit
    fi
    #ubi1 is not in read-only mode, but no directory is mounted on ubi1
    if [ $(mount | grep "ubi1") = ""];
        then echo "$TAG: [Unmount]Unmount!!!" > /dev/kmsg
        format_ubi
        cd /data_backup
        tar -zxvf userdata.tar.gz -C /STATE/
        reboot
    else
        #ubi1 is not in read-only mode, directories mounted on ubi1, but the file can't write
        #/data is mounted in read-only mode
        if [ $result_write -eq 1 ];
            then echo "$TAG: [Read-only]/data is Readonly!!!" > /dev/kmsg
            mount_read_only
            cd /data_backup
            tar -zxvf userdata.tar.gz -C /STATE/
            reboot
        fi
    fi
fi
echo "$TAG === Initial: LYNQ MOUNT Abnomal End ===" > /dev/kmsg


a_int=$(df | grep '/data$' | awk '{print $4}')
b_int=1024

   if [ $a_int -eq $b_int ];then
       echo "$a_int = $b_int"
   elif [ $a_int -lt $b_int ];then
       find /data -name '*.pcap' | xargs rm -rf
       sleep 0.02
       find /data -name '*.cap' | xargs rm -rf
       sleep 0.02
       find /data -name '*.log' | xargs rm -rf
       sleep 0.02
       c_int=$(df | grep '/data$' | awk '{print $4}')

       if [ $c_int -eq $b_int ];then
	      echo "$c_int = $b_int"
       elif [ $c_int -lt $b_int ];then
          echo '#/bin/sh' > /tmp/delete_partition.sh
          echo 'cd /data' >> /tmp/delete_partition.sh
          echo 'GLOBIGNORE=agps_sup1:.update_status:atsvc:ccci_cfg:core:gpio:gps_mnl:lynq-low-power.sh:mdlog:misc:mpe_mnl:nvram:ril:vendor:lynq-test.sh:wifi:delete_partition.sh' >> /tmp/delete_partition.sh

          echo 'rm -rf *' >>  /tmp/delete_partition.sh
          cd /tmp
          chmod 777 delete_partition.sh
          /tmp/delete_partition.sh
          rm -rf delete_partition.sh
       else
          echo "$c_int > $b_int"
       fi
     
    else
    echo "userdata avalil size is $a_int"
    fi

