#!/bin/bash
SCRIPTVERSION='v1.38'
SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
PIA_CONF_FILE=${SCRIPTPATH}'/../config/pialert.conf'
PIA_CONF_FILE_PATH=${SCRIPTPATH}'/../config'
PIA_DB_FILE=${SCRIPTPATH}'/../db/pialert.db'
PIA_DB_TOOLS_FILE=${SCRIPTPATH}'/../db/pialert_tools.db'
PIA_DB_FILE_PATH=${SCRIPTPATH}'/../db'
PIA_BACK_FILE_PATH=${SCRIPTPATH}'/../back'
PIA_REPORTS_PATH=${SCRIPTPATH}'/../front/reports'
PIA_TMP_PATH=${SCRIPTPATH}'/../front/php/tmp'
#Textformating
bold=$(tput bold)
normal=$(tput sgr0)
CURRENT_USER=$(whoami)

case $1 in

  help)

read -r -d '' PIALERTCLI_MANUAL << EOM
pialert-cli $SCRIPTVERSION (https://github.com/leiweibau/Pi.Alert)
Command line tool of Pi.Alert, which is used to support the front and back end.
Usage: ${bold}pialert-cli <command>${normal}

This is the list of supported commands

 ${bold}disable_scan <MIN>${normal}
    Stops all active scans and prevents new scans from starting. You can set a
    Timeout in minutes. If no timeout is set, Pi.Alert restarts itself with
    the next scan after 10min.

 ${bold}disable_mainscan <MIN>${normal}
    Disables the main scanning method for Pi.Alert (ARP scan, reading the
    Pi-hole DHCP leases and device database, as well as collecting data from
    other routers).

 ${bold}disable_icmp_mon${normal}
    Allows the start of new scans again. If the ICMPSCAN_ACTIVE parameter
    does not exist yet, it will be created and set to FALSE.

 ${bold}disable_service_mon${normal}
    Allows the start of new scans again. If the SCAN_WEBSERVICES parameter
    does not exist yet, it will be created and set to FALSE.

 ${bold}disable_satellites${normal}
    Allows the start of new scans again. If the SATELLITES_ACTIVE parameter
    does not exist yet, it will be created and set to FALSE.

 ${bold}enable_scan${normal}
    Allows the start of new scans again.

 ${bold}enable_mainscan <MIN>${normal}
    Enables the main scanning method for Pi.Alert (ARP scan, reading the
    Pi-hole DHCP leases and device database, as well as collecting data from
    other routers).

 ${bold}enable_icmp_mon${normal}
    Enable ICMP Monitoring. If the ICMPSCAN_ACTIVE parameter does not
    exist yet, it will be created and set to TRUE.

 ${bold}enable_service_mon${normal}"
    Enable Web Service Monitoring. If the SCAN_WEBSERVICES parameter does not
    exist yet, it will be created and set to TRUE.

 ${bold}enable_satellites${normal}"
    Enable Satellites. If the SATELLITES_ACTIVE parameter does not exist yet,
    it will be created and set to TRUE.

 ${bold}reporting_test${normal}
    Test reporting for all activated services.

 ${bold}set_apikey${normal}
    With the API key it is possible to make queries to the database without
    using the web page. If an API key already exists, it will be replaced.

 ${bold}set_autopassword${normal}
    Sets a new random password as a hashed value and show it plaintext in the
    console. If the PIALERT_WEB_PROTECTION parameter does not exist yet, it
    will be created and set to TRUE (login enabled).

 ${bold}set_login${normal}
    Sets the parameter PIALERT_WEB_PROTECTION in the config file to TRUE. If
    the parameter is not present, it will be created. Additionally the default
    password '123456' is set.

 ${bold}set_password <password>${normal}
    Sets the new password as a hashed value. If the PIALERT_WEB_PROTECTION
    parameter does not exist yet, it will be created and set to TRUE (login
    enabled).

 ${bold}set_permissions${normal}
    Repair file group permissions

 ${bold}set_sudoers${normal}
    Create sudoer file for www-data and Pi.Alert user

    Options:
    --lxc:        set "root" as user name

 ${bold}unset_login${normal}
    Sets the parameter PIALERT_WEB_PROTECTION in the config file to FALSE. If
    the parameter is not present, it will be created. Additionally the default
    password '123456' is set.

 ${bold}unset_sudoers${normal}
    Delete sudoer file for www-data and Pi.Alert user

 ${bold}update_db${normal}
    The script tries to make the database compatible for this fork.

2025 leiweibau (https://github.com/leiweibau/Pi.Alert) (https://leiweibau.net)

EOM

    echo -e "$PIALERTCLI_MANUAL" | more -e
    ;;

  set_login)
    ## Check if PIALERT_WEB_PROTECTION exists
    CHECK_PROT=$(grep "PIALERT_WEB_PROTECTION" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create PIALERT_WEB_PROTECTION and enable it
        sed -i "/^VENDORS_DB.*/a PIALERT_WEB_PROTECTION = True" $PIA_CONF_FILE
        sed -i "/^PIALERT_WEB_PROTECTION.*/a PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'" $PIA_CONF_FILE
    else
        ## Switch PIALERT_WEB_PROTECTION to enable
        sed -i "/PIALERT_WEB_PROTECTION/c\PIALERT_WEB_PROTECTION = True" $PIA_CONF_FILE
    fi
    echo "Login is now enabled"
    ;;

  unset_login)
    ## Check if PIALERT_WEB_PROTECTION exists
    CHECK_PROT=$(grep "PIALERT_WEB_PROTECTION" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create PIALERT_WEB_PROTECTION and disable it
        sed -i "/^VENDORS_DB.*/a PIALERT_WEB_PROTECTION = False" $PIA_CONF_FILE
        sed -i "/^PIALERT_WEB_PROTECTION.*/a PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'" $PIA_CONF_FILE
    else
        ## Switch PIALERT_WEB_PROTECTION to disable
        sed -i "/PIALERT_WEB_PROTECTION/c\PIALERT_WEB_PROTECTION = False" $PIA_CONF_FILE
    fi
    echo "Login is now disabled"
    ;;

  set_password)
    PIA_PASS=$2
    ## Check if PIALERT_WEB_PROTECTION exists
    CHECK_PROT=$(grep "PIALERT_WEB_PROTECTION" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create PIALERT_WEB_PROTECTION and enable it
        sed -i "/^VENDORS_DB.*/a PIALERT_WEB_PROTECTION = True" $PIA_CONF_FILE
    fi
    ## Prepare Hash
    PIA_PASS_HASH=$(echo -n $PIA_PASS | sha256sum | awk '{print $1}')
    echo "   The hashed password is:"
    echo "   $PIA_PASS_HASH"
    ## Check if the password parameter is set
    CHECK_PWD=$(grep "PIALERT_WEB_PASSWORD" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PWD -eq 0 ]
    then
        sed -i "/^PIALERT_WEB_PROTECTION.*/a PIALERT_WEB_PASSWORD = '$PIA_PASS_HASH'" $PIA_CONF_FILE
    else
        sed -i "/PIALERT_WEB_PASSWORD/c\PIALERT_WEB_PASSWORD = '$PIA_PASS_HASH'" $PIA_CONF_FILE
    fi
    echo ""
    echo "The new password is set"
    ;;

  set_autopassword)
    ## Check if PIALERT_WEB_PROTECTION exists
    CHECK_PROT=$(grep "PIALERT_WEB_PROTECTION" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create PIALERT_WEB_PROTECTION and enable it
        sed -i "/^VENDORS_DB.*/a PIALERT_WEB_PROTECTION = True" $PIA_CONF_FILE
    fi
    ## Create autopassword
    PIA_AUTOPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
    echo "   The password is: $PIA_AUTOPASS"
    ## Prepare Hash
    PIA_AUTOPASS_HASH=$(echo -n $PIA_AUTOPASS | sha256sum | awk '{print $1}')
    echo "   The hashed password is:"
    echo "   $PIA_AUTOPASS_HASH"
    ## Check if the password parameter is set
    CHECK_PWD=$(grep "PIALERT_WEB_PASSWORD" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PWD -eq 0 ]
    then
        ## Create password parameter
        sed -i "/^PIALERT_WEB_PROTECTION.*/a PIALERT_WEB_PASSWORD = '$PIA_AUTOPASS_HASH'" $PIA_CONF_FILE
    else
        ## Overwrite password parameter
        sed -i "/PIALERT_WEB_PASSWORD/c\PIALERT_WEB_PASSWORD = '$PIA_AUTOPASS_HASH'" $PIA_CONF_FILE
    fi
    echo ""
    echo "The new password is set"
    ;;

  disable_scan)
    STOPTIMER=$2
    re='^[0-9]+$'
    if ! [[ $STOPTIMER =~ $re ]] ; then
       echo "No timeout is set. Pi.Alert restarts itself with the next scan after 10min."
       STOPTIMER=10
    fi
    sudo killall arp-scan
    echo $STOPTIMER > ${SCRIPTPATH}/../config/setting_stoppialert
    /usr/bin/python3 ${SCRIPTPATH}/pialert_reporting_test.py reporting_starttimer
    echo "Configured Pi.Alert scans are disabled"
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_002', 'pialert-cli', 'LogStr_0511', '', '${STOPTIMER} min');"
        if [ $? -ne 0 ]; then
            echo "Failed to write to the journal, but continuing."
        fi
    fi

    ;;

  enable_scan)
    rm ${SCRIPTPATH}/../config/setting_stoppialert
    /usr/bin/python3 ${SCRIPTPATH}/pialert_reporting_test.py reporting_stoptimer
    echo "Configured Pi.Alert scans are enabled"
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_002', 'pialert-cli', 'LogStr_0510', '', '');"
    fi
    ;;

  enable_service_mon)
    ## Check if SCAN_WEBSERVICES exists
    CHECK_PROT=$(grep "SCAN_WEBSERVICES" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create SCAN_WEBSERVICES and enable it
        sed -i "/^VENDORS_DB.*/a SCAN_WEBSERVICES  = True" $PIA_CONF_FILE
    else
        ## Switch SCAN_WEBSERVICES to enable
        sed -i "/SCAN_WEBSERVICES/c\SCAN_WEBSERVICES  = True" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_030', 'pialert-cli', 'LogStr_0301', '', '');"
    fi
    echo "Web Service Monitoring is now enabled"
    ;;

  disable_service_mon)
    ## Check if SCAN_WEBSERVICES exists
    CHECK_PROT=$(grep "SCAN_WEBSERVICES" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create SCAN_WEBSERVICES and disable it
        sed -i "/^VENDORS_DB.*/a SCAN_WEBSERVICES  = False" $PIA_CONF_FILE
    else
        ## Switch SCAN_WEBSERVICES to disable
        sed -i "/SCAN_WEBSERVICES/c\SCAN_WEBSERVICES  = False" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_030', 'pialert-cli', 'LogStr_0302', '', '');"
    fi
    echo "Web Service Monitoring is now disabled"
    ;;

  enable_satellites)
    ## Check if SCAN_WEBSERVICES exists
    CHECK_PROT=$(grep "SATELLITES_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create SCAN_WEBSERVICES and enable it
        sed -i "/^VENDORS_DB.*/a SATELLITES_ACTIVE = True" $PIA_CONF_FILE
    else
        ## Switch SCAN_WEBSERVICES to enable
        sed -i "/SATELLITES_ACTIVE/c\SATELLITES_ACTIVE = True" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_033', 'pialert-cli', 'LogStr_0305', '', '');"
    fi
    echo "Satellites are now enabled"
    ;;

  disable_satellites)
    ## Check if SCAN_WEBSERVICES exists
    CHECK_PROT=$(grep "SATELLITES_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create SCAN_WEBSERVICES and disable it
        sed -i "/^VENDORS_DB.*/a SATELLITES_ACTIVE = False" $PIA_CONF_FILE
    else
        ## Switch SCAN_WEBSERVICES to disable
        sed -i "/SATELLITES_ACTIVE/c\SATELLITES_ACTIVE = False" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_033', 'pialert-cli', 'LogStr_0306', '', '');"
    fi
    echo "Satellites are now disabled"
    ;;

  enable_icmp_mon)
    ## Check if ICMPSCAN_ACTIVE exists
    CHECK_PROT=$(grep "ICMPSCAN_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create ICMPSCAN_ACTIVE and enable it
        sed -i "/^VENDORS_DB.*/a ICMPSCAN_ACTIVE   = True" $PIA_CONF_FILE
    else
        ## Switch ICMPSCAN_ACTIVE to enable
        sed -i "/ICMPSCAN_ACTIVE/c\ICMPSCAN_ACTIVE   = True" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_031', 'pialert-cli', 'LogStr_0303', '', '');"
    fi
    echo "ICMP Monitoring is now enabled"
    ;;

  disable_icmp_mon)
    ## Check if ICMPSCAN_ACTIVE exists
    CHECK_PROT=$(grep "ICMPSCAN_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create ICMPSCAN_ACTIVE and disable it
        sed -i "/^VENDORS_DB.*/a ICMPSCAN_ACTIVE   = False" $PIA_CONF_FILE
    else
        ## Switch ICMPSCAN_ACTIVE to disable
        sed -i "/ICMPSCAN_ACTIVE/c\ICMPSCAN_ACTIVE   = False" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_031', 'pialert-cli', 'LogStr_0304', '', '');"
    fi
    echo "ICMP Monitoring is now disabled"
    ;;

  enable_mainscan)
    ## Check if ARPSCAN_ACTIVE exists
    CHECK_PROT=$(grep "ARPSCAN_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create ARPSCAN_ACTIVE and enable it
        sed -i "/^VENDORS_DB.*/a ARPSCAN_ACTIVE = True" $PIA_CONF_FILE
    else
        ## Switch ARPSCAN_ACTIVE to enable
        sed -i "/ARPSCAN_ACTIVE/c\ARPSCAN_ACTIVE = True" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_032', 'pialert-cli', 'LogStr_9991', '', '');"
    fi
    echo "Main Scan is now enabled"
    ;;

  disable_mainscan)
    ## Check if ARPSCAN_ACTIVE exists
    CHECK_PROT=$(grep "ARPSCAN_ACTIVE" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_PROT -eq 0 ]
    then
        ## Create ARPSCAN_ACTIVE and disable it
        sed -i "/^VENDORS_DB.*/a ARPSCAN_ACTIVE = False" $PIA_CONF_FILE
    else
        ## Switch ARPSCAN_ACTIVE to disable
        sed -i "/ARPSCAN_ACTIVE/c\ARPSCAN_ACTIVE = False" $PIA_CONF_FILE
    fi
    if [ "$CURRENT_USER" != "www-data" ]; then
        sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_032', 'pialert-cli', 'LogStr_9992', '', '');"
    fi
    echo "Main Scan is now disabled"
    ;;

  update_db)
    ## update database
    echo "################################################################################"
    echo "# You are planning to update the Pi.Alert DB. Please make sure that no scan    #"
    echo "# takes place during the update to avoid possible database errors afterwards!  #"
    echo "#                                                                              #"
    echo "# This can be done by pausing the Arp scan via the settings page. However,     #"
    echo "# scans that are already running will not be terminated. For more information, #"
    echo "# check the Help/FAQ section in Pi.Alert                                       #"
    echo "#                                                                              #"
    echo "# Press STRG+C to Abort                                                        #"
    echo "################################################################################"
    echo ""
    echo ""
    printf "%s " "Press enter to continue"
    read ans
    echo ""
    echo "Update DB $PIA_DB_FILE"
    echo -e "\nPurge old db backup"
    rm ${PIA_DB_FILE_PATH}/pialert.db.bak
    rm ${PIA_DB_FILE_PATH}/pialert_tools.db.bak
    echo -e "...Create backup before insert new table"
    sqlite3 "$PIA_DB_FILE" ".backup '${PIA_DB_FILE_PATH}/pialert.db.bak'"
    sqlite3 "$PIA_DB_TOOLS_FILE" ".backup '${PIA_DB_FILE_PATH}/pialert_tools.db.bak'"
    # Fritzbox_Network
    echo -e "...Insert new table 'Fritzbox_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Fritzbox_Network\" (
      \"FB_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"FB_IP\" STRING(50) COLLATE NOCASE,
      \"FB_Name\" STRING(50),
      \"FB_Vendor\" STRING(250)
    );"
    # Mikrotik_Network
    echo -e "...Insert new table 'Mikrotik_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Mikrotik_Network\" (
      \"MT_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"MT_IP\" STRING(50) COLLATE NOCASE,
      \"MT_Name\" STRING(50),
      \"MT_Vendor\" STRING(250)
    );"
    # Unifi_Network
    echo -e "...Insert new table 'Unifi_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Unifi_Network\" (
      \"UF_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"UF_IP\" STRING(50) COLLATE NOCASE,
      \"UF_Name\" STRING(50),
      \"UF_Vendor\" STRING(250)
    );"
    # Nmap_DHCP_Server
    echo -e "...Insert new table 'Nmap_DHCP_Server' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Nmap_DHCP_Server\" (
      \"scan_num\" INTEGER NOT NULL,
      \"dhcp_server\" TEXT NOT NULL
    );"
    # Pi.Alert Journal
    echo -e "...Insert new table 'pialert_journal' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"pialert_journal\" (
      \"journal_id\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"Journal_DateTime\" TEXT NOT NULL,
      \"LogClass\" NUMERIC NOT NULL,
      \"Trigger\" TEXT NOT NULL,
      \"LogString\" TEXT NOT NULL,
      \"Hash\" TEXT,
      \"Additional_Info\" TEXT
    );"
    # Devices.dev_Model
    echo -e "...Insert new column 'dev_Model' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_Model")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_Model\" STRING(250);"
    else
      echo "        Column 'dev_Model' already exists in the 'Devices' table."
    fi
    # Devices.dev_Serialnumber
    echo -e "...Insert new column 'dev_Serialnumber' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_Serialnumber")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_Serialnumber\" STRING(100);"
    else
      echo "        Column 'dev_Serialnumber' already exists in the 'Devices' table."
    fi
    # Devices.dev_ConnectionType
    echo -e "...Insert new column 'dev_ConnectionType' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_ConnectionType")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_ConnectionType\" STRING(30);"
    else
      echo "        Column 'dev_ConnectionType' already exists in the 'Devices' table."
    fi
    # ICMP_Mon
    echo -e "...Insert new table 'ICMP_Mon' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"ICMP_Mon\" (
      \"icmp_ip\" NUMERIC NOT NULL,
      \"icmp_hostname\" TEXT,
      \"icmp_LastScan\" TEXT NOT NULL,
      \"icmp_PresentLastScan\" INTEGER DEFAULT 0,
      \"icmp_avgrtt\" NUMERIC,
      \"icmp_AlertEvents\" INTEGER DEFAULT 0,
      \"icmp_AlertDown\" INTEGER DEFAULT 0,
      \"icmp_Favorite\" INTEGER DEFAULT 0,
      \"icmp_Notes\" INTEGER,
      \"icmp_type\" TEXT,
      \"icmp_group\" TEXT,
      \"icmp_location\" TEXT,
      \"icmp_owner\" TEXT,
      PRIMARY KEY(\"icmp_ip\")
    );"
    # ICMP_Mon_CurrentScan
    echo -e "...Insert new table 'ICMP_Mon_CurrentScan' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"ICMP_Mon_CurrentScan\" (
      \"cur_ip\" NUMERIC NOT NULL,
      \"cur_LastScan\" TEXT NOT NULL,
      \"cur_Present\" INTEGER,
      \"cur_PresentChanged\" INTEGER,
      \"cur_avgrrt\" NUMERIC,
      \"cur_AlertEvents\" INTEGER NOT NULL DEFAULT 0,
      \"cur_AlertDown\" INTEGER NOT NULL DEFAULT 0,
      PRIMARY KEY(\"cur_ip\")
    );"
    # ICMP_Mon_Events
    echo -e "...Insert new table 'ICMP_Mon_Events' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"ICMP_Mon_Events\" (
      \"icmpeve_ip\" NUMERIC NOT NULL,
      \"icmpeve_DateTime\" TEXT NOT NULL,
      \"icmpeve_Present\" INTEGER,
      \"icmpeve_avgrtt\" NUMERIC
    );"
    # Services.mon_ssl_subject
    echo -e "...Insert new column 'mon_ssl_subject' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_ssl_subject")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_ssl_subject\" TEXT;"
    else
      echo "        Column 'mon_ssl_subject' already exists in the 'Services' table."
    fi
    # Services.mon_ssl_issuer
    echo -e "...Insert new column 'mon_ssl_issuer' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_ssl_issuer")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_ssl_issuer\" TEXT;"
    else
      echo "        Column 'mon_ssl_issuer' already exists in the 'Services' table."
    fi
    # Services.mon_ssl_valid_from
    echo -e "...Insert new column 'mon_ssl_valid_from' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_ssl_valid_from")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_ssl_valid_from\" TEXT;"
    else
      echo "        Column 'mon_ssl_valid_from' already exists in the 'Services' table."
    fi
    # Services.mon_ssl_valid_to
    echo -e "...Insert new column 'mon_ssl_valid_to' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_ssl_valid_to")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_ssl_valid_to\" TEXT;"
    else
      echo "        Column 'mon_ssl_valid_to' already exists in the 'Services' table."
    fi
    # Services.mon_ssl_fc
    echo -e "...Insert new column 'mon_ssl_fc' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_ssl_fc")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_ssl_fc\" INTEGER DEFAULT 0;"
    else
      echo "        Column 'mon_ssl_fc' already exists in the 'Services' table."
    fi
    # Services_CurrentScan.cur_ssl_subject
    echo -e "...Insert new column 'cur_ssl_subject' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ssl_subject")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_ssl_subject\" TEXT;"
    else
      echo "        Column 'cur_ssl_subject' already exists in the 'Services_CurrentScan' table."
    fi
    # Services_CurrentScan.cur_ssl_issuer
    echo -e "...Insert new column 'cur_ssl_issuer' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ssl_issuer")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_ssl_issuer\" TEXT;"
    else
      echo "        Column 'cur_ssl_issuer' already exists in the 'Services_CurrentScan' table."
    fi
    # Services_CurrentScan.cur_ssl_valid_from
    echo -e "...Insert new column 'cur_ssl_valid_from' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ssl_valid_from")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_ssl_valid_from\" TEXT;"
    else
      echo "        Column 'cur_ssl_valid_from' already exists in the 'Services_CurrentScan' table."
    fi
    # Services_CurrentScan.cur_ssl_valid_to
    echo -e "...Insert new column 'cur_ssl_valid_to' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ssl_valid_to")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_ssl_valid_to\" TEXT;"
    else
      echo "        Column 'cur_ssl_valid_to' already exists in the 'Services_CurrentScan' table."
    fi
    # Services_CurrentScan.cur_ssl_fc
    echo -e "...Insert new column 'cur_ssl_fc' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ssl_fc")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_ssl_fc\" INTEGER DEFAULT 0;"
    else
      echo "        Column 'cur_ssl_fc' already exists in the 'Services_CurrentScan' table."
    fi
    # Services_Events.moneve_ssl_fc
    echo -e "...Insert new column 'moneve_ssl_fc' to table 'Services_Events' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_Events\");" | awk -F\| '{print $2}' | grep -wci "moneve_ssl_fc")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_Events\" ADD COLUMN \"moneve_ssl_fc\" INTEGER DEFAULT 0;"
    else
      echo "        Column 'moneve_ssl_fc' already exists in the 'Services_Events' table."
    fi
    # Online_History.data_source
    echo -e "...Insert new column 'Data_Source' to table 'Online_History' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Online_History\");" | awk -F\| '{print $2}' | grep -wci "Data_Source")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Online_History\" ADD COLUMN \"Data_Source\" TEXT;"
    else
      echo "        Column 'Data_Source' already exists in the 'Online_History' table."
    fi
    # ICMP_Mon.icmp_Archived
    echo -e "...Insert new column 'icmp_Archived' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_Archived")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_Archived\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'icmp_Archived' already exists in the 'ICMP_Mon' table."
    fi
    # Devices.dev_LinkSpeed
    echo -e "...Insert new column 'dev_LinkSpeed' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_LinkSpeed")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_LinkSpeed\" STRING(30);"
    else
      echo "        Column 'dev_LinkSpeed' already exists in the 'Devices' table."
    fi
    # Satellites
    # CurrentScan.cur_ScanSource (Satellite)
    echo -e "...Insert new column 'cur_ScanSource' to table 'CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_ScanSource")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"CurrentScan\" ADD COLUMN \"cur_ScanSource\" STRING(250) NOT NULL DEFAULT 'local';"
    else
      echo "        Column 'cur_ScanSource' already exists in the 'CurrentScan' table."
    fi
    # Satellites
    echo -e "...Insert new table 'Satellites' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Satellites\" (
      \"sat_id\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"sat_name\" STRING(150) NOT NULL UNIQUE,
      \"sat_token\" STRING(250) NOT NULL UNIQUE,
      \"sat_password\" STRING(250) NOT NULL UNIQUE,
      \"sat_lastupdate\" DATETIME NOT NULL
    );"
    # Devices.dev_ScanSource
    echo -e "...Insert new column 'dev_ScanSource' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_ScanSource")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_ScanSource\" STRING(250) NOT NULL DEFAULT 'local';"
    else
      echo "        Column 'dev_ScanSource' already exists in the 'Devices' table."
    fi
    # Satellites_Network
    echo -e "...Insert new table 'Satellites_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Satellites_Network\" (
      \"Sat_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"Sat_IP\" STRING(50) COLLATE NOCASE,
      \"Sat_Name\" STRING(50),
      \"Sat_Vendor\" STRING(250),
      \"Sat_ScanMethod\" STRING(50),
      \"Sat_Token\" STRING(250)
    );"
    # Satellites.sat_remote_version
    echo -e "...Insert new column 'sat_remote_version' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_remote_version")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_remote_version\" STRING(20);"
    else
      echo "        Column 'sat_remote_version' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_arp
    echo -e "...Insert new column 'sat_conf_scan_arp' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_arp")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_arp\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_arp' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_fritzbox
    echo -e "...Insert new column 'sat_conf_scan_fritzbox' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_fritzbox")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_fritzbox\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_fritzbox' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_mikrotik
    echo -e "...Insert new column 'sat_conf_scan_mikrotik' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_mikrotik")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_mikrotik\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_mikrotik' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_unifi
    echo -e "...Insert new column 'sat_conf_scan_unifi' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_unifi")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_unifi\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_unifi' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_host_data
    echo -e "...Insert new column 'sat_host_data' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_host_data")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_host_data\" TEXT;"
    else
      echo "        Column 'sat_host_data' already exists in the 'Satellites' table."
    fi
    # Devices.dev_PresencePage
    echo -e "...Insert new column 'dev_PresencePage' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_PresencePage")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_PresencePage\" BOOLEAN NOT NULL DEFAULT 1;"
    else
      echo "        Column 'dev_PresencePage' already exists in the 'Devices' table."
    fi
    # Parameters.par_Long_Value
    echo -e "...Insert new column 'par_Long_Value' to table 'Parameters' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Parameters\");" | awk -F\| '{print $2}' | grep -wci "par_Long_Value")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Parameters\" ADD COLUMN \"par_Long_Value\" TEXT;"
    else
      echo "        Column 'par_Long_Value' already exists in the 'Parameters' table."
    fi
    # Satellites.sat_conf_scan_openwrt
    echo -e "...Insert new column 'sat_conf_scan_openwrt' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_openwrt")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_openwrt\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_openwrt' already exists in the 'Satellites' table."
    fi
    # network_infrastructure.net_networkname
    echo -e "...Insert new column 'net_networkname' to table 'network_infrastructure' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"network_infrastructure\");" | awk -F\| '{print $2}' | grep -wci "net_networkname")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"network_infrastructure\" ADD COLUMN \"net_networkname\" STRING(250) NOT NULL DEFAULT 'local';"
    else
      echo "        Column 'net_networkname' already exists in the 'network_infrastructure' table."
    fi
    # ICMP_Mon_Connections
    echo -e "...Insert new table 'ICMP_Mon_Connections' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"ICMP_Mon_Connections\" (
      \"icmpeve_ip\" NUMERIC NOT NULL,
      \"icmpeve_DateTime\" TEXT NOT NULL,
      \"icmpeve_Present\" INTEGER,
      \"icmpeve_EventType\" STRING(50) NOT NULL COLLATE NOCASE,
      \"icmpeve_AdditionalInfo\" STRING(250) DEFAULT ''
    );"
    # Devices.dev_Scan_Validation
    echo -e "...Insert new column 'dev_Scan_Validation' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_Scan_Validation")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_Scan_Validation\" INTEGER NOT NULL DEFAULT 0;"
    else
      echo "        Column 'dev_Scan_Validation' already exists in the 'Devices' table."
    fi
    # Devices.dev_Scan_Validation_State
    echo -e "...Insert new column 'dev_Scan_Validation_State' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_Scan_Validation_State")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_Scan_Validation_State\" INTEGER NOT NULL DEFAULT 0;"
    else
      echo "        Column 'dev_Scan_Validation_State' already exists in the 'Devices' table."
    fi
    # ICMP_Mon.icmp_Scan_Validation
    echo -e "...Insert new column 'icmp_Scan_Validation' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_Scan_Validation")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_Scan_Validation\" INTEGER NOT NULL DEFAULT 0;"
    else
      echo "        Column 'icmp_Scan_Validation' already exists in the 'ICMP_Mon' table."
    fi
    # ICMP_Mon.icmp_Scan_Validation_State
    echo -e "...Insert new column 'icmp_Scan_Validation_State' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_Scan_Validation_State")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_Scan_Validation_State\" INTEGER NOT NULL DEFAULT 0;"
    else
      echo "        Column 'icmp_Scan_Validation_State' already exists in the 'ICMP_Mon' table."
    fi
    # Satellites.sat_conf_scan_pihole_net
    echo -e "...Insert new column 'sat_conf_scan_pihole_net' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_pihole_net")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_pihole_net\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_pihole_net' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_pihole_dhcp
    echo -e "...Insert new column 'sat_conf_scan_pihole_dhcp' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_pihole_dhcp")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_pihole_dhcp\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_pihole_dhcp' already exists in the 'Satellites' table."
    fi
    # Openwrt_Network
    echo -e "...Insert new table 'Openwrt_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Openwrt_Network\" (
      \"OWRT_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"OWRT_IP\" STRING(50) COLLATE NOCASE,
      \"OWRT_Name\" STRING(50),
      \"OWRT_Vendor\" STRING(250)
    );"
    # Asuswrt_Network
    echo -e "...Insert new table 'Asuswrt_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Asuswrt_Network\" (
      \"ASUS_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"ASUS_IP\" STRING(50) COLLATE NOCASE,
      \"ASUS_Name\" STRING(50),
      \"ASUS_Vendor\" STRING(250),
      \"ASUS_Method\" STRING(50)
    );"
    # Satellites.sat_conf_scan_asuswrt
    echo -e "...Insert new column 'sat_conf_scan_asuswrt' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_asuswrt")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_asuswrt\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_asuswrt' already exists in the 'Satellites' table."
    fi
    # ICMP_Mon.icmp_vendor
    echo -e "...Insert new column 'icmp_vendor' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_vendor")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_vendor\" STRING(100);"
    else
      echo "        Column 'icmp_vendor' already exists in the 'ICMP_Mon' table."
    fi
    # ICMP_Mon.icmp_model
    echo -e "...Insert new column 'icmp_model' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_model")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_model\" STRING(100);"
    else
      echo "        Column 'icmp_model' already exists in the 'ICMP_Mon' table."
    fi
    # ICMP_Mon.icmp_serial
    echo -e "...Insert new column 'icmp_serial' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_serial")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_serial\" STRING(25);"
    else
      echo "        Column 'icmp_serial' already exists in the 'ICMP_Mon' table."
    fi
    # Services.mon_AlertUp
    echo -e "...Insert new column 'mon_AlertUp' to table 'Services' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services\");" | awk -F\| '{print $2}' | grep -wci "mon_AlertUp")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services\" ADD COLUMN \"mon_AlertUp\" INTEGER DEFAULT 0;"
    else
      echo "        Column 'mon_AlertUp' already exists in the 'Services' table."
    fi
    # Services_CurrentScan.cur_AlertUp
    echo -e "...Insert new column 'cur_AlertUp' to table 'Services_CurrentScan' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Services_CurrentScan\");" | awk -F\| '{print $2}' | grep -wci "cur_AlertUp")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Services_CurrentScan\" ADD COLUMN \"cur_AlertUp\" INTEGER DEFAULT 0;"
    else
      echo "        Column 'cur_AlertUp' already exists in the 'Services_CurrentScan' table."
    fi
    # pfsense_Network
    echo -e "...Insert new table 'pfsense_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"pfsense_Network\" (
      \"PF_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"PF_IP\" STRING(50) COLLATE NOCASE,
      \"PF_Name\" STRING(50),
      \"PF_Vendor\" STRING(250),
      \"PF_Method\" STRING(50),
      \"PF_Interface\" STRING(20),
      \"PF_Custom_a\" STRING(100),
      \"PF_Custom_b\" STRING(100),
      \"PF_Connected\" BOOLEAN,
      \"PF_Datetime\" DATETIME
    );"
    # opnsense_Network
    echo -e "...Insert new table 'opnsense_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"opnsense_Network\" (
      \"OPN_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"OPN_IP\" STRING(50) COLLATE NOCASE,
      \"OPN_Name\" STRING(50),
      \"OPN_Vendor\" STRING(250),
      \"OPN_Method\" STRING(50),
      \"OPN_Interface\" STRING(20),
      \"OPN_Custom_a\" STRING(100),
      \"OPN_Custom_b\" STRING(100),
      \"OPN_Connected\" BOOLEAN,
      \"OPN_Datetime\" DATETIME
    );"
    # AdGuard_Network
    echo -e "...Insert new table 'AdGuard_Network' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"AdGuard_Network\" (
      \"AG_MAC\" STRING(50) NOT NULL COLLATE NOCASE,
      \"AG_IP\" STRING(50) NOT NULL COLLATE NOCASE UNIQUE,
      \"AG_Name\" STRING(100),
      \"AG_Connected\" BOOLEAN NOT NULL DEFAULT 0,
      \"AG_LeaseExpires\" INTEGER DEFAULT 0,
      \"AG_LastQueryTime\" DATETIME,
      PRIMARY KEY(\"AG_MAC\")
    );"
    # AdGuard_Activity
    echo -e "...Insert new table 'AdGuard_Activity' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"AdGuard_Activity\" (
      \"AGA_IP\" STRING(50) NOT NULL COLLATE NOCASE UNIQUE,
      \"AGA_MAC\" STRING(50) COLLATE NOCASE,
      \"AGA_Name\" STRING(100),
      \"AGA_LastQueryTime\" DATETIME NOT NULL,
      PRIMARY KEY(\"AGA_IP\")
    );"
    # Satellites.sat_conf_scan_pfsense
    echo -e "...Insert new column 'sat_conf_scan_pfsense' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_pfsense")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_pfsense\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_pfsense' already exists in the 'Satellites' table."
    fi
    # Log_History_Cleanup
    echo -e "...Insert new table 'Log_History_Cleanup' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_Cleanup\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_InternetIP
    echo -e "...Insert new table 'Log_History_InternetIP' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_InternetIP\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_Nmap
    echo -e "...Insert new table 'Log_History_Nmap' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_Nmap\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_Scan
    echo -e "...Insert new table 'Log_History_Scan' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_Scan\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_Speedtest
    echo -e "...Insert new table 'Log_History_Speedtest' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_Speedtest\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_Vendors
    echo -e "...Insert new table 'Log_History_Vendors' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_Vendors\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Log_History_WebServices
    echo -e "...Insert new table 'Log_History_WebServices' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Log_History_WebServices\" (
      \"ScanDate\" DATETIME NOT NULL,
      \"Logfile\" BLOB,
      PRIMARY KEY(\"ScanDate\")
    );"
    # Tools_Nmap_ManScan
    echo -e "...Insert new table 'Tools_Nmap_ManScan' to DB"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Tools_Nmap_ManScan\" (
      \"ID\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"scan_date\" DATETIME NOT NULL,
      \"scan_target\" STRING(50) NOT NULL,
      \"scan_type\" STRING(50) NOT NULL,
      \"scan_result\" TEXT,
      \"reserve_a\" INTEGER,
      \"reserve_b\" TEXT,
      \"reserve_c\" TEXT,
      \"reserve_d\" TEXT
    );"
    # Tools_Nmap_Queue
    echo -e "...Insert new table 'Tools_Nmap_Queue' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Tools_Nmap_Queue\" (
      \"queue_id\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"device_mac\" TEXT NOT NULL COLLATE NOCASE,
      \"target_ip\" TEXT NOT NULL,
      \"scan_type\" TEXT NOT NULL DEFAULT 'detail',
      \"source\" TEXT NOT NULL DEFAULT 'manual',
      \"status\" TEXT NOT NULL DEFAULT 'queued',
      \"requested_at\" TEXT NOT NULL,
      \"started_at\" TEXT,
      \"completed_at\" TEXT,
      \"attempts\" INTEGER NOT NULL DEFAULT 0,
      \"last_error\" TEXT,
      \"result_id\" INTEGER,
      UNIQUE (\"device_mac\", \"scan_type\")
    );"
    # Tools_Nmap_Schedule (prepared for a future opt-in scheduler; not active)
    echo -e "...Insert new table 'Tools_Nmap_Schedule' to DB-Tools"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Tools_Nmap_Schedule\" (
      \"schedule_id\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"device_mac\" TEXT NOT NULL COLLATE NOCASE UNIQUE,
      \"scan_type\" TEXT NOT NULL DEFAULT 'detail',
      \"interval_minutes\" INTEGER NOT NULL,
      \"next_run_at\" TEXT,
      \"enabled\" INTEGER NOT NULL DEFAULT 0,
      \"last_queued_at\" TEXT
    );"
    # Tools_Speedtest_History
    echo -e "...Insert new table 'Tools_Speedtest_History' to DB"
    sqlite3 "$PIA_DB_TOOLS_FILE" "CREATE TABLE IF NOT EXISTS \"Tools_Speedtest_History\" (
      \"ID\" INTEGER PRIMARY KEY AUTOINCREMENT,
      \"speed_date\" TEXT,
      \"speed_isp\" TEXT,
      \"speed_server\" TEXT,
      \"speed_ping\" NUMERIC,
      \"speed_down\" NUMERIC,
      \"speed_up\" NUMERIC
    );"
    # Index on Services_Events
    echo -e "...Create index on Services_Events (DateTime)"
    sqlite3 "$PIA_DB_FILE" "CREATE INDEX IF NOT EXISTS \"idx_services_events_datetime\"
    ON \"Services_Events\" (\"moneve_DateTime\");"

    # Services_Events_Journal
    echo -e "...Insert new table 'Services_Events_Journal' to DB"
    sqlite3 "$PIA_DB_FILE" "CREATE TABLE IF NOT EXISTS \"Services_Events_Journal\" (
      \"monevj_URL\" TEXT NOT NULL,
      \"monevj_DateTime\" TEXT NOT NULL,
      \"monevj_Additional_Info\" TEXT
    );"
    # Index on Services_Events_Journal
    echo -e "...Create index on Services_Events_Journal (URL + DateTime)"
    sqlite3 "$PIA_DB_FILE" "CREATE INDEX IF NOT EXISTS \"idx_Services_Events_Journal_URL_DateTime\"
    ON \"Services_Events_Journal\" (\"monevj_URL\", \"monevj_DateTime\" DESC);"
    # Devices.dev_MQTTDevice
    echo -e "...Insert new column 'dev_MQTTDevice' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_MQTTDevice")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_MQTTDevice\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'dev_MQTTDevice' already exists in the 'Devices' table."
    fi
    # Devices.dev_MQTTDevice_cleanup
    echo -e "...Insert new column 'dev_MQTTDevice_cleanup' to table 'Devices' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Devices\");" | awk -F\| '{print $2}' | grep -wci "dev_MQTTDevice_cleanup")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Devices\" ADD COLUMN \"dev_MQTTDevice_cleanup\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'dev_MQTTDevice_cleanup' already exists in the 'Devices' table."
    fi
    # ICMP_Mon.icmp_MQTTDevice
    echo -e "...Insert new column 'icmp_MQTTDevice' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_MQTTDevice")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_MQTTDevice\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'icmp_MQTTDevice' already exists in the 'ICMP_Mon' table."
    fi
    # ICMP_Mon.icmp_MQTTDevice_cleanup
    echo -e "...Insert new column 'icmp_MQTTDevice_cleanup' to table 'ICMP_Mon' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"ICMP_Mon\");" | awk -F\| '{print $2}' | grep -wci "icmp_MQTTDevice_cleanup")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"ICMP_Mon\" ADD COLUMN \"icmp_MQTTDevice_cleanup\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'icmp_MQTTDevice_cleanup' already exists in the 'ICMP_Mon' table."
    fi
    # Satellites.sat_conf_scan_opnsense
    echo -e "...Insert new column 'sat_conf_scan_opnsense' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_opnsense")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_opnsense\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_opnsense' already exists in the 'Satellites' table."
    fi
    # Satellites.sat_conf_scan_adguard
    echo -e "...Insert new column 'sat_conf_scan_adguard' to table 'Satellites' to DB"
    existing=$(sqlite3 "$PIA_DB_FILE" "PRAGMA table_info(\"Satellites\");" | awk -F\| '{print $2}' | grep -wci "sat_conf_scan_adguard")
    if [ "$existing" -eq 0 ]; then
      sqlite3 "$PIA_DB_FILE" "ALTER TABLE \"Satellites\" ADD COLUMN \"sat_conf_scan_adguard\" BOOLEAN NOT NULL DEFAULT 0;"
    else
      echo "        Column 'sat_conf_scan_adguard' already exists in the 'Satellites' table."
    fi

    echo -e "\nUpdate finished!\n"

    # Enter to Journal
    sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_010', 'pialert-cli', 'LogStr_0010', '', '');"

    ;;

  set_apikey)
    ## Create API-Key
    ##PIA_APIKEY_RAND="Test"
    PIA_APIKEY_RAND=$(head -c 4096 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 60 | head -n 1)
    ## Check if PIALERT_APIKEY exists
    CHECK_API=$(grep "PIALERT_APIKEY" $PIA_CONF_FILE | wc -l)
    if [ $CHECK_API -eq 0 ]
    then
        ## Create PIALERT_APIKEY
        sed -i "/^VENDORS_DB.*/a PIALERT_APIKEY             = '$PIA_APIKEY_RAND'" $PIA_CONF_FILE
    else
        ## Change PIALERT_APIKEY
        sed -i "/PIALERT_APIKEY/c\PIALERT_APIKEY             = '$PIA_APIKEY_RAND'" $PIA_CONF_FILE
    fi
    echo "API-Key set"
    ;;

  set_permissions)
    echo "Pi.Alert Group: www-data"
    echo "    Set DB Group Permissions only"
    sudo chmod -R 775 "$PIA_DB_FILE_PATH"
    sudo chgrp -R www-data "$PIA_DB_FILE_PATH"
    echo "    Set Config Group Permissions only"
    sudo chmod -R 775 "$PIA_CONF_FILE_PATH"
    sudo chgrp -R www-data "$PIA_CONF_FILE_PATH"
    echo "    Set Back Group Permissions only"
    sudo chmod -R 775 "$PIA_BACK_FILE_PATH"
    sudo chgrp -R www-data "$PIA_BACK_FILE_PATH"
    echo "    Set tmp Group Permissions only"
    sudo chmod -R 775 "$PIA_TMP_PATH"
    sudo chgrp -R www-data "$PIA_TMP_PATH"
    echo "    Set Reports Permissions"
    sudo chmod -R 775 "$PIA_REPORTS_PATH"
    sudo chgrp -R www-data "$PIA_REPORTS_PATH"
    echo "Group Permissions set"
    ;;

  set_sudoers)
    # get username
    if [ "$2" == "--lxc" ]; then
        PIA_USER="root"
    else
        # Default behavior
        PIA_USER="$(id -un)"
    fi
    # get username from install directory
    echo "Pi.Alert User: $PIA_USER"
    echo -e "\nFrontend sudoer"
    echo "www-data ALL=(ALL) NOPASSWD: ${SCRIPTPATH}/pialert-cli" | sudo tee /etc/sudoers.d/pialert-frontend
    echo "www-data ALL=(ALL) NOPASSWD: /usr/bin/df" | sudo tee -a /etc/sudoers.d/pialert-frontend
    echo "www-data ALL=(ALL) NOPASSWD: /usr/bin/nmap" | sudo tee -a /etc/sudoers.d/pialert-frontend
    echo "www-data ALL=(ALL) NOPASSWD: ${SCRIPTPATH}/speedtest/speedtest" | sudo tee -a /etc/sudoers.d/pialert-frontend
    echo -e "\nBackend sudoer"
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: ${SCRIPTPATH}/pialert-cli" | sudo tee /etc/sudoers.d/pialert-backend
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: /usr/sbin/arp-scan" | sudo tee -a /etc/sudoers.d/pialert-backend
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: /usr/bin/nmap" | sudo tee -a /etc/sudoers.d/pialert-backend
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: /usr/bin/chown, /usr/bin/chmod" | sudo tee -a /etc/sudoers.d/pialert-backend 
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: ${SCRIPTPATH}/speedtest/speedtest" | sudo tee -a /etc/sudoers.d/pialert-backend
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: /usr/bin/ping" | sudo tee -a /etc/sudoers.d/pialert-backend
    echo "${PIA_USER} ALL=(ALL) NOPASSWD: /usr/sbin/shutdown" | sudo tee -a /etc/sudoers.d/pialert-backend
    ;;

  unset_sudoers)
    ## Remove sudoer files
    echo "Removing sudoers"
    sudo rm /etc/sudoers.d/pialert-frontend
    sudo rm /etc/sudoers.d/pialert-backend
    ;;

  reporting_test)
    ## test reporting
    /usr/bin/python3 ${SCRIPTPATH}/pialert_reporting_test.py reporting_test
    echo "Test executed"
    ;;

  arg_list)
    echo disable_scan disable_mainscan disable_icmp_mon disable_service_mon disable_satellites enable_scan enable_mainscan enable_icmp_mon enable_service_mon enable_satellites reporting_test set_apikey set_autopassword set_login set_password set_permissions set_sudoers unset_login unset_sudoers update_db
    ;;

  *)
    echo "pialert-cli $SCRIPTVERSION (https://github.com/leiweibau/Pi.Alert)"
    echo "Use \"${bold}pialert-cli help${normal}\" for a list of supported commands."
esac
