#!/bin/bash

#remounts / for writing
function unlock_fs()
{
    echo -n "Remounting / for writing... "
    if ! mount -o remount,rw /; then
        echo "Unable to mount / for writing, exiting..." >&2
        exit 1
    else
        echo "OK"
    fi
}

# remounts / for read-only
function lock_fs()
{
    echo -n "Remounting / for read-only... "
    if ! mount -o remount,ro /; then
        echo "Could not remount / for read only" >&2
    else
        echo "OK"
    fi
}

function update_confile()
{
    # check whether the key:value pair is already in the config
    if grep -q "^${1}:.*${2}" "$UNIPI_LTE_CONFIG"; then
        # yes it is, nothing to update
        echo "The key \"$1\" with value $2 is already present"
        return 0
    fi
    
    # the key:value pair is not present or active, we should change the config

    # check whether the key is there, even commented
    if grep -q "^;*${1}:" "$UNIPI_LTE_CONFIG"; then
        # the key is there, we will mangle its value and leave the comment, if there is any
        sed -i "s/^;*${1}:[^;]*/${1}: ${2} /I" "$UNIPI_LTE_CONFIG"
        echo "The key $1 is already present, but with different value or commented out => changing"
    else
        # the key is not present at all, add it after start of the [MAIN] section
        sed -i "/\\[MAIN\\]/a ${1}: ${2}" "$UNIPI_LTE_CONFIG"
        echo "The key $1 is not present at all => adding"
    fi

    return 1
}

function force_comment()
{
    # if the parameter is uncommented, comment it
    echo "Forcing comment on $1"
    sed -i "s/\\(^$1:.*\\)/;\\1/I" "$UNIPI_LTE_CONFIG"
}

function configure_gsm()
{
    #flag for checking whether some change in the config file happend
    changed=0
    enabled=0

    # skip first 3 paramaters "instance", "gsm", and "gsm0"
    shift
    shift
    shift

    while [ -n "${1}" ]; do
        case ${1} in
            enabled)
                # LTE should be enabled
                enabled=1
                echo "Enabled = 1"
                ;;
            disabled)
                # LTE should be disabled
                enabled=0
                echo "Enabled = 0"
                ;;
            apn)
                #update the APN
                shift
                apn="$1"
                echo "APN = $apn"
                    # update_confile() returns 0 if nothing changed, 1 when the config is changed
                if ! update_confile "apn" "\"IP\",\"${apn}\""; then
                    echo "changed"
                    changed=1
                fi
                ;;
            pincode)
                #update PIN code
                shift
                pin="$1"
                echo "PIN = $pin"
                if [ "$pin" == "" ]; then
                    # pin is empty, comment out the respecitve line
                    force_comment "pin"
                else
                    # some pin received, update it
                    # update_confile() returns 0 if nothing changed, 1 when the config is changed
                    if ! update_confile "pin" "${pin}"; then
                        echo "changed"
                        changed=1
                    fi
                fi
                ;;
            phonenumber)
                shift
                phonenumber="$1"
                echo "Phone number = $phonenumber"
                ;;
            *)
                # unknown parameter
                echo "Unknown parameter: $1"
                ;;
        esac
        shift
    done

    # if the LTE should be disabled, stop the service and disable it
    if [ "$enabled" == "0" ]; then
        #check whether the service is active
        if [ "$(systemctl is-active unipi-lte)" == "active" ]; then
            echo -n "LTE daemon is running, trying to stop it... "
            if ! systemctl stop unipi-lte; then
                echo "FAILED"
            else
                echo "Ok"
            fi
        else
            echo "LTE daemon is not running"
        fi

        # check whether the service is enabled
        if [ "$(systemctl is-enabled unipi-lte)" == "enabled" ]; then
            echo -n "LTE daemon is enabled, trying to disable it... "
            if ! systemctl disable unipi-lte; then
                echo "FAILED"
            else
                echo "Ok"
            fi
        else
            echo "LTE daemon is already disabled"
        fi
    else        
        # LTE should be on, generate the interfaces.d file
        echo "Generating wwan0 interface configuration to $TMP_INTERFACES_D/wwan0"
        echo "allow-hotplug wwan0" > "$TMP_INTERFACES_D/wwan0"
        echo "iface wwan0 inet dhcp" >> "$TMP_INTERFACES_D/wwan0"

        # the LTE daemon should be running
        if [ "$(systemctl is-enabled unipi-lte)" == "disabled" ]; then
            # daemon is disabled, enable it first
            echo -n "LTE daemon is disabled, trying to enabled it... "
            if ! systemctl enable unipi-lte; then
                echo "FAILED"
            else
                echo "Ok"
            fi

            # is the deamon running by eny chance?
            if [ "$(systemctl is-active unipi-lte)" == "active" ]; then
                # it's running, restart it
                echo -n "LTE daemon is already running, trying to restart it... "
                if ! systemctl restart unipi-lte; then
                    echo "FAILED"
                else
                    echo "Ok"
                fi
            else
                # not running, try to start it
                echo -n "LTE daemon is not running, trying to start it... "
                if ! systemctl start unipi-lte; then
                    echo "FAILED"
                else
                    echo "Ok"
                fi
            fi
            # end of "is disabled, should be running" section
        else
            # daemon is enabled, check if it is running
            if [ "$(systemctl is-active unipi-lte)" == "active" ]; then
                # it's running, if some change happend, restart the unipi-lte
                if [ "$changed" == "1" ]; then
                    echo -n "Restarting unipi-lte daemon..."
                    if ! systemctl restart unipi-lte; then
                        echo "FAILED"
                    else
                        echo "Ok"
                    fi
                else
                    echo "No change in configuration, no need to restart the LTE deamon"
                fi
            else
                # not running, just start it    
                echo -n "LTE daemon is stopped, trying to start it... "
                if ! systemctl start unipi-lte; then
                    echo "FAILED"
                else
                    echo "Ok"
                fi
            fi
            # end of "already enabled, start or restart it" section
        fi
        # end of "should be running" section
    fi
}
