if [ -e $(dirname $0)/images/anti_version.txt ]; then
CURRENT_ANTI_VER=`cat $(dirname $0)/images/anti_version.txt`
fi
if [ -z "$CURRENT_ANTI_VER" ]; then CURRENT_ANTI_VER=0; fi
ver=`fastboot $* getvar anti 2>&1 | grep -oP "anti: \K[0-9]+"`
if [ -z "$ver" ]; then ver=0; fi
#if [ $ver -gt $CURRENT_ANTI_VER ]; then echo "Current device antirollback version is greater than this pakcage"; exit 1; fi
fastboot $* getvar product 2>&1 | grep "xaga"
if [ $? -ne 0 ] ; then echo "Missmatching image and device"; exit 1; fi
SCRIPT_PATH=$(dirname $0)
handle_error(){
    echo $@
    exit 2;
}
fastboot $* getvar crc 2>&1 | grep "^crc: 1"
if [ $? -eq 0 ]; then
fastboot $* flash crclist       ${SCRIPT_PATH}/images/crclist.txt       || handle_error flash crclist error
fastboot $* flash sparsecrclist ${SCRIPT_PATH}/images/sparsecrclist.txt || handle_error flash sparsecrclist error
fi

fastboot $* erase boot_ab  || handle_error erase boot_ab error
fastboot $* erase expdb    || handle_error erase expdb error

fastboot $* flash preloader1       ${SCRIPT_PATH}/images/preloader_xaga.bin || handle_error flash preloader1 error
fastboot $* flash preloader2       ${SCRIPT_PATH}/images/preloader_xaga.bin || handle_error flash preloader2 error
fastboot $* flash vbmeta_ab        ${SCRIPT_PATH}/images/vbmeta.img         || handle_error flash vbmeta_ab error
fastboot $* flash vbmeta_system_ab ${SCRIPT_PATH}/images/vbmeta_system.img  || handle_error flash vbmeta_system_ab error
fastboot $* flash vbmeta_vendor_ab ${SCRIPT_PATH}/images/vbmeta_vendor.img  || handle_error flash vbmeta_vendor_ab error
fastboot $* flash md1img_ab        ${SCRIPT_PATH}/images/md1img.img         || handle_error flash md1img_ab error
fastboot $* flash spmfw_ab         ${SCRIPT_PATH}/images/spmfw.img          || handle_error flash spmfw_ab error
fastboot $* flash mcf_ota_ab       ${SCRIPT_PATH}/images/mcf_ota.img        || handle_error flash mcf_ota_ab error
fastboot $* flash audio_dsp_ab     ${SCRIPT_PATH}/images/audio_dsp.img      || handle_error flash audio_dsp_ab error
fastboot $* flash pi_img_ab        ${SCRIPT_PATH}/images/pi_img.img         || handle_error flash pi_img_ab error
fastboot $* flash dpm_ab           ${SCRIPT_PATH}/images/dpm.img            || handle_error flash dpm_ab error
fastboot $* flash scp_ab           ${SCRIPT_PATH}/images/scp.img            || handle_error flash scp_ab error
fastboot $* flash ccu_ab           ${SCRIPT_PATH}/images/ccu.img            || handle_error flash ccu_ab error
fastboot $* flash vcp_ab           ${SCRIPT_PATH}/images/vcp.img            || handle_error flash vcp_ab error
fastboot $* flash sspm_ab          ${SCRIPT_PATH}/images/sspm.img           || handle_error flash sspm_ab error
fastboot $* flash mcupm_ab         ${SCRIPT_PATH}/images/mcupm.img          || handle_error flash mcupm_ab error
fastboot $* flash gpueb_ab         ${SCRIPT_PATH}/images/gpueb.img          || handle_error flash gpueb_ab error
fastboot $* flash apusys_ab        ${SCRIPT_PATH}/images/apusys.img         || handle_error flash apusys_ab error
fastboot $* flash mvpu_algo_ab     ${SCRIPT_PATH}/images/mvpu_algo.img      || handle_error flash mvpu_algo_ab error
fastboot $* flash gz_ab            ${SCRIPT_PATH}/images/gz.img             || handle_error flash gz_ab error
fastboot $* flash lk_ab            ${SCRIPT_PATH}/images/lk.img             || handle_error flash lk_ab error
fastboot $* flash vendor_boot_ab   ${SCRIPT_PATH}/images/vendor_boot.img    || handle_error flash vendor_boot_ab error
fastboot $* flash dtbo_ab          ${SCRIPT_PATH}/images/dtbo.img           || handle_error flash dtbo_ab error
fastboot $* flash tee_ab           ${SCRIPT_PATH}/images/tee.img            || handle_error flash tee_ab error
fastboot $* flash logo_ab          ${SCRIPT_PATH}/images/logo.bin           || handle_error flash logo_ab error
fastboot $* flash super            ${SCRIPT_PATH}/images/super.img          || handle_error flash super error
fastboot $* flash rescue           ${SCRIPT_PATH}/images/rescue.img         || handle_error flash rescue error
fastboot $* flash cust             ${SCRIPT_PATH}/images/cust.img           || handle_error flash cust error
fastboot $* flash boot_ab          ${SCRIPT_PATH}/images/boot.img           || handle_error flash boot_ab error
fastboot $* oem cdms
fastboot $* set_active a  || handle_error set_active a error

fastboot $* reboot
if [ $? -ne 0 ] ; then echo "Reboot error"; exit 1; fi
exit 0

