#!/bin/sh

BACKGROUND=0
MODEMD="modemd"

DEFAULT_XML_PATH="/tmp/esim_shell_profiles_operation.xml"
ESIM_PROFILES_OPER_LOG="/nvm/esim_profiles_operation.log"
ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH="/tmp/lpac_esim/esim_lpac_download_response.json"
ESIM_LPAC_ENABLE_RESPONSE_JSON_PATH="/tmp/lpac_esim/esim_lpac_enable_response.json"
ESIM_LPAC_DISABLE_RESPONSE_JSON_PATH="/tmp/lpac_esim/esim_lpac_disable_response.json"
ESIM_LPAC_DELETE_RESPONSE_JSON_PATH="/tmp/lpac_esim/esim_lpac_delete_response.json"
ESIM_LPAC_NOTIFY_RESPONSE_JSON_PATH="/tmp/lpac_esim/esim_lpac_notify_response.json"

AC_CODE_MAX_LEN=256
PARAM_MAX_LEN=128
ICCID_MAX_LEN=20
ICCID_MIN_LEN=19

#300s
TASK_TIMEOUT=300

verify_string() {
    local tmp_param="${1}"

    if [ -n $tmp_param ] && [ ${#tmp_param} -le $PARAM_MAX_LEN ] \
       && [ -z "$(echo $tmp_param | grep -E '<\\s*/?\\s*script\\s*.*>|eval\\s*\\(.*\\)|expression\\s*\\(.*\\)|javascript\\s*:|vbscript\\s*:|[\\`\\$\\;\\&\\|]|\"*.\\s*(on(dbl)?click|onmouse.+|ondrag.*|ondrop|oncopy)\\s*=.*[>//]')" ]; then
        return 0
    else
        return 1
    fi
}

verify_encrypt_string() {
    local tmp_param="${1}"

    if [ -n $tmp_param ] && [ ${#tmp_param} -le $PARAM_MAX_LEN ]; then
        return 0
    else
        return 1
    fi
}

verify_bool_string() {
    local tmp_param="${1}"

    if [ -n $tmp_param ] && [ $tmp_param = "true" -o $tmp_param = "false" ]; then
        return 0
    else
        return 1
    fi
}

verify_iccid_string() {
    local tmp_param="${1}"

    if [ -n $tmp_param ] && [ ${#tmp_param} -ge $ICCID_MIN_LEN ] && [ ${#tmp_param} -le $ICCID_MAX_LEN ]; then
        return 0
    else
        return 1
    fi
}

get_cur_profiles_cnt() {
    local cur_cnt=0

    cur_cnt="$(cat /var/config/esim 2>/dev/null | grep -cw 'profiles {' 2>/dev/null)"
    case "$cur_cnt" in
        ''|*[!0-9]*)
            cur_cnt=0
            ;;
        *)
            cur_cnt=$((cur_cnt))
            ;;
    esac

    echo $cur_cnt
}

wait_task_completed() {
    local wait_max=$TASK_TIMEOUT
    local progress=1
    local fail_reason=""
    local last_log_line=0
    local cur_log_line=0

    if [ "$(renv get esim_operation_status 2>/dev/null)" != "pending" ]; then
        if [ "$(renv get esim_operation_status 2>/dev/null)" = "fail" ]; then
            renv set esim_operation_progress 100 >/dev/null 2>&1
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            echo "Failed to $1 eSIM profile."
            return 1
        fi

        if [ "$(renv get esim_operation_status 2>/dev/null)" = "success" ]; then
            renv set esim_operation_progress 100 >/dev/null 2>&1
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            echo "$1 eSIM profile successfully."
            return 0
        fi

        return 1
    fi

    renv set esim_operation_progress $progress >/dev/null 2>&1

    last_log_line="$(wc -l $ESIM_PROFILES_OPER_LOG 2>/dev/null | awk '{print $1}' 2>/dev/null)"
    case "$last_log_line" in
        ''|*[!0-9]*)
            last_log_line=0
            ;;
        *)
            last_log_line=$((last_log_line))
            ;;
    esac

    while [ $((wait_max--)) -gt 0 ]
    do
        if [ "$(renv get esim_operation_status 2>/dev/null)" = "fail" ]; then
            renv set esim_operation_progress 100 >/dev/null 2>&1
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1

            cur_log_line="$(wc -l $ESIM_PROFILES_OPER_LOG 2>/dev/null | awk '{print $1}' 2>/dev/null)"
            case "$cur_log_line" in
                ''|*[!0-9]*)
                    cur_log_line=0
                    ;;
                *)
                    cur_log_line=$((cur_log_line))
                    ;;
            esac

            fail_reason="$(tail -n 1 $ESIM_PROFILES_OPER_LOG 2>/dev/null | awk -F ';' '{print $4}')"
            if [ $cur_log_line -gt $last_log_line ] && [ -n "$fail_reason" ] && [ "$fail_reason" != "Success" ]; then
                echo "$fail_reason"
            else
                echo "Failed to $1 eSIM profile."
            fi

            return 1
        fi

        if [ "$(renv get esim_operation_status 2>/dev/null)" = "success" ]; then
            renv set esim_operation_progress 100 >/dev/null 2>&1
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            echo "$1 eSIM profile successfully."
            return 0
        fi

        if [ "$1" = "download" ]; then
            progress=$((progress+1))
            if [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es10b_get_euicc_challenge_and_info' 2>/dev/null)" ] && [ $progress -lt 5 ]; then
                progress=5
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es9p_initiate_authentication' 2>/dev/null)" ] && [ $progress -lt 10 ]; then
                progress=10
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es10b_authenticate_server' 2>/dev/null)" ] && [ $progress -lt 15 ]; then
                progress=15
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es9p_authenticate_client' 2>/dev/null)" ] && [ $progress -lt 20 ]; then
                progress=20
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es8p_meatadata_parse' 2>/dev/null)" ] && [ $progress -lt 25 ]; then
                progress=25
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es10b_prepare_download' 2>/dev/null)" ] && [ $progress -lt 30 ]; then
                progress=30
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es9p_get_bound_profile_package' 2>/dev/null)" ] && [ $progress -lt 35 ]; then
                progress=35
            elif [ -n "$(cat $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH 2>/dev/null | grep 'es10b_load_bound_profile_package' 2>/dev/null)" ] && [ $progress -lt 40 ]; then
                progress=40
            fi
        elif [ "$1" = "enable" ]; then
            progress=$((progress+2))
        elif [ "$1" = "disable" ]; then
            progress=$((progress+2))
        elif [ "$1" = "delete" ]; then
            progress=$((progress+2))
        else
            progress=$((progress+1))
        fi

        if [ $progress -le 98 ]; then
            renv set esim_operation_progress $progress >/dev/null 2>&1
        fi

        sleep 1
    done

    renv set esim_operation_status fail >/dev/null 2>&1
    renv set esim_operation_progress 100 >/dev/null 2>&1
    renv del esim_operation_notify >/dev/null 2>&1
    renv del esim_operation_file_path >/dev/null 2>&1

    echo "Failed to $1 eSIM profile."
    return 1
}

clean_esim_renv() {
    renv del esim_operation_status >/dev/null 2>&1
    renv del esim_operation_method >/dev/null 2>&1
    renv del esim_operation_progress >/dev/null 2>&1
    renv del esim_operation_notify >/dev/null 2>&1
    renv del esim_operation_file_path >/dev/null 2>&1
}

download_profile() {
    local activation_code=""
    local download_as_enabled="false"
    local nickname=""
    local auto_apn="true"
    local apn_polling="false"
    local apn="internet"
    local username=""
    local password=""
    local auth_type="none"
    local tmp_param=""

    if [ $# -eq 0 ]; then
        show_usage
        return 1
    fi

    clean_esim_renv
    rm -f $ESIM_LPAC_DOWNLOAD_RESPONSE_JSON_PATH >/dev/null 2>&1

    if [ "$(uci get devcfg.dual_modem_support 2>/dev/null)" = "true" ]; then
        MODEMD="modemd2"
    fi

    if [ -n "$(echo "${1}" | grep '\.xml' 2>/dev/null)" ]; then
        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method download >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path "${1}" >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path "${1}" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to download eSIM profile."
            return 1
        fi
    else
        while [ $# -gt 0 ]
        do
            case "${1}" in
                "download_as_enabled")
                    shift
                    verify_bool_string "${1}"
                    if [ $? -eq 0 ]; then
                        download_as_enabled="${1}"
                    else
                        echo "Invalid download_as_enabled"
                        return 1
                    fi
                    ;;
                "nickname")
                    shift
                    verify_string "${1}"
                    if [ $? -eq 0 ]; then
                        nickname="${1}"
                    else
                        echo "Invalid nickname"
                        return 1
                    fi
                    ;;
                "auto_apn")
                    shift
                    verify_bool_string "${1}"
                    if [ $? -eq 0 ]; then
                        auto_apn="${1}"
                    else
                        echo "Invalid auto_apn"
                        return 1
                    fi
                    ;;
                "apn_polling")
                    shift
                    verify_bool_string "${1}"
                    if [ $? -eq 0 ]; then
                        apn_polling="${1}"
                    else
                        echo "Invalid apn_polling"
                        return 1
                    fi
                    ;;
                "apn")
                    shift
                    verify_string "${1}"
                    if [ $? -eq 0 ]; then
                        apn="${1}"
                    else
                        echo "Invalid apn"
                        return 1
                    fi
                    ;;
                "username")
                    shift
                    verify_string "${1}"
                    if [ $? -eq 0 ]; then
                        username="${1}"
                    else
                        echo "Invalid username"
                        return 1
                    fi
                    ;;
                "password")
                    shift
                    verify_encrypt_string "${1}"
                    if [ $? -eq 0 ]; then
                        password="${1}"
                    else
                        echo "Invalid password"
                        return 1
                    fi
                    ;;
                "auth_type")
                    shift
                    if [ -n "${1}" ] && [ "${1}" = "none" -o "${1}" = "chap" -o "${1}" = "pap" ]; then
                        auth_type="${1}"
                    else
                        echo "Invalid auth_type"
                        return 1
                    fi
                    ;;
                *)
                    tmp_param="${1}"
                    if [ -n $tmp_param ] && [ ${#tmp_param} -le $AC_CODE_MAX_LEN ] && [ -n "$(echo $tmp_param | grep -E '^(LPA:)?1\$[^$]+\$[^$]*(\$[^$]*)?(\$[^$]*)?$')" ]; then
                        activation_code=$tmp_param
                    else
                        echo "Invalid activation_code"
                        return 1
                    fi
                    ;;
            esac

            shift
        done

        if [ -z "$activation_code" ]; then
            echo "Invalid activation_code"
            return 1
        fi

        cat > $DEFAULT_XML_PATH <<EOF
<esim>
    <profiles>
        <action>download</action>
        <activation_code>$activation_code</activation_code>
        <download_as_enabled>$download_as_enabled</download_as_enabled>
        <nickname>$nickname</nickname>
        <auto_apn>$auto_apn</auto_apn>
        <apn_polling>$apn_polling</apn_polling>
        <apn>$apn</apn>
        <username>$username</username>
        <password>$password</password>
        <auth_type>$auth_type</auth_type>
    </profiles>
</esim>
EOF

        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method download >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path $DEFAULT_XML_PATH >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path $DEFAULT_XML_PATH >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to download eSIM profile."
            return 1
        fi
    fi

    if [ $BACKGROUND -ne 1 ]; then
        esim_mgmt wait download
        return $?
    else
        esim_mgmt wait download >/dev/null 2>&1 &
        return 0
    fi
}

enable_profile() {
    local iccid=""

    if [ $# -eq 0 ]; then
        show_usage
        return 1
    fi

    clean_esim_renv

    if [ "$(uci get devcfg.dual_modem_support 2>/dev/null)" = "true" ]; then
        MODEMD="modemd2"
    fi

    if [ -n "$(echo "${1}" | grep '\.xml' 2>/dev/null)" ]; then
        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method enable >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path "${1}" >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path "${1}" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to enable eSIM profile."
            return 1
        fi
    else
        verify_iccid_string "${1}"
        if [ $? -eq 0 ]; then
            iccid="${1}"
        else
            echo "Invalid iccid"
            return 1
        fi

        cat > $DEFAULT_XML_PATH <<EOF
<esim>
    <profiles>
        <action>enable</action>
        <iccid>$iccid</iccid>
    </profiles>
</esim>
EOF

        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method enable >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path $DEFAULT_XML_PATH >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path $DEFAULT_XML_PATH >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to enable eSIM profile."
            return 1
        fi
    fi

    if [ $BACKGROUND -ne 1 ]; then
        esim_mgmt wait enable
        return $?
    else
        esim_mgmt wait enable >/dev/null 2>&1 &
        return 0
    fi
}

disable_profile() {
    local iccid=""

    if [ $# -eq 0 ]; then
        show_usage
        return 1
    fi

    clean_esim_renv

    if [ "$(uci get devcfg.dual_modem_support 2>/dev/null)" = "true" ]; then
        MODEMD="modemd2"
    fi

    if [ -n "$(echo "${1}" | grep '\.xml' 2>/dev/null)" ]; then
        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method disable >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path "${1}" >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path "${1}" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to disable eSIM profile."
            return 1
        fi
    else
        verify_iccid_string "${1}"
        if [ $? -eq 0 ]; then
            iccid="${1}"
        else
            echo "Invalid iccid"
            return 1
        fi

        cat > $DEFAULT_XML_PATH <<EOF
<esim>
    <profiles>
        <action>disable</action>
        <iccid>$iccid</iccid>
    </profiles>
</esim>
EOF

        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method disable >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path $DEFAULT_XML_PATH >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path $DEFAULT_XML_PATH >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to disable eSIM profile."
            return 1
        fi
    fi

    if [ $BACKGROUND -ne 1 ]; then
        esim_mgmt wait disable
        return $?
    else
        esim_mgmt wait disable >/dev/null 2>&1 &
        return 0
    fi
}

delete_profile() {
    local iccid=""

    if [ $# -eq 0 ]; then
        show_usage
        return 1
    fi

    if [ -n "$(usi get system.device_model 2>/dev/null | grep EG5101 2>/dev/null)" ] && [ $(get_cur_profiles_cnt) -eq 1 ]; then
        echo "Deleting the last profile is not allowed."
        return 1
    fi

    clean_esim_renv

    if [ "$(uci get devcfg.dual_modem_support 2>/dev/null)" = "true" ]; then
        MODEMD="modemd2"
    fi

    if [ -n "$(echo "${1}" | grep '\.xml' 2>/dev/null)" ]; then
        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method delete >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path "${1}" >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path "${1}" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to delete eSIM profile."
            return 1
        fi
    else
        verify_iccid_string "${1}"
        if [ $? -eq 0 ]; then
            iccid="${1}"
        else
            echo "Invalid iccid"
            return 1
        fi

        if [ "$(usi get esim.enabled_profile_iccid 2>/dev/null)" = "$iccid" ]; then
            echo "The profile is already enabled. To delete it, you need to disable it first."
            return 1
        fi

        cat > $DEFAULT_XML_PATH <<EOF
<esim>
    <profiles>
        <action>delete</action>
        <iccid>$iccid</iccid>
    </profiles>
</esim>
EOF

        renv set esim_operation_status pending >/dev/null 2>&1
        renv set esim_operation_progress 1 >/dev/null 2>&1
        renv set esim_operation_method delete >/dev/null 2>&1
        renv set esim_operation_notify true >/dev/null 2>&1
        renv set esim_operation_file_path $DEFAULT_XML_PATH >/dev/null 2>&1
        rmsg $MODEMD cmd esim_lpa action operation operation_xml_path $DEFAULT_XML_PATH >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            renv del esim_operation_notify >/dev/null 2>&1
            renv del esim_operation_file_path >/dev/null 2>&1
            renv set esim_operation_status fail >/dev/null 2>&1
            renv set esim_operation_progress 100 >/dev/null 2>&1
            echo "Failed to delete eSIM profile."
            return 1
        fi
    fi

    if [ $BACKGROUND -ne 1 ]; then
        esim_mgmt wait delete
        return $?
    else
        esim_mgmt wait delete >/dev/null 2>&1 &
        return 0
    fi
}

check_profiles_cnt_limit() {
    local max_cnt=0
    local cur_cnt=0

    max_cnt="$(uci get esim.profiles.max_entry_num 2>/dev/null)"
    case "$max_cnt" in
        ''|*[!0-9]*)
            max_cnt=0
            ;;
        *)
            max_cnt=$((max_cnt))
            ;;
    esac

    cur_cnt=$(get_cur_profiles_cnt)

    if [ $max_cnt -gt 0 ] && [ $cur_cnt -gt 0 ] && [ $cur_cnt -ge $max_cnt ]; then
        echo "The profile limit has been reached, no new profiles can be downloaded."
        return 1
    fi

    return 0
}

check_task_pending() {
    if [ "$(renv get esim_operation_status 2>/dev/null)" != "pending" ]; then
        return 0
    fi

    if [ "$(renv get esim_operation_method 2>/dev/null)" = "download" ]; then
        echo "The previous download task has not been completed. Please try again later."
    elif [ "$(renv get esim_operation_method 2>/dev/null)" = "enable" ]; then
        echo "The previous enable task has not been completed. Please try again later."
    elif [ "$(renv get esim_operation_method 2>/dev/null)" = "disable" ]; then
        echo "The previous disable task has not been completed. Please try again later."
    elif [ "$(renv get esim_operation_method 2>/dev/null)" = "delete" ]; then
        echo "The previous delete task has not been completed. Please try again later."
    else
        echo "The previous task has not been completed. Please try again later."
    fi

    return 1
}

check_esim_support() {
    if [ "$(usi get uci.esim_support 2>/dev/null)" != "true" ]; then
        echo "eSIM management is inactive. Please ensure the eSIM card is inserted correctly and try rebooting the device."
        return 1
    fi

    return 0
}

show_usage() {
    cat <<EOF
Usage: esim_mgmt [<options>] <command> [<arguments> <values>]

commands:
    download    <xml> / <activation_code> [<arguments> <values>] (e.g. esim_mgmt download xml_path, esim_mgmt download activation_code download_as_enabled false nickname name auto_apn true...)
    enable      <xml> / <iccid> (e.g. esim_mgmt enable xml_path, esim_mgmt enable iccid)
    disable     <xml> / <iccid> (e.g. esim_mgmt disable xml_path, esim_mgmt disable iccid)
    delete      <xml> / <iccid> (e.g. esim_mgmt delete xml_path, esim_mgmt delete iccid)

options:
    -h          display this help and exit
    -b          Background Execution
EOF
}

if [ $# -eq 0 ]; then
    show_usage
    exit 1
fi

while [ $# -gt 0 ]
do
    case "${1}" in
        "-h")
            show_usage
            exit 0
            ;;
        "-b")
            BACKGROUND=1;
            ;;
        "download")
            check_esim_support
            if [ $? -ne 0 ]; then
                exit 1
            fi

            check_task_pending
            if [ $? -ne 0 ]; then
                exit 1
            fi

            check_profiles_cnt_limit
            if [ $? -ne 0 ]; then
                exit 1
            fi

            shift
            download_profile $@
            exit $?
            ;;
        "enable")
            check_esim_support
            if [ $? -ne 0 ]; then
                exit 1
            fi

            check_task_pending
            if [ $? -ne 0 ]; then
                exit 1
            fi

            shift
            enable_profile $@
            exit $?
            ;;
        "disable")
            check_esim_support
            if [ $? -ne 0 ]; then
                exit 1
            fi

            check_task_pending
            if [ $? -ne 0 ]; then
                exit 1
            fi

            shift
            disable_profile $@
            exit $?
            ;;
        "delete")
            check_esim_support
            if [ $? -ne 0 ]; then
                exit 1
            fi

            check_task_pending
            if [ $? -ne 0 ]; then
                exit 1
            fi

            shift
            delete_profile $@
            exit $?
            ;;
        "wait")
            check_esim_support
            if [ $? -ne 0 ]; then
                exit 1
            fi

            shift
            case "${1}" in
                "download")
                    wait_task_completed download
                    exit $?
                    ;;
                "enable")
                    wait_task_completed enable
                    exit $?
                    ;;
                "disable")
                    wait_task_completed disable
                    exit $?
                    ;;
                "delete")
                    wait_task_completed delete
                    exit $?
                    ;;
                *)
                    exit 1
                    ;;
            esac
            ;;
        *)
            show_usage
            exit 1
            ;;
    esac

    shift
done

