#!/bin/bash

TARGET_KEY="AP_VERSION_ID"

# read /usr/lib/os-release judge $TARGET_KEY is exit?  if exit,then return
while read line
do
#        echo $line
        line_key=${line%%=*}
        if [[ ${line_key} = ${TARGET_KEY} ]]; then
                echo "${TARGET_KEY} is exits"
        fi

done < /usr/lib/os-release

for str1 in `cat /usr/lib/os-release`
do
    #echo $str1
    ap_version_id=$(echo $str1|grep 'AP_VERSION_ID'|awk '{print $1}')
    echo $ap_version_id
done

for str in `cat /proc/cmdline`
do
    # echo $str
    key=${str%%=*}
    if [[ $key = ${TARGET_KEY} ]] || [[ ${ap_version_id} != $str  ]]; then
    grep "AP_VERSION_ID" /usr/lib/os-release >/dev/null
        if [ $? -eq 0 ]; then
        sed -i "s/^.*AP_VERSION_ID.*$/$str/g" /usr/lib/os-release
            if [ $? -ne 0 ]; then
                echo "FAILED"
                exit 1
            fi
            exit 0
        else
        echo ${str} >> /usr/lib/os-release
        exit 1
        fi
    fi
done

