#!/bin/bash
#
# wrd v1.3.1 August 2023
#
# Script to configure and register country code for CRDA
#
# Version   Date        Description
#   0.9     13 Jul 2021 Initial version with separate country code file
#   1.0     14 Jul 2021 Country codes now as bash array in script,
#                       Code is now registered immediately (iw reg set xx)
#   1.1     17 Jul 2021 Remove multiple option to prevent multiple selection
#   1.2     19 Jul 2021 Corrected error msg for inactive wifi
#   1.3     05 Aug 2023 Corrected text for GB
#   1.3.1   05 Aug 2023 Removed confusing message about active WiFi required
#
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# Country codes and descriptions according to ISO 3166 country codes
country_arr=("AE|United Arab Emirates"
  "AF|Afghanistan"
  "AL|Albania"
  "AM|Armenia"
  "AO|Angola"
  "AR|Argentina"
  "AT|Austria"
  "AU|Australia"
  "AZ|Azerbaijan"
  "BA|Bosnia and Herzegovina"
  "BB|Barbados"
  "BD|Bangladesh"
  "BE|Belgium"
  "BF|Burkina Faso"
  "BG|Bulgaria"
  "BH|Bahrain"
  "BI|Burundi"
  "BJ|Benin"
  "BL|Saint Barthélemy"
  "BM|Bermuda"
  "BN|Brunei Darussalam"
  "BO|Bolivia"
  "BR|Brazil"
  "BS|Bahamas"
  "BT|Bhutan"
  "BW|Botswana"
  "BY|Belarus"
  "BZ|Belize"
  "CA|Canada"
  "CD|Congo Democratic Republic"
  "CF|Central African Republic"
  "CG|Congo"
  "CH|Switzerland"
  "CI|Côte d'Ivoire"
  "CL|Chile"
  "CM|Cameroon"
  "CN|China"
  "CO|Colombia"
  "CR|Costa Rica"
  "CY|Cyprus"
  "CZ|Czechia"
  "DE|Germany"
  "DK|Denmark"
  "DO|Dominican Republic"
  "DZ|Algeria"
  "EC|Ecuador"
  "EE|Estonia"
  "EG|Egypt"
  "ES|Spain"
  "FI|Finland"
  "FR|France"
  "GB|UK (GB and N. Ireland)"
  "GE|Georgia"
  "GR|Greece"
  "GT|Guatemala"
  "HK|Hong Kong"
  "HN|Honduras"
  "HR|Croatia"
  "HU|Hungary"
  "ID|Indonesia"
  "IE|Ireland"
  "IL|Israel"
  "IN|India"
  "IR|Iran"
  "IS|Iceland"
  "IT|Italy"
  "JM|Jamaica"
  "JO|Jordan"
  "JP|Japan"
  "KE|Kenya"
  "KR|Korea"
  "KW|Kuwait"
  "KZ|Kazakhstan"
  "LB|Lebanon"
  "LI|Liechtenstein"
  "LK|Sri Lanka"
  "LT|Lithuania"
  "LU|Luxembourg"
  "LV|Latvia"
  "MA|Morocco"
  "MC|Monaco"
  "MD|Moldova"
  "ME|Montenegro"
  "MK|North Macedonia"
  "MO|Macao"
  "MT|Malta"
  "MX|Mexico"
  "MY|Malaysia"
  "MZ|Mozambique"
  "NA|Namibia"
  "NE|Niger"
  "NG|Nigeria"
  "NI|Nicaragua"
  "NL|Netherlands"
  "NO|Norway"
  "NP|Nepal"
  "NZ|New Zealand"
  "OM|Oman"
  "PA|Panama"
  "PE|Peru"
  "PG|Papua New Guinea"
  "PH|Philippines"
  "PK|Pakistan"
  "PL|Poland"
  "PR|Puerto Rico"
  "PT|Portugal"
  "PY|Paraguay"
  "QA|Qatar"
  "RO|Romania"
  "RS|Serbia"
  "RU|Russia"
  "RW|Rwanda"
  "SA|Saudi Arabia"
  "SE|Sweden"
  "SG|Singapore"
  "SI|Slovenia"
  "SK|Slovakia"
  "SN|Senegal"
  "SO|Somalia"
  "SR|Suriname"
  "SS|South Sudan"
  "SV|El Salvador"
  "SY|Syria"
  "TH|Thailand"
  "TN|Tunisia"
  "TR|Turkey"
  "TT|Trinidad and Tobago"
  "TW|Taiwan"
  "TZ|Tanzania"
  "UA|Ukraine"
  "UG|Uganda"
  "US|United States of America"
  "UY|Uruguay"
  "UZ|Uzbekistan"
  "VE|Venezuela"
  "VN|Viet Nam"
  "YE|Yemen"
  "ZA|South Africa"
  "ZM|Zambia"
  "ZW|Zimbabwe")

# Config file for CRDA
crdafile="/etc/sysconfig/network"

WIFIICON=/usr/share/icons/other_configuration.png

# Script must be run as root
run_as_root() {
  #########################################################
  ### Ensure this is run as root                          #
  #########################################################
  this_script_dir="$(cd "$(dirname "$0")" && pwd)"
  this_script="$(basename $0)"
  this_script_path_n_name="${this_script_dir}/${this_script}"
  GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
  if [ "$UID" != "0" ]; then
    yadtxt=" \n                             <b>*** ALERT ***</b>  \n\n"
    yadtxt=$yadtxt"               This script must be run as root \n\n"
    yadtxt=$yadtxt"                 Would you like to continue? \n"
    yad --center --window-icon=$WIFIICON --title="$TITLE" \
      --width 340 \
      --question \
      --button=Yes:0 \
      --button="Exit":1 \
      --buttons-layout=spread \
      --text="$yadtxt"
    if [ $? -eq 0 ]; then
      $GUI_SU $this_script_path_n_name
      exit
    else
      exit 0
    fi
  fi
}

# Let user select a country code and apply and register the CRDA
get_crda() {
  countrycode=""

  while [ "$countrycode" == "" ]; do
    countrycode=$(for ix in {0..139}; do
      ixcountry=${country_arr[ix]}
      echo $ixcountry | awk -F'|' '{print $1}'
      echo $ixcountry | awk -F'|' '{print $2}'
    done |
      yad --center --width=500 --height=500 --window-icon=$WIFIICON \
        --title "Select Regulatory Domain" \
        --button="Ok:0" --button="Exit:1" \
        --buttons-layout=center \
        --text-align=center \
        --text "$yadtxt" \
        --list \
        --column="Code" --column="Country")
    ret=$?
    [[ $ret -eq 1 ]] && exit 0
  done

  countrycode=$(echo $countrycode | awk -F'|' '{print $1}')

  yad --width=500 --window-icon=$WIFIICON --title "Regulatory Domain Set" \
    --text-align=center --buttons-layout=center \
    --text "\n\n    Country <b>$countrycode</b> selected.\n\nApplying and registering <b>$countrycode</b> now.\n\n Goodbye!\n\n" \
       --button="Ok:0"

  # Configure country code
  crda_line=$(grep CRDA_DOMAIN $crdafile && sed -i "/CRDA_DOMAIN/ s/CRDA_DOMAIN=.*/CRDA_DOMAIN=$countrycode/" $crdafile ||
    echo "CRDA_DOMAIN=$countrycode" >>$crdafile)

  # Register country code
  iw reg set $countrycode

 }

run_as_root
get_crda

exit

