#!/bin/sh
folder_path="/tmp/replace_path"
pattern="|>>|"
myFile=/tmp/replace.info
myFile_gz=/tmp/config.tar.gz

help() {
    cat <<_HELP_
ipk install api wrapper

Usage:  $0 -p [URL_GZ] -f [URL_INFO] -s

Example:
    $0 -p https://xxxx -f https://xxxx
_HELP_
}
ONLY_CHANGE_EMS_SN=0

while getopts "p:f:s" opt
do
    case $opt in
        p)
        URL_GZ="$2"
        shift 2
        ;;
        f)
        URL_INFO="$2"
        shift 2
        ;;
        s)
        ONLY_CHANGE_EMS_SN=1
        shift 1
        ;;
        ?)
        help
        exit 1
    esac
done

replace_sn() {
    for file in "$1"/*; do
        if [ -d "$file" ]; then
            replace_sn "$file"
        elif [ -f "$file" ] && [ "$file" != "$1/uplink_channels.json" ]; then
            sed -i "s/\[$search_text\]/$replace_text/g" "$file"
        fi
    done
}

do_change()
{
     search_text="$1"
     replace_text="$2"
     if [ "$ONLY_CHANGE_EMS_SN" == "1" ];  then
        search_path="$folder_path"/app/config/ems
        find "$search_path" -type f -exec sed -i "s/\[$search_text\]/$replace_text/g" {} \;
     else
        search_path="$folder_path"
        #find "$search_path" -type f ! -name "uplink_channels.json" -print | while read line
        #do
        #  sed -i "s/\[$search_text\]/$replace_text/g" "$line"
        #done
        replace_sn "$search_path"
     fi
     return 0
}

gc_exec()
{
    gc_flag="$1"
    rm -rf "$folder_path"
    if [ -f "$myFile_gz" ]; then
     rm "$myFile_gz"*
     echo "gz rm"
    fi
    if [ -f "$myFile" ]; then
       rm "$myFile"
    fi
    return "$gc_flag"
}

main()
{
    gc_exec 0
    mkdir "$folder_path"
  
    wget -t 2 -T 5 "$URL_GZ" -P /tmp/
    ret="$?"
    [ "$ret" != "0" ] && {
        echo $pattern 'download '$URL_GZ ' failed'
        gc_exec "$ret"
        exit 1
    }
    tar xzf "$myFile_gz" -C "$folder_path"
    ret="$?"
    [ "$ret" != "0" ] && {
        echo $pattern 'tar '$URL_GZ ' failed'
        gc_exec "$ret"
        exit 1
    }
     	
    if [ -z "$URL_INFO" ]; then
        echo 'no info file'
    else
    wget -t 2 -T 5 "$URL_INFO" -P /tmp/
    ret="$?"
    [ "$ret" != "0" ] && {
        echo $pattern 'download '$URL_INFO ' failed'
        gc_exec "$ret"
        exit 1
    }
    cat $myFile | while read line
    do
#   echo "$line"
    st=$(echo "$line" | awk '{print $1}')
    rt=$(echo "$line" | awk '{print $2}')
    if [ -n "$st" -a -n "$rt" ];  then
         do_change "$st" "$rt"
    fi 
    done
    fi

    echo "do change SN"

    SN_BASE=$(/lib/dyiot/bin/factory get | grep -i 'SN=' | awk -F '=' '{print $2}')
    first=$(echo "$SN_BASE" | awk '{printf substr($0,1,5)}')
    second=$(echo "$SN_BASE" | awk '{printf substr($0,7)}')
    rst=$(echo "$SN_BASE" | awk '{printf substr($0,6,1)}')
    if [ "$rst" == "D" ]; then
       SN_CHANGE=$(echo "$first""E""$second")
    else
       SN_CHANGE="$SN_BASE"
    fi

    do_change "GW_SN" "$SN_BASE"
    do_change "GW_6UL_SN" "$SN_CHANGE"

    cp "$folder_path"/. /. -R
    ret="$?"
    [ "$ret" != "0" ] && {
        echo $pattern 'replace failed'
        gc_exec "$ret"
        exit 1
    }
    echo $pattern 'replace success'
    gc_exec 0

}

main
