#! /bin/bash

if [ $# = 1 ];then
    branch_name=$1
    if [ "foo${branch_name}" != "foomain" ];then
        echo "Branch name not found!"
        exit 0
    fi

    #modify build date
    build_date="`date \"+%Y-%m-%d\"`"
    cd oem/etc && awk -v new_date="$build_date" '/^\[upgrade\]/{f=1} f==1 && /^date=/{sub(/=.*/, "=" new_date); f=0} 1' speaker.conf > temp.conf && mv speaker.conf ../../speaker.conf.bak && mv temp.conf speaker.conf

    cd ../ && tar cpf ../oem_backup.tar . && cd ../

    date="`date \"+%Y%m%d\"`"
    SOFT_VERSION="`grep firmware oem/etc/speaker.conf | cut -d '=' -f2`"

    name="u_m2_g2_fs_v_${SOFT_VERSION}_d_${date}.img"

    [ ! -d output ] && mkdir output

    tar -zcf output/${name} oem_backup.tar etc oem usr www app && echo "Wirte Sign..." && /bin/rk_generate output/${name} G2-DIA ${SOFT_VERSION} && echo "Wirte Sign complete!"

    rm -rf oem_backup.tar

    mv speaker.conf.bak oem/etc/speaker.conf

    echo "The upgrade package has been generated in the output/${name}"
else
    echo "usage:$0 branch-name"
fi

exit 0