#!/bin/bash

para="$1"

case ${para} in
    pre)
        cd /userdata
        rm -rf /userdata/*
	    sync

        [ ! -d /userdata/upgrade ] && mkdir -p /userdata/upgrade
		[ ! -d /userdata/recovery ] && mkdir -p /userdata/recovery
		[ ! -d /userdata/lost+found ] && mkdir -p /userdata/lost+found
		[ ! -f /userdata/ispeaker.log ] && touch /userdata/ispeaker.log
		[ ! -f /userdata/tmp_state.conf ] && touch /userdata/tmp_state.conf
		[ ! -f /userdata/.pppoe_status ] && touch /userdata/.pppoe_status
        cat << END > /userdata/tmp_state.conf
[intercom]
onekey_state=idle
END

        model="`sysconf /etc/speaker.conf get system ui_model`"
		[ ! -d /userdata/cfg/eq_bin ] && mkdir -p /userdata/cfg/eq_bin
		if [ ! -z ${model} ];then
			if [ "foo${model}" = "fooSW15" -o "foo${model}" = "fooSW16" ];then
				cp /oem/cfg/eq_bin/SW15.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			elif [ "foo${model}" = "fooSC15" -o "foo${model}" = "fooSW16" ];then
				cp /oem/cfg/eq_bin/SC15.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			elif [ "foo${model}" = "fooSH30" ];then
				cp /oem/cfg/eq_bin/SH30.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			elif [ "foo${model}" = "fooSL50" ];then
				cp /oem/cfg/eq_bin/SL50.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			elif [ "foo${model}" = "fooSL30" ];then
				cp /oem/cfg/eq_bin/SL30.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			elif [ "foo${model}" = "fooSC10" ];then
				cp /oem/cfg/eq_bin/SC10.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
            elif [ "foo${model}" = "fooSH10" ];then
				cp /oem/cfg/eq_bin/SH30.bin /userdata/cfg/eq_bin/Para_48000Hz_2ch.bin
			else
				rm -rf /userdata/cfg/eq_bin/*
			fi
		else
			rm -rf /userdata/cfg/eq_bin/*
		fi
        sync
    ;;
    *)
        sync
        [ ! -f ${para} ] && exit 1
        model="`/etc/scripts/getmodel.sh`"
        [ -z ${model} ] && exit 1
        /bin/rk_parser ${para} ${model}
        recode_check="$?"
        if [ "foo${recode_check}" != "foo0" ];then
            rm -rf ${para}
            sync
            exit 1
        fi
        sync

        /etc/scripts/shell_action.sh showReboot
        reset_default="`sysconf /etc/speaker.conf get upgrade reset_default`"
        updatetime="`sysconf /etc/speaker.conf get upgrade date`"

        if [ ! -f /userdata/updatetime.txt ];then
            touch /userdata/updatetime.txt
            cat << END > /userdata/updatetime.txt
[upgrade]
date=2019-12-02
END
        fi

        sysconf /userdata/updatetime.txt set upgrade date ${updatetime}

        if [ "foo${reset_default}" = "foono" ];then
            rm -rf /oem/.upgrade
            #/usr/bin/recoverySystem ota ${para}
            updateEngine --image_url=${para} --misc=update --savepath=${para}  --reboot
            recode="$?"
            if [ "${recode}" = 255 ];then
                touch /oem/.upgrade
                rm -rf ${para}
                sync
                exit 1
            fi
        else
            rm -rf /oem/.userdata
            #/usr/bin/recoverySystem ota ${para}
            updateEngine --image_url=${para} --misc=update --savepath=${para}  --reboot
            recode="$?"
            if [ "${recode}" = 255 ];then
                touch /oem/.userdata
                rm -rf ${para}
                sync
                exit 1
            fi
        fi
esac

sync

exit 0