#!/bin/bash
NEW_VERSION=181

RTKBASE_USER=rtkbase
RTKBASE_PATH=/usr/local/${RTKBASE_USER}
RTKBASE_GIT=${RTKBASE_PATH}/rtkbase
RTKBASE_UPDATE=${RTKBASE_PATH}/update
RTKBASE_TOOLS=${RTKBASE_GIT}/tools
RTKBASE_WEB=${RTKBASE_GIT}/web_app
RTKBASE_UNIT=${RTKBASE_GIT}/unit
RTKBASE_RECV=${RTKBASE_GIT}/receiver_cfg
BASEDIR=`realpath $(dirname $(readlink -f "$0"))`
BASENAME=`basename $(readlink -f "$0")`
ORIGDIR=`pwd`
#echo BASEDIR=${BASEDIR} BASENAME=${BASENAME}
RECVPORT=/dev/serial0
RTKBASE_INSTALL=rtkbase_install.sh
SET_BASE_POS=UnicoreSetBasePos.sh
UNICORE_SETTIGNS=UnicoreSettings.sh
UNICORE_CONFIGURE=UnicoreConfigure.sh
TAILSCALE_GET_HREF=tailscale_get_href.sh
SYSTEM_UPGRADE=system_upgrade.sh
EXEC_UPDATE=exec_update.sh
SETTINGS_NOW=${RTKBASE_GIT}/settings.conf
SETTINGS_SAVE=${RTKBASE_GIT}/settings.save
SETTINGS_DEFAULT=${RTKBASE_GIT}/settings.conf.default
NMEACONF=NmeaConf
RTCM3LED=Rtcm3Led
CONF_TAIL=RTCM3_OUT.txt
CONF980=UM980_${CONF_TAIL}
CONF982=UM982_${CONF_TAIL}
CONFBYNAV=Bynav_${CONF_TAIL}
CONFSEPTENTRIO=Septentrio_${CONF_TAIL}
TESTSEPTENTRIO=Septentrio_TEST.txt
SERVER_PATCH=server_py.patch
GNSS_RPROXY_PATCH=gnss_rproxy_server_py.patch
RTKBASE_CONFIG_MANAGER_PATCH=RTKBaseConfigManager_py.patch
STATUS_PATCH=status_js.patch
SETTING_PATCH=settings_js.patch
BASE_PATCH=base_html.patch
RUNCAST_PATCH=run_cast_sh.patch
SETTING_JS_PATCH=settings_js.patch
SETTING_HTML_PATCH=settings_html.patch
SETTINGS_CONF_PATCH=settings_conf_default.patch
PPP_CONF_PATCH=ppp_conf.patch
STR2STR_RTCM_SVR_PATCH=str2str_rtcm_svr.patch
STR2STR_TCP_PATCH=str2str_tcp.patch
STR2STR_NTRIP_A_PATCH=str2str_ntrip_A.patch
SYSCONGIG=RtkbaseSystemConfigure.sh
SYSSERVICE=RtkbaseSystemConfigure.service
NETWORK_EVENT=rtkbase_network_event.sh
CHECK_INTERNET=rtkbase_check_internet.sh
CHECK_INTERNET_SERVICE=rtkbase_check_internet.service
CHECK_SATELITES=rtkbase_check_satelites.sh
CHECK_SATELITES_SERVICE=rtkbase_check_satelites.service
SEPTENTRIO_NAT=rtkbase_septentrio_NAT.sh
SEPTENTRIO_NAT_SERVICE=rtkbase_septentrio_NAT.service
MODEM_WEB_PROXY_SERVICE=rtkbase_modem_web_proxy.service
DHCP_CONF=rtkbase_DHCP.conf
DHCP_SERVICE=rtkbase_DHCP.service
TUNE_POWER=tune_power.sh
CONFIG=config.txt
CONFIG_ORIG=config.original
RTKLIB=rtklib
SERVICE_PATH=/etc/systemd/system
NETWORK_DISPATHER_PATH=/usr/lib/NetworkManager/dispatcher.d
PI=pi
BANNER=/etc/ssh/sshd_config.d/rename_user.conf
FAVICON=favicon.ico
VERSION=version.txt
ELT0x33_RULES=99-ELT0x33.rules
START_ELT0x33=startELT0x33.sh
ONOFF_ELT0x33=onoffELT0x33.sh
NTRIP_LED=ntrip_led.sh
PBC=PBC.sh
SEPTENTRIO_LINK=70-usb-net-septentrio.link
MOBILE_LINK=70-usb-net-mobile.link
SEPTENTRIO_MODEM=77-mm-septentio-port-types.rules
AUTOCONNECT_CONF=autoconnect-retries-forever.conf
LINK_RULES=/usr/lib/systemd/network
MODEM_RULES=/lib/udev/rules.d
NETWORK_CONF=/usr/lib/NetworkManager/conf.d
REBOOT_SH=reboot.sh
RESET_RECEIVER=reset_receiver.sh
ONLINE_UPDATE=NO

lastcode=N
exitcode=0

ExitCodeCheck(){
  lastcode=$1
  if [[ $lastcode > $exitcode ]]
  then
     exitcode=${lastcode}
     #echo exitcode=${exitcode}
  fi
}

configure_cmdline(){
  CMDLINE=$1/cmdline.txt
  #echo \$1=${1} CMDLINE=${CMDLINE}

  if [[ -f ${CMDLINE} ]]
  then
     DONT_EDIT=`grep "^DO NOT EDIT THIS FILE" ${CMDLINE}`
     #echo DONT_EDIT_${CMDLINE}=${DONT_EDIT}
     if [[ "${DONT_EDIT}" == "" ]]
     then
        HAVE_CONSOLE_LOGIN=`grep "console=serial0" ${CMDLINE}`
        #echo HAVE_CONSOLE_LOGIN=${HAVE_CONSOLE_LOGIN}

        if [[ ${HAVE_CONSOLE_LOGIN} != "" ]]
        then
           sed -i s/console=serial[0-9],[0-9]*\ //  "${CMDLINE}"
           ExitCodeCheck $?
           #cat ${CMDLINE}
           #echo
           echo Cnahged ${CMDLINE}
           NEEDREBOOT=Y
        fi
     fi
  fi
}

configure_config(){
  HAVE_PI4=`cat /proc/cpuinfo | grep Model | grep "Pi 4"`
  BOOTCONFIG=$1/config.txt
  #echo \$1=${1} BOOTCONFIG=${BOOTCONFIG} HAVE_PI4=${HAVE_PI4}
  if [[ "${HAVE_PI4}" != "" ]] && [[ -f ${BOOTCONFIG} ]]; then
     DONT_EDIT=`grep "^DO NOT EDIT THIS FILE" ${BOOTCONFIG}`
     #echo DONT_EDIT_${BOOTCONFIG}=${DONT_EDIT}
     if [[ "${DONT_EDIT}" == "" ]]
     then
        HAVE_UART=`grep "^enable_uart=" ${BOOTCONFIG}`
        #echo HAVE_UART=${HAVE_UART}
        ENABLED_UART=`grep "^enable_uart=1" ${BOOTCONFIG}`
        #echo ENABLED_UART=${ENABLED_UART}
        HAVE_MINI_BT=`grep "^dtoverlay=miniuart-bt" ${BOOTCONFIG}`
        #echo HAVE_MINI_BT=${HAVE_MINI_BT}
        HAVE_CORE_FREQ=`grep "^core_freq=250" ${BOOTCONFIG}`
        #echo HAVE_CORE_FREQ=${HAVE_CORE_FREQ}

        START_LINE=0
        while : ; do
           PI4_SECTION=`tail -n+${START_LINE} ${BOOTCONFIG} | awk '/^\[pi4\]/ {print NR+1; exit 0; }'`
           #echo before PI4_SECTION=${PI4_SECTION}
           if [[ ${PI4_SECTION} != "" ]]; then
              if [[ ${START_LINE} != 0 ]]; then
                 let "PI4_SECTION=${PI4_SECTION}+${START_LINE}+1"
                 #echo after PI4_SECTION=${PI4_SECTION} START_LINE=${START_LINE}
              fi
              NEXT_SECTION=`tail -n+${PI4_SECTION} ${BOOTCONFIG} | awk '/^\[/ {print NR; exit 0; }'`
              #echo NEXT_SECTION=${NEXT_SECTION}
              if [[ ${NEXT_SECTION} == "" ]]; then
                 #echo tail -n+${PI4_SECTION} ${BOOTCONFIG}
                 #tail -n+${PI4_SECTION} ${BOOTCONFIG}
                 HAVE_OTG=`tail -n+${PI4_SECTION} ${BOOTCONFIG} | grep "^otg_mode=1"`
                 break
              else
                 let "NEXT_SECTION=${NEXT_SECTION}-1"
                 #echo tail -n+${PI4_SECTION} ${BOOTCONFIG} \| head -n+${NEXT_SECTION}
                 #tail -n+${PI4_SECTION} ${BOOTCONFIG} | head -n+${NEXT_SECTION}
                 HAVE_OTG=`tail -n+${PI4_SECTION} ${BOOTCONFIG} | head -n+${NEXT_SECTION} | grep "^otg_mode=1"`
              fi
              if [[ ${HAVE_OTG} != "" ]]; then
                 break
              fi
              let "START_LINE=${PI4_SECTION}-1"
           else
              break
           fi

        done
        #echo after loop HAVE_OTG=${HAVE_OTG}

        if [[ ${HAVE_UART} == "" ]] || [[ ${HAVE_MINI_BT} == "" ]] || [[ ${HAVE_CORE_FREQ} == "" ]]
        then
           echo [all] >> ${BOOTCONFIG}
           echo >> ${BOOTCONFIG}
           NEEDREBOOT=Y
        fi

        if [[ ${HAVE_UART} == "" ]]
        then
           echo enable_uart=1 >> ${BOOTCONFIG}
           echo Uart0 added to ${BOOTCONFIG}
        elif [[ ${ENABLED_UART} == "" ]]
        then
           sed -i s/^enable_uart=.*/enable_uart=1/  "${BOOTCONFIG}"
           ExitCodeCheck $?
           echo Uart0 enabled at ${BOOTCONFIG}
           NEEDREBOOT=Y
        fi

        if [[ ${HAVE_MINI_BT} == "" ]]
        then
           echo dtoverlay=miniuart-bt >> ${BOOTCONFIG}
           echo Bluetooth change to miniUART into ${BOOTCONFIG}
        fi

        if [[ ${HAVE_CORE_FREQ} == "" ]]
        then
           echo core_freq=250 >> ${BOOTCONFIG}
           echo Core freq added to ${BOOTCONFIG}
        fi

        if [[ ${HAVE_OTG} == "" ]]
        then
           echo [pi4] >> ${BOOTCONFIG}
           echo otg_mode=1 >> ${BOOTCONFIG}
           echo Otg mode added to ${BOOTCONFIG}
           NEEDREBOOT=Y
        fi
     fi
  fi
}

replace_config(){
  BOOTCONFIG=$1/${CONFIG}
  NEWCONFIG=${BASEDIR}/${CONFIG}
  #echo \$1=${1} BOOTCONFIG=${BOOTCONFIG} NEWCONFIG=${NEWCONFIG}
  if [[ -f ${BOOTCONFIG} ]]
  then
     DONT_EDIT=`grep "^DO NOT EDIT THIS FILE" ${BOOTCONFIG}`
     #echo DONT_EDIT_${BOOTCONFIG}=${DONT_EDIT}
     if [[ "${DONT_EDIT}" == "" ]]
     then
        #echo diff -q ${NEWCONFIG} ${BOOTCONFIG}
        IS_DIFF=`diff -q ${NEWCONFIG} ${BOOTCONFIG}`
        #echo IS_DIFF=${IS_DIFF}
        if [[ "${IS_DIFF}" != "" ]]
        then
           #echo diff -q ${BASEDIR}/${CONFIG_ORIG} ${BOOTCONFIG}
           IS_ORIG=`diff -q ${BASEDIR}/${CONFIG_ORIG} ${BOOTCONFIG}`
           #echo IS_ORIG=${IS_ORIG}
           if [[ "${IS_ORIG}" == "" ]]
           then
              #echo cp ${BOOTCONFIG} ${BOOTCONFIG}.old
              cp ${BOOTCONFIG} ${BOOTCONFIG}.old
              ExitCodeCheck $?
              #echo mv ${NEWCONFIG} ${BOOTCONFIG}
              mv ${NEWCONFIG} ${BOOTCONFIG}
              ExitCodeCheck $?
              NEEDREBOOT=Y
              echo ${BOOTCONFIG} replaced. Old version in the ${BOOTCONFIG}.old
           else
              echo ${BOOTCONFIG} is not original. Change instead of replace
              configure_config $1
           fi
        else
           echo ${BOOTCONFIG} is already replaced
        fi
     fi
  fi
}

delete_all_extracted(){
  if [[ "${FILES_EXTRACT}" != "" ]]
  then
     #echo rm -rf ${FILES_EXTRACT}
     rm -rf ${FILES_EXTRACT}
  fi
}

check_version(){
  echo '################################'
  echo 'CHECK VERSION'
  echo '################################'
  if [ -f ${BASEDIR}/${VERSION} ]
  then
     NEW_VERSION=`cat ${BASEDIR}/${VERSION}`
     ExitCodeCheck $?
  fi
  #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]] && [ -f ${RTKBASE_PATH}/${VERSION} ]
  then
     UPDATE=Y
     OLD_VERSION=`cat ${RTKBASE_PATH}/${VERSION}`
     ExitCodeCheck $?
     #echo NEW_VERSION=${NEW_VERSION} OLD_VERSION=${OLD_VERSION}
     if [ "${NEW_VERSION}" -lt "${OLD_VERSION}" ]
     then
        echo Already installed version'('${OLD_VERSION}')' is newer, than install.sh version'('${NEW_VERSION}')'. Exiting
        delete_all_extracted
        exit
     else
        echo Update from version ${OLD_VERSION} to version ${NEW_VERSION}
        if [ -f ${SETTINGS_NOW} ]
        then
           #echo sudo -u "${RTKBASE_USER}" cp ${SETTINGS_NOW} ${SETTINGS_SAVE}
           sudo -u "${RTKBASE_USER}" cp ${SETTINGS_NOW} ${SETTINGS_SAVE}
           ExitCodeCheck $?
        fi
     fi
  else
     echo Install version ${NEW_VERSION}
     OLD_VERSION=${NEW_VERSION}
     UPDATE=N
  fi
}

check_boot_configiration(){
   echo '################################'
   if have_full
   then
      echo 'CHECK BOOT CONFIGURATION'
   else
      echo 'REPLACE BOOT CONFIGURATION'
   fi
   echo '################################'

   configure_cmdline /boot
   configure_cmdline /boot/firmware

   if have_full
   then
      configure_config /boot
      configure_config /boot/firmware
   else
      replace_config /boot
      replace_config /boot/firmware
   fi
   #hciuart_enabled=$(systemctl is-enabled hciuart.service)
   #[[ "${hciuart_enabled}" != "disabled" ]] && [[ "${hciuart_enabled}" != "masked" ]] && systemctl disable hciuart
}

is_packet_not_installed(){
   instaled=`dpkg-query -W ${1} 2>/dev/null | grep ${1}`
   #echo 1=${1} instaled=${instaled}
   if [[ ${instaled} != "" ]]
   then
      return 1
   fi
}

NEED_INSTALL=
install_packet_if_not_installed(){
   is_packet_not_installed ${1} && NEED_INSTALL="${NEED_INSTALL} ${1}"
   #echo NEED_INSTALL=${NEED_INSTALL} \$\1=${1}
}

check_platform(){
   platform=$(uname -m)
   if [[ ! ${platform} =~ 'aarch64' ]] && [[ ! ${platform} =~ 'armv7l' ]]; then
      echo ELT_RTKBase cannot be installed on ${platform} architecture
      exit 1
   fi
}

restart_as_root(){
   WHOAMI=`whoami`
   if [[ ${WHOAMI} != "root" ]]
   then
      #echo sudo ${0} ${1}
      sudo ${0} ${1}
      #echo exit after sudo
      exit $?
   fi
   #echo i am ${WHOAMI}$
}

do_reboot(){
   #echo NEEDREBOOT=${NEEDREBOOT}
   if [[ ${NEEDREBOOT} == "Y" ]]; then
      if [[ "${1}" != "END" ]]; then
         echo Please try again ${0} after reboot
      fi
      delete_all_extracted
      echo Rebooting now!!!!
      reboot now
      exit
   fi

}

info_reboot(){
   #echo NEEDREBOOT=${NEEDREBOOT}
   if [[ ${NEEDREBOOT} == "Y" ]]
   then
      echo Please REBOOT, because start configuration changed!
   fi
}

install_additional_utilies(){
   echo '################################'
   echo 'INSTALL ADDITIONAL UTILITIES'
   echo '################################'

   NEED_INSTALL=
   install_packet_if_not_installed avahi-utils
   install_packet_if_not_installed avahi-daemon
   install_packet_if_not_installed uuid
   install_packet_if_not_installed cpufrequtils
   install_packet_if_not_installed uhubctl
   install_packet_if_not_installed ntpdate
   install_packet_if_not_installed gpiod
   install_packet_if_not_installed raspi-utils-core

   #echo NEED_INSTALL=${NEED_INSTALL}
   if [[ "${NEED_INSTALL}" != "" ]]
   then
      apt-get install -q -y ${NEED_INSTALL}
      ExitCodeCheck $?
      NEED_INSTALL=
   fi
}

install_tailscale(){
   if ! type tailscale >/dev/null 2>&1; then
      echo '################################'
      echo 'INSTALL TAILSCALE'
      echo '################################'
      #echo curl -fsSL https://tailscale.com/install.sh \| sh
      curl -fsSL https://tailscale.com/install.sh | sh
      ExitCodeCheck $?
      if type tailscale >/dev/null 2>&1; then
         #echo mkdir -p /etc/bash_completion.d
         mkdir -p /etc/bash_completion.d
         ExitCodeCheck $?
         #echo tailscale completion bash \> /etc/bash_completion.d/tailscale
         tailscale completion bash > /etc/bash_completion.d/tailscale
         ExitCodeCheck $?
         #echo source \<\(tailscale completion bash\)
         #source <(tailscale completion bash)
         #ExitCodeCheck $?
      fi
   fi
   #tailscale status
}

delete_pi_user(){
   FOUND=`sed 's/:.*//' /etc/passwd | grep "${PI}"`
   if [[ -n "${FOUND}" ]]
   then
      echo '################################'
      echo 'DELETE PI USER'
      echo '################################'
      userdel -r "${PI}"
      ExitCodeCheck $?
   fi
   if [[ -f "${BANNER}" ]]
   then
      rm -r "${BANNER}"
      if ! ischroot && systemctl is-active --quiet sshd; then
         systemctl restart sshd
      fi
   fi
}

change_hostname(){
   echo '################################'
   echo 'CHANGE HOSTNAME IF STANDART'
   echo '################################'
   STANDART_HOST=raspberrypi
   #STANDART_HOST=rtkbase
   RTKBASE_HOST=${RTKBASE_USER}
   #RTKBASE_HOST=raspberrypi
   CHANGE_HOST_RTKBASE=Y
   CHANGE_HOST_NOW=N

   NOW_HOST=`hostname`
   #echo NOW_HOST=$NOW_HOST
   if [[ $NOW_HOST != $STANDART_HOST ]]
   then
      CHANGE_HOST_RTKBASE=N
   fi

   HOSTNAME=/etc/hostname
   NOW_HOSTNAME=`cat $HOSTNAME`
   #echo NOW_HOSTNAME=$NOW_HOSTNAME
   if [[ $NOW_HOSTNAME != $STANDART_HOST ]]
   then
      CHANGE_HOST_RTKBASE=N
   fi
   if [[ $NOW_HOSTNAME != $NOW_HOST ]]
   then
       CHANGE_HOST_NOW=Y
   fi

   HOSTS=/etc/hosts
   NOW_HOSTS=`grep "127.0.1.1" $HOSTS | tail -n-1 | awk -F ' ' '{print $2}'`
   #echo NOW_HOSTS=$NOW_HOSTS
   if [[ $NOW_HOSTS != $STANDART_HOST ]]
   then
      CHANGE_HOST_RTKBASE=N
   fi
   if [[ $NOW_HOSTS != $NOW_HOST ]]
   then
       CHANGE_HOST_NOW=Y
   fi

   #echo 1=${1}
   RESTART_AVAHI=N
   if [[ $CHANGE_HOST_RTKBASE = Y ]]
   then
      echo Set \"$RTKBASE_HOST\" as host
      hostname $RTKBASE_HOST
      echo $RTKBASE_HOST >$HOSTNAME
      sed -i s/127\.0\.1\.1.*/127\.0\.1\.1\ $RTKBASE_HOST/ "$HOSTS"
      ExitCodeCheck $?
      RESTART_AVAHI=Y
   elif [[ $CHANGE_HOST_NOW = Y ]]
   then
      if [[ "${1}" != "0" ]]
      then
         echo Set \"$NOW_HOST\" as host
         echo $NOW_HOST >$HOSTNAME
         sed -i s/127\.0\.1\.1.*/127\.0\.1\.1\ $NOW_HOST/ "$HOSTS"
         ExitCodeCheck $?
         RESTART_AVAHI=Y
      else
         echo WARNING!!! hostname=$NOW_HOST /etc/hostname=$NOW_HOSTNAME /etc/hosts resolve $NOW_HOSTS
      fi
   fi
   if ! ischroot
   then
      if [[ $RESTART_AVAHI = Y ]]
      then
         systemctl is-active --quiet avahi-daemon && sudo systemctl restart avahi-daemon
      fi
   fi
}

unpack_files(){
   if [[ "${FILES_EXTRACT}" != "" ]]
   then
      echo '################################'
      echo 'UNPACK FILES'
      echo '################################'

      # Find __ARCHIVE__ marker, read archive content and decompress it
      ARCHIVE=$(awk '/^__ARCHIVE__/ {print NR + 1; exit 0; }' "${0}")
      # Check if there is some content after __ARCHIVE__ marker (more than 100 lines)
      [[ $(sed -n '/^__ARCHIVE__/,$p' "${0}" | wc -l) -lt 100 ]] && echo "ELT_RTKBase isn't bundled inside ${0}" && exit 1
      #echo tail -n+${ARCHIVE} "${0}" \| tar xpJv --no-same-owner --no-same-permissions  --wildcards -C ${BASEDIR} ${FILES_EXTRACT}
      tail -n+${ARCHIVE} "${0}" | tar xpJv --no-same-owner --no-same-permissions  --wildcards -C ${BASEDIR} ${FILES_EXTRACT} >/dev/null
      ExitCodeCheck $?
   fi
}

stop_rtkbase_services(){
  if ! ischroot
  then
     echo '################################'
     echo 'STOP RTKBASE SERVICES'
     echo '################################'
     serviceList="str2str_ntrip_A.service \
                  str2str_ntrip_B.service \
                  str2str_local_ntrip_caster \
                  str2str_rtcm_svr.service \
                  str2str_rtcm_client.service \
                  str2str_rtcm_udp_svr.service \
                  str2str_rtcm_udp_client.service \
                  str2str_rtcm_serial.service \
                  str2str_file.service \
                  str2str_tcp.service \
                  rtkrcv_raw2nmea.service \
                  rtkbase_archive.service \
                  rtkbase_archive.timer \
                  modem_check.service \
                  modem_check.timer \
                  rtkbase_gnss_web_proxy.service \
                  ${MODEM_WEB_PROXY_SERVICE} \
                  ${SYSSERVICE} \
                  ${CHECK_INTERNET_SERVICE} \
                  ${CHECK_SATELITES_SERVICE} \
                  ${SEPTENTRIO_NAT_SERVICE} \
                  ${DHCP_SERVICE}"
     if [[ "${ONLINE_UPDATE}" != "UPDATE" ]]; then
        serviceList="${serviceList} rtkbase_web.service"
     fi
     #echo serviceList=${serviceList}
     for service_name in ${serviceList}
     do
         service_active=$(systemctl is-active "${service_name}")
         if [ "${service_active}" != "inactive" ]
         then
            #echo ${service_name} is ${service_active}
            #echo systemctl stop "${service_name}"
            systemctl stop "${service_name}"
            if [ "${service_name}" = "str2str_tcp.service" ]
            then
               need_sleep=Y
            fi
         fi
     done
     if [ "${need_sleep}" = "Y" ]
     then
        #echo sleep 2
        sleep 2
     fi
  fi
}

add_rtkbase_user(){
   echo '################################'
   echo 'ADD RTKBASE USER'
   echo '################################'

   HAVEUSER=`cat /etc/passwd | grep ${RTKBASE_USER}`
   #echo HAVEUSER=${HAVEUSER}
   if [[ ${HAVEUSER} == "" ]]
   then
      HAVE_DEB12=`lsb_release -c | grep bookworm`
      if [[ "${HAVE_DEB12}" != "" ]]; then
         UID990="--firstuid 800 --lastuid 999"
      else
         UID990=
      fi
      #echo adduser --gecos "RTKBase user" ${UID990} --disabled-password --home ${RTKBASE_PATH} --shell /usr/sbin/nologin ${RTKBASE_USER}
      adduser --gecos "RTKBase user" ${UID990} --disabled-password --home ${RTKBASE_PATH} --shell /usr/sbin/nologin ${RTKBASE_USER}
      ExitCodeCheck $?
      # --gecos instead --comment for raspbian 11
   fi

   HAVEUSER=`cat /etc/passwd | grep ${RTKBASE_USER}`
   #echo HAVEUSER=${HAVEUSER}
   if [[ ${HAVEUSER} == "" ]]
   then
      echo Failed to create ${RTKBASE_USER} user
      delete_all_extracted
      exit
   fi

   usermod -a -G plugdev,dialout,gpio ${RTKBASE_USER}
   ExitCodeCheck $?

   RTKBASE_SUDOER=/etc/sudoers.d/${RTKBASE_USER}
   #echo RTKBASE_SUDOER=${RTKBASE_SUDOER}
   if [[ ! -f "${RTKBASE_SUDOER}" ]]
   then
      #echo echo "rtkbase ALL=NOPASSWD: ALL" \> ${RTKBASE_SUDOER}
      echo "rtkbase ALL=NOPASSWD: ALL" > ${RTKBASE_SUDOER}
   fi

   if [[ ! -d "${RTKBASE_PATH}" ]]
   then
      #echo mkdir ${RTKBASE_PATH}
      mkdir ${RTKBASE_PATH}
      ExitCodeCheck $?
   fi

   #echo chown rtkbase:rtkbase ${RTKBASE_PATH}
   chown rtkbase:rtkbase ${RTKBASE_PATH}
   ExitCodeCheck $?
   #echo chmod 755 ${RTKBASE_PATH}
   chmod 755 ${RTKBASE_PATH}
   ExitCodeCheck $?

   if [[ ! -d "${RTKBASE_UPDATE}" ]]
   then
      #echo mkdir ${RTKBASE_UPDATE}
      mkdir ${RTKBASE_UPDATE}
      ExitCodeCheck $?
   fi

   if [[ -d "${RTKBASE_GIT}" ]]
   then
      #echo chown -R rtkbase:rtkbase ${RTKBASE_GIT}
      chown -R rtkbase:rtkbase ${RTKBASE_GIT}
   fi
}

doPatch(){
  patch -f ${1} ${2}
  ExitCodeCheck $?
}

install_rtklib() {
    echo '################################'
    echo 'INSTALLING RTKLIB'
    echo '################################'
    platform=$(uname -m)
    RTKLIB_DIR=${BASEDIR}/${RTKLIB}/${platform}
    if [[ -d ${RTKLIB_DIR} ]] && lsb_release -c | grep -q 'bookworm'; then
       echo RtkLib copyed from ${RTKLIB_DIR}
       LOCAL_BIN=/usr/local/bin/
       #echo chmod 711 ${RTKLIB_DIR}/*
       chmod 711 ${RTKLIB_DIR}/*
       #echo mv ${RTKLIB_DIR}/* ${LOCAL_BIN}
       mv ${RTKLIB_DIR}/* ${LOCAL_BIN}
       ExitCodeCheck $?
    else
       echo RtkLib will be compiled from original and patches
       echo Get Rtklib 2.4.3 b34j release and unnpack it
       sudo -u "${RTKBASE_USER}" wget -qO - https://github.com/rtklibexplorer/RTKLIB/archive/refs/tags/b34j.tar.gz | tar -xvz >/dev/null
       #Install Rtklib app
       RTKLIB_CURDIR=`pwd`
       RTKLIB_GIT=${RTKLIB_CURDIR}/RTKLIB-b34j
       RTKLIB_PATCH=${BASEDIR}/${RTKLIB}
       doPatch ${RTKLIB_GIT}/src/stream.c ${RTKLIB_PATCH}/stream.patch
       doPatch ${RTKLIB_GIT}/src/streamsvr.c ${RTKLIB_PATCH}/streamsvr.patch
       doPatch ${RTKLIB_GIT}/src/rinex.c ${RTKLIB_PATCH}/rinex.patch
       doPatch ${RTKLIB_GIT}/app/consapp/str2str/str2str.c ${RTKLIB_PATCH}/str2str.patch
       doPatch ${RTKLIB_GIT}/app/consapp/str2str/gcc/makefile ${RTKLIB_PATCH}/str2str_makefile.patch
       doPatch ${RTKLIB_GIT}/app/consapp/convbin/convbin.c ${RTKLIB_PATCH}/convbin.patch
       doPatch ${RTKLIB_GIT}/app/consapp/convbin/gcc/makefile ${RTKLIB_PATCH}/convbin_makefile.patch
       doPatch ${RTKLIB_GIT}/app/consapp/rtkrcv/gcc/makefile ${RTKLIB_PATCH}/rtkrcv_makefile.patch
       #TODO add correct CTARGET in makefile?
       echo Compile and install
       make -s --directory=${RTKLIB_GIT}/app/consapp/str2str/gcc
       make -s --directory=${RTKLIB_GIT}/app/consapp/str2str/gcc install
       make -s --directory=${RTKLIB_GIT}/app/consapp/rtkrcv/gcc
       make -s --directory=${RTKLIB_GIT}/app/consapp/rtkrcv/gcc install
       make -s --directory=${RTKLIB_GIT}/app/consapp/convbin/gcc
       make -s --directory=${RTKLIB_GIT}/app/consapp/convbin/gcc install
       #deleting RTKLIB
       rm -rf ${RTKLIB_GIT}
    fi
    #ls -la /usr/local/bin/
    #echo rm -rf ${BASEDIR}/${RTKLIB}
    rm -rf ${BASEDIR}/${RTKLIB}
    ExitCodeCheck $?
}

copy_rtkbase_install_file(){
  echo '################################'
  echo 'COPY RTKBASE INSTALL FILE'
  echo '################################'

  CACHE_PIP=${RTKBASE_PATH}/.cache/pip
  #echo CACHE_PIP=${CACHE_PIP}
  if [[ ! -d ${CACHE_PIP} ]]
  then
     #echo mkdir -p ${CACHE_PIP}
     mkdir -p ${CACHE_PIP}
     ExitCodeCheck $?
  fi
  #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${CACHE_PIP}
  chown ${RTKBASE_USER}:${RTKBASE_USER} ${CACHE_PIP}
  ExitCodeCheck $?

  #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]
  then
     #echo mv ${BASEDIR}/${RTKBASE_INSTALL} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${RTKBASE_INSTALL} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${RTKBASE_INSTALL}
  chmod +x ${RTKBASE_PATH}/${RTKBASE_INSTALL}
  ExitCodeCheck $?

  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]
  then
     #echo mv ${BASEDIR}/${VERSION} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${VERSION} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
}

install_rtkbase_system_configure(){
  echo '################################'
  echo 'INSTALL RTKBASE SYSTEM CONFIGURE'
  echo '################################'

  #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]
  then
     #echo mv ${BASEDIR}/${SYSCONGIG} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${SYSCONGIG} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${SYSCONGIG}
  chmod +x ${RTKBASE_PATH}/${SYSCONGIG}
  ExitCodeCheck $?

  #echo mv ${BASEDIR}/${SYSSERVICE} ${SERVICE_PATH}/
  mv ${BASEDIR}/${SYSSERVICE} ${SERVICE_PATH}/
  ExitCodeCheck $?

  #echo mv ${BASEDIR}/${NETWORK_EVENT} ${NETWORK_DISPATHER_PATH}/
  mv ${BASEDIR}/${NETWORK_EVENT} ${NETWORK_DISPATHER_PATH}/
  ExitCodeCheck $?
  #echo chmod +x ${NETWORK_DISPATHER_PATH}/${NETWORK_EVENT}
  chmod +x ${NETWORK_DISPATHER_PATH}/${NETWORK_EVENT}
  ExitCodeCheck $?

  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]; then
     #echo mv ${BASEDIR}/${CHECK_INTERNET} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${CHECK_INTERNET} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${CHECK_INTERNET}
  chmod +x ${RTKBASE_PATH}/${CHECK_INTERNET}
  ExitCodeCheck $?

  #echo mv ${BASEDIR}/${CHECK_INTERNET_SERVICE} ${SERVICE_PATH}/
  mv ${BASEDIR}/${CHECK_INTERNET_SERVICE} ${SERVICE_PATH}/
  ExitCodeCheck $?

  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]; then
     #echo mv ${BASEDIR}/${CHECK_SATELITES} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${CHECK_SATELITES} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${CHECK_SATELITES}
  chmod +x ${RTKBASE_PATH}/${CHECK_SATELITES}
  ExitCodeCheck $?

  #echo mv ${BASEDIR}/${CHECK_SATELITES_SERVICE} ${SERVICE_PATH}/
  mv ${BASEDIR}/${CHECK_SATELITES_SERVICE} ${SERVICE_PATH}/
  ExitCodeCheck $?

  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]; then
     #echo mv ${BASEDIR}/${DHCP_CONF} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${DHCP_CONF} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${DHCP_CONF}
  chmod +x ${RTKBASE_PATH}/${DHCP_CONF}
  ExitCodeCheck $?

  #echo mv ${BASEDIR}/${DHCP_SERVICE} ${SERVICE_PATH}/
  mv ${BASEDIR}/${DHCP_SERVICE} ${SERVICE_PATH}/
  ExitCodeCheck $?

  if ! ischroot; then
     #echo systemctl daemon-reload
     systemctl daemon-reload
  fi

  #echo systemctl enable ${SYSSERVICE}
  systemctl enable ${SYSSERVICE}
  ExitCodeCheck $?

  #echo systemctl enable ${CHECK_INTERNET_SERVICE}
  systemctl enable ${CHECK_INTERNET_SERVICE}
  ExitCodeCheck $?
  #echo systemctl enable ${CHECK_SATELITES_SERVICE}
  systemctl enable ${CHECK_SATELITES_SERVICE}
  ExitCodeCheck $?
}

install_tune_power(){
  echo '################################'
  echo 'INSTALL POWER TUNE'
  echo '################################'

  #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
  if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]
  then
     #echo mv ${BASEDIR}/${TUNE_POWER} ${RTKBASE_PATH}/
     mv ${BASEDIR}/${TUNE_POWER} ${RTKBASE_PATH}/
     ExitCodeCheck $?
  fi
  #echo chmod +x ${RTKBASE_PATH}/${TUNE_POWER}
  chmod +x ${RTKBASE_PATH}/${TUNE_POWER}
  ExitCodeCheck $?
}

correct_units(){
   echo '################################'
   echo 'CORRECT UNITS'
   echo '################################'

   STR2STR_TCP=${RTKBASE_UNIT}/str2str_tcp.service
   #echo STR2STR_TCP=${STR2STR_TCP}
   patch -f ${STR2STR_TCP} ${BASEDIR}/${STR2STR_TCP_PATCH}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${STR2STR_TCP_PATCH}
   ExitCodeCheck $?

   STR2STR_NTRIP_A=${RTKBASE_UNIT}/str2str_ntrip_A.service
   #echo STR2STR_NTRIP_A=${STR2STR_NTRIP_A}
   patch -f ${STR2STR_NTRIP_A} ${BASEDIR}/${STR2STR_NTRIP_A_PATCH}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${STR2STR_NTRIP_A_PATCH}
   ExitCodeCheck $?

   STR2STR_RTCM_SVR=${RTKBASE_UNIT}/str2str_rtcm_svr.service
   #echo STR2STR_RTCM_SVR=${STR2STR_RTCM_SVR}
   patch -f ${STR2STR_RTCM_SVR} ${BASEDIR}/${STR2STR_RTCM_SVR_PATCH}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${STR2STR_RTCM_SVR_PATCH}
   ExitCodeCheck $?

   for file in ${RTKBASE_UNIT}/str2str*
   do
      #echo sudo -u "${RTKBASE_USER}" sed -i s/^LogRateLimitBurst=.*/LogRateLimitBurst=100/ "${file}"
      sudo -u "${RTKBASE_USER}" sed -i s/^LogRateLimitBurst=.*/LogRateLimitBurst=100/ "${file}"
      ExitCodeCheck $?
   done
}

rtkbase_install(){
   #echo ${RTKBASE_PATH}/${RTKBASE_INSTALL} -u ${RTKBASE_USER} -j -d
   ${RTKBASE_PATH}/${RTKBASE_INSTALL} -u ${RTKBASE_USER} -j -d
   ExitCodeCheck $?
   if [ $lastcode != 0 ]
   then
      echo BUG: ${RTKBASE_INSTALL} -j -d finished with exitcode = $lastcode
      #ls -la ${RTKBASE_PATH}/${RTKBASE_INSTALL}
   fi

   correct_units

   #echo ${RTKBASE_PATH}/${RTKBASE_INSTALL} -u ${RTKBASE_USER} -t -g
   ${RTKBASE_PATH}/${RTKBASE_INSTALL} -u ${RTKBASE_USER} -t -g
   ExitCodeCheck $?
   if [ $lastcode != 0 ]
   then
      echo BUG: ${RTKBASE_INSTALL} -t -g finished with exitcode = $lastcode
      #ls -la ${RTKBASE_PATH}/${RTKBASE_INSTALL}
   fi

   #echo rm -f ${RTKBASE_PATH}/${RTKBASE_INSTALL}
   rm -f ${RTKBASE_PATH}/${RTKBASE_INSTALL}
   #echo chown -R ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}
   #chown -R ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}
   #ExitCodeCheck $?
}

restart_rtkbase_if_started(){
   if [[ "${ONLINE_UPDATE}" != "UPDATE" ]]; then
      if ! ischroot
      then
         systemctl is-active --quiet rtkbase_web.service && sudo systemctl restart rtkbase_web.service
      fi
   fi
}

rename_net_device(){
   OLD_DEVICE_ETH="${1}"
   NEW_DEVICE_ETH="${2}"
   UUID=`nmcli --fields UUID,DEVICE con show | grep "${OLD_DEVICE_ETH}" | awk -F ' ' '{print $1}'`
   if [[ "${UUID}" != "" ]]; then
      #echo nmcli connection delete uuid "${UUID}"
      nmcli connection delete uuid "${UUID}"
      ExitCodeCheck $?
   fi
   #echo ip link set ${OLD_DEVICE_ETH} down
   ip link set ${OLD_DEVICE_ETH} down
   ExitCodeCheck $?
   #echo ip link set ${OLD_DEVICE_ETH} name ${NEW_DEVICE_ETH}
   ip link set ${OLD_DEVICE_ETH} name ${NEW_DEVICE_ETH}
   ExitCodeCheck $?
   #echo ip link set ${NEW_DEVICE_ETH} up
   ip link set ${NEW_DEVICE_ETH} up
   ExitCodeCheck $?
   #echo nmcli device up ${NEW_DEVICE_ETH}
   nmcli device up ${NEW_DEVICE_ETH}
   ExitCodeCheck $?
   #echo exitcode=$exitcode
}

configure_for_septentrio(){
   if [[ ! -f ${LINK_RULES}/${SEPTENTRIO_LINK} ]]; then
      NEW_MOSAIC_ETH=septentrio
      OLD_MOSAIC_ETH=$(ip route | grep 192.168.3.0 | awk -F ' ' '{print $3}')
      #echo OLD_MOSAIC_ETH=${OLD_MOSAIC_ETH} NEW_MOSAIC_ETH=${NEW_MOSAIC_ETH}
      if [[ "${OLD_MOSAIC_ETH}" != "" ]] && [[ "${OLD_MOSAIC_ETH}" != "${NEW_MOSAIC_ETH}" ]]; then
         echo '################################'
         echo 'CONFIGURE USB-ETH FOR SEPTENTRIO'
         echo '################################'
         rename_net_device "${OLD_MOSAIC_ETH}" "${NEW_MOSAIC_ETH}"
      fi
   fi
}

configure_for_mobile(){
   if [[ ! -f ${LINK_RULES}/${MOBILE_LINK} ]]; then
      NEW_MOBILE_ETH=mobile
      for productpath in $(find /sys/bus/usb/devices/usb*/ -name product); do
          product=`cat "${productpath}"`
          if [[ "${product}" == "Mobile" ]]; then
             syspath=$(dirname "${productpath}")
             netpath=`find "${syspath}" -name net -type d | head -n 1`
             ethpath=`find "${netpath}" -name eth* -type d | head -n 1`
             OLD_MOBILE_ETH=$(basename "${ethpath}")
             #echo ethpath="${ethpath}" OLD_MOBILE_ETH=${OLD_MOBILE_ETH}
             break
          fi
      done
      echo OLD_MOBILE_ETH=${OLD_MOBILE_ETH} NEW_MOBILE_ETH=${NEW_MOBILE_ETH}
      if [[ "${OLD_MOBILE_ETH}" != "" ]] && [[ "${OLD_MOBILE_ETH}" != "${NEW_MOBILE_ETH}" ]]; then
         echo '################################'
         echo 'CONFIGURE USB-ETH FOR MOBILE'
         echo '################################'
         rename_net_device "${OLD_MOBILE_ETH}" "${NEW_MOBILE_ETH}"
      fi
   fi
}

configure_for_unicore(){
   echo '################################'
   echo 'CONFIGURE FOR UNICORE'
   echo '################################'

   #echo mv ${BASEDIR}/${SET_BASE_POS} ${RTKBASE_GIT}/
   mv ${BASEDIR}/${SET_BASE_POS} ${RTKBASE_GIT}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}/${SET_BASE_POS}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}/${SET_BASE_POS}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_GIT}/${SET_BASE_POS}
   chmod +x ${RTKBASE_GIT}/${SET_BASE_POS}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${NMEACONF} ${RTKBASE_GIT}/
   mv ${BASEDIR}/${NMEACONF} ${RTKBASE_GIT}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}/${NMEACONF}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_GIT}/${NMEACONF}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_GIT}/${NMEACONF}
   chmod +x ${RTKBASE_GIT}/${NMEACONF}
   ExitCodeCheck $?

   #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
   if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]; then
      #echo mv ${BASEDIR}/${RTCM3LED} ${RTKBASE_PATH}/
      mv ${BASEDIR}/${RTCM3LED} ${RTKBASE_PATH}/
      ExitCodeCheck $?
   fi
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_PATH}/${RTCM3LED}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_PATH}/${RTCM3LED}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_PATH}/${RTCM3LED}
   chmod +x ${RTKBASE_PATH}/${RTCM3LED}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${UNICORE_CONFIGURE} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${UNICORE_CONFIGURE} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE}
   chmod +x ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${TAILSCALE_GET_HREF} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${TAILSCALE_GET_HREF} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${TAILSCALE_GET_HREF}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${TAILSCALE_GET_HREF}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${TAILSCALE_GET_HREF}
   chmod +x ${RTKBASE_TOOLS}/${TAILSCALE_GET_HREF}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${SYSTEM_UPGRADE} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${SYSTEM_UPGRADE} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${SYSTEM_UPGRADE}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${SYSTEM_UPGRADE}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${SYSTEM_UPGRADE}
   chmod +x ${RTKBASE_TOOLS}/${SYSTEM_UPGRADE}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${EXEC_UPDATE} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${EXEC_UPDATE} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${EXEC_UPDATE}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${EXEC_UPDATE}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${EXEC_UPDATE}
   chmod +x ${RTKBASE_TOOLS}/${EXEC_UPDATE}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${CONF980} ${RTKBASE_RECV}/
   mv ${BASEDIR}/${CONF980} ${RTKBASE_RECV}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONF980}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONF980}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${CONF982} ${RTKBASE_RECV}/
   mv ${BASEDIR}/${CONF982} ${RTKBASE_RECV}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONF982}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONF982}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${CONFBYNAV} ${RTKBASE_RECV}/
   mv ${BASEDIR}/${CONFBYNAV} ${RTKBASE_RECV}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONFBYNAV}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONFBYNAV}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${CONFSEPTENTRIO} ${RTKBASE_RECV}/
   mv ${BASEDIR}/${CONFSEPTENTRIO} ${RTKBASE_RECV}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONFSEPTENTRIO}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${CONFSEPTENTRIO}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${TESTSEPTENTRIO} ${RTKBASE_RECV}/
   mv ${BASEDIR}/${TESTSEPTENTRIO} ${RTKBASE_RECV}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${TESTSEPTENTRIO}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_RECV}/${TESTSEPTENTRIO}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${SEPTENTRIO_NAT} ${RTKBASE_GIT}/
   mv ${BASEDIR}/${SEPTENTRIO_NAT} ${RTKBASE_GIT}/
   ExitCodeCheck $?

   #echo chmod +x ${RTKBASE_GIT}/${SEPTENTRIO_NAT}
   chmod +x ${RTKBASE_GIT}/${SEPTENTRIO_NAT}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${SEPTENTRIO_NAT_SERVICE} ${SERVICE_PATH}/
   mv ${BASEDIR}/${SEPTENTRIO_NAT_SERVICE} ${SERVICE_PATH}/
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${MODEM_WEB_PROXY_SERVICE} ${SERVICE_PATH}/
   mv ${BASEDIR}/${MODEM_WEB_PROXY_SERVICE} ${SERVICE_PATH}/
   ExitCodeCheck $?

   WEB_IMAGES=${RTKBASE_WEB}/static/images
   #echo mv ${BASEDIR}/${FAVICON} ${WEB_IMAGES}/
   mv ${BASEDIR}/${FAVICON} ${WEB_IMAGES}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${WEB_IMAGES}/${FAVICON}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${WEB_IMAGES}/${FAVICON}
   ExitCodeCheck $?

   UDEV_RULES=/etc/udev/rules.d
   #echo mv ${BASEDIR}/${ELT0x33_RULES} ${UDEV_RULES}/
   mv ${BASEDIR}/${ELT0x33_RULES} ${UDEV_RULES}/
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${SEPTENTRIO_LINK} ${LINK_RULES}/
   mv ${BASEDIR}/${SEPTENTRIO_LINK} ${LINK_RULES}/
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${MOBILE_LINK} ${LINK_RULES}/
   mv ${BASEDIR}/${MOBILE_LINK} ${LINK_RULES}/
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${SEPTENTRIO_MODEM} ${MODEM_RULES}/
   mv ${BASEDIR}/${SEPTENTRIO_MODEM} ${MODEM_RULES}/
   ExitCodeCheck $?

   #echo mkdir -p ${NETWORK_CONF}
   mkdir -p ${NETWORK_CONF}
   ExitCodeCheck $?
   #echo mv ${BASEDIR}/${AUTOCONNECT_CONF} ${NETWORK_CONF}/
   mv ${BASEDIR}/${AUTOCONNECT_CONF} ${NETWORK_CONF}/
   ExitCodeCheck $?

   if ! ischroot; then
      #echo nmcli general reload
      nmcli general reload
      ExitCodeCheck $?
   fi

   #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
   if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]; then
      #echo mv ${BASEDIR}/${START_ELT0x33} ${RTKBASE_PATH}/
      mv ${BASEDIR}/${START_ELT0x33} ${RTKBASE_PATH}/
      ExitCodeCheck $?
      #echo mv ${BASEDIR}/${PBC} ${RTKBASE_PATH}/
      mv ${BASEDIR}/${PBC} ${RTKBASE_PATH}/
      ExitCodeCheck $?
   fi

   #echo mv ${BASEDIR}/${ONOFF_ELT0x33} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${ONOFF_ELT0x33} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${ONOFF_ELT0x33}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${ONOFF_ELT0x33}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${ONOFF_ELT0x33}
   chmod +x ${RTKBASE_TOOLS}/${ONOFF_ELT0x33}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${REBOOT_SH} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${REBOOT_SH} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${REBOOT_SH}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${REBOOT_SH}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${REBOOT_SH}
   chmod +x ${RTKBASE_TOOLS}/${REBOOT_SH}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${RESET_RECEIVER} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${RESET_RECEIVER} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${RESET_RECEIVER}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${RESET_RECEIVER}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${RESET_RECEIVER}
   chmod +x ${RTKBASE_TOOLS}/${RESET_RECEIVER}
   ExitCodeCheck $?

   #echo mv ${BASEDIR}/${NTRIP_LED} ${RTKBASE_TOOLS}/
   mv ${BASEDIR}/${NTRIP_LED} ${RTKBASE_TOOLS}/
   ExitCodeCheck $?
   #echo chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${NTRIP_LED}
   chown ${RTKBASE_USER}:${RTKBASE_USER} ${RTKBASE_TOOLS}/${NTRIP_LED}
   ExitCodeCheck $?
   #echo chmod +x ${RTKBASE_TOOLS}/${NTRIP_LED}
   chmod +x ${RTKBASE_TOOLS}/${NTRIP_LED}
   ExitCodeCheck $?

   SERVER_PY=${RTKBASE_WEB}/server.py
   #echo SERVER_PY=${SERVER_PY}
   patch -f ${SERVER_PY} ${BASEDIR}/${SERVER_PATCH}
   ExitCodeCheck $?
   chmod 644 ${SERVER_PY}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${SERVER_PATCH}
   ExitCodeCheck $?

   GNSS_RPROXY_PY=${RTKBASE_WEB}/gnss_rproxy_server.py
   #echo GNSS_RPROXY_PY=${GNSS_RPROXY_PY}
   patch -f ${GNSS_RPROXY_PY} ${BASEDIR}/${GNSS_RPROXY_PATCH}
   ExitCodeCheck $?
   chmod 644 ${GNSS_RPROXY_PY}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${GNSS_RPROXY_PATCH}
   ExitCodeCheck $?

   RTKBASE_CONFIG_MANAGER_PY=${RTKBASE_WEB}/RTKBaseConfigManager.py
   #echo RTKBASE_CONFIG_MANAGER_PY=${RTKBASE_CONFIG_MANAGER_PY}
   patch -f ${RTKBASE_CONFIG_MANAGER_PY} ${BASEDIR}/${RTKBASE_CONFIG_MANAGER_PATCH}
   ExitCodeCheck $?
   chmod 644 ${RTKBASE_CONFIG_MANAGER_PY}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${RTKBASE_CONFIG_MANAGER_PATCH}
   ExitCodeCheck $?

   STATUS_JS=${RTKBASE_WEB}/static/status.js
   #echo STATUS_JS=${STATUS_JS}
   patch -f ${STATUS_JS} ${BASEDIR}/${STATUS_PATCH}
   ExitCodeCheck $?
   chmod 644 ${STATUS_JS}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${STATUS_PATCH}
   ExitCodeCheck $?

   SETTING_JS=${RTKBASE_WEB}/static/settings.js
   #echo SETTING_JS=${SETTING_JS}
   patch -f ${SETTING_JS} ${BASEDIR}/${SETTING_JS_PATCH}
   ExitCodeCheck $?
   chmod 644 ${SETTING_JS}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${SETTING_JS_PATCH}
   ExitCodeCheck $?

   SETTING_HTML=${RTKBASE_WEB}/templates/settings.html
   #echo SETTING_HTML=${SETTING_HTML}
   patch -f ${SETTING_HTML} ${BASEDIR}/${SETTING_HTML_PATCH}
   ExitCodeCheck $?
   chmod 644 ${SETTING_HTML}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${SETTING_HTML_PATCH}
   ExitCodeCheck $?

   patch -f ${SETTINGS_DEFAULT} ${BASEDIR}/${SETTINGS_CONF_PATCH}
   ExitCodeCheck $?
   chmod 644 ${SETTINGS_DEFAULT}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${SETTINGS_CONF_PATCH}
   ExitCodeCheck $

   BASE_HTML=${RTKBASE_WEB}/templates/base.html
   #echo BASE_HTML=${BASE_HTML}
   patch -f ${BASE_HTML} ${BASEDIR}/${BASE_PATCH}
   ExitCodeCheck $?
   chmod 644 ${BASE_HTML}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${BASE_PATCH}
   ExitCodeCheck $?

   RUNCAST_SH=${RTKBASE_GIT}/run_cast.sh
   #echo RUNCAST_SH=${RUNCAST_SH}
   patch -f ${RUNCAST_SH} ${BASEDIR}/${RUNCAST_PATCH}
   ExitCodeCheck $?
   chmod 755 ${RUNCAST_SH}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${RUNCAST_PATCH}
   ExitCodeCheck $?

   PPP_CONF=${RTKBASE_WEB}/rtklib_configs/rtkbase_ppp-static_default.conf
   #echo PPP_CONF=${PPP_CONF}
   patch -f ${PPP_CONF} ${BASEDIR}/${PPP_CONF_PATCH}
   ExitCodeCheck $?
   chmod 755 ${PPP_CONF}
   ExitCodeCheck $?
   rm -f ${BASEDIR}/${PPP_CONF_PATCH}
   ExitCodeCheck $?

   restart_rtkbase_if_started
}

configure_settings(){
   if [ -f ${SETTINGS_SAVE} ]; then
      echo '################################'
      echo 'RESTORE SETTINGS'
      echo '################################'
   else
      echo '################################'
      echo 'CONFIGURE SETTINGS'
      echo '################################'
   fi

   sed="sudo -u ${RTKBASE_USER} sed -i"
   #echo sed=${sed}

   #echo BASEDIR=${BASEDIR} RTKBASE_PATH=${RTKBASE_PATH}
   if [[ "${BASEDIR}" != "${RTKBASE_PATH}" ]]
   then
      #echo mv ${BASEDIR}/${UNICORE_SETTIGNS} ${RTKBASE_PATH}/
      mv ${BASEDIR}/${UNICORE_SETTIGNS} ${RTKBASE_PATH}/
      ExitCodeCheck $?
   fi
   #echo chmod +x ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
   chmod +x ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
   ExitCodeCheck $?

   if [ -f ${SETTINGS_SAVE} ]; then
      #echo sudo -u "${RTKBASE_USER}" mv ${SETTINGS_SAVE} ${SETTINGS_NOW}
      sudo -u "${RTKBASE_USER}" mv ${SETTINGS_SAVE} ${SETTINGS_NOW}
      ExitCodeCheck $?

      source <( grep -v '^#' "${SETTINGS_DEFAULT}" | grep 'version=' )
      #echo version=${version} VERSION=${VERSION}
      ${sed} s/^version=.*/version=${version}/ "${SETTINGS_NOW}"
      ExitCodeCheck $?

      #echo if ! grep -q "^mobile_modem_web_ip=" "${SETTINGS_NOW}"; then
      if ! grep -q "^mobile_modem_web_ip=" "${SETTINGS_NOW}"; then
         echo insert mobile_modem_web_ip and mobile_modem_web_proxy_port into ${SETTINGS_NOW}
         #echo ${sed} "/^gnss_rcv_web_proxy_port=/a #ip address of the integrated modem web service (ie on E3372-325)\nmobile_modem_web_ip=192.168.8.1\n#port number for the Flask proxy app used to display the modem web service\nmobile_modem_web_proxy_port=7070" "${SETTINGS_NOW}"
         ${sed} "/^gnss_rcv_web_proxy_port=/a #ip address of the integrated modem web service (ie on E3372-325)\nmobile_modem_web_ip=192.168.8.1\n#port number for the Flask proxy app used to display the modem web service\nmobile_modem_web_proxy_port=7070" "${SETTINGS_NOW}"
         ExitCodeCheck $?
      fi

      #echo ${RTKBASE_PATH}/${UNICORE_SETTIGNS} ${OLD_VERSION}
      ${RTKBASE_PATH}/${UNICORE_SETTIGNS} ${OLD_VERSION}
      ExitCodeCheck $?
   else
      #echo ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
      ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
      ExitCodeCheck $?
   fi
   #echo rm -f ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
   rm -f ${RTKBASE_PATH}/${UNICORE_SETTIGNS}
}

configure_gnss(){
   UP_TIME=`cat /proc/uptime | sed "s/\..*$//"`
   LIMIT_TIME=40
   LEFT_TIME=`expr ${LIMIT_TIME} - ${UP_TIME}`
   #echo UP_TIME=${UP_TIME} LIMIT_TIME=${LIMIT_TIME} LEFT_TIME=${LEFT_TIME}
   if [[ ${LEFT_TIME} -gt 0 ]]; then
      #echo sleep ${LEFT_TIME}
      sleep ${LEFT_TIME}
   fi

   rtkbase_path=${RTKBASE_GIT}
   #echo source "${rtkbase_path}/tools/opizero_temp_offset.sh"
   source "${rtkbase_path}/tools/opizero_temp_offset.sh"
   ExitCodeCheck $?

   HAVE_TTYUSB=`find /dev/ttyUSB* 2>/dev/null`
   HAVE_TTYACM=`find /dev/ttyACM* 2>/dev/null`
   HAVE_TTYAMA=`find /dev/ttyAMA* 2>/dev/null`
   #echo HAVE_TTYUSB=${HAVE_TTYUSB} HAVE_TTYACM=${HAVE_TTYACM} HAVE_TTYAMA=${HAVE_TTYAMA} RECVPORT=${RECVPORT}
   if [[ "${HAVE_TTYUSB}" == "" ]] && [[ "${HAVE_TTYACM}" == "" ]] && [[ "${HAVE_TTYAMA}" == "" ]] && [[ ! -c "${RECVPORT}" ]]; then
      echo 'No any ports for GNSS receiver. We can'\''t detect and configure.'
      ExitCodeCheck 1
   else
      if [[ "${UPDATE}" != "Y" ]] || ! have_full; then
         for i in `seq 1 3`; do
            #echo ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE} -u ${RTKBASE_USER} -e
            ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE} -u ${RTKBASE_USER} -e
            ExitCodeCheck $?
            if [[ $lastcode == 0 ]]; then
               break;
            fi
         done
      fi

      source <( grep '^com_port=' "${rtkbase_path}"/settings.conf ) #import settings
      if [[ "${com_port}" == "" ]]; then
         echo 'GNSS receiver is not specified. We can'\''t configure.'
         ExitCodeCheck 1
      else
         for i in `seq 1 3`; do
            #echo ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE} -u ${RTKBASE_USER} -c
            ${RTKBASE_TOOLS}/${UNICORE_CONFIGURE} -u ${RTKBASE_USER} -c
            ExitCodeCheck $?
            if [[ $lastcode == 0 ]]; then
               break;
            fi
         done
      fi
   fi

   restart_rtkbase_if_started
}

start_rtkbase_services(){
  #echo ${RTKBASE_TOOLS}/insall.sh -u ${RTKBASE_USER} -s
  ${RTKBASE_TOOLS}/install.sh -u ${RTKBASE_USER} -s
  ExitCodeCheck $?

  source <( grep '^receiver=' "${SETTINGS_NOW}" ) #import settings
  ExitCodeCheck $?
  #echo receiver=${receiver}

  DHCP_enabled=$(systemctl is-enabled "${DHCP_SERVICE}")
  [[ "${DHCP_enabled}" != "disabled" ]] && [[ "${DHCP_enabled}" != "masked" ]] && systemctl disable "${DHCP_SERVICE}"

  GNSS_WEB_PROXY=rtkbase_gnss_web_proxy.service
  if [[ "${receiver}" =~ Septentrio ]]; then
     #echo systemctl start "${DHCP_SERVICE}"
     systemctl start "${DHCP_SERVICE}"
     ExitCodeCheck $?

     #echo systemctl enable --now "${SEPTENTRIO_NAT_SERVICE}"
     systemctl enable --now "${SEPTENTRIO_NAT_SERVICE}"
     ExitCodeCheck $?

     #echo systemctl enable --now "${GNSS_WEB_PROXY}"
     systemctl enable --now "${GNSS_WEB_PROXY}"
     ExitCodeCheck $?
  fi

  MOBILE=/sys/class/net/mobile
  if [[ -L ${MOBILE} ]]; then
     #echo systemctl enable --now "${MODEM_WEB_PROXY_SERVICE}"
     systemctl enable --now "${MODEM_WEB_PROXY_SERVICE}"
     ExitCodeCheck $?
  fi

  #echo systemctl start "${CHECK_INTERNET_SERVICE}"
  systemctl start "${CHECK_INTERNET_SERVICE}"
  ExitCodeCheck $?
  HAVE_ELT0x33=`find -P /dev/serial/by-id -name "*ELT0x33*" 2>/dev/null`
  if [[ "${HAVE_ELT0x33}" != "" ]]; then
     #echo systemctl start "${CHECK_SATELITES_SERVICE}"
     systemctl start "${CHECK_SATELITES_SERVICE}"
     ExitCodeCheck $?
  fi
}

delete_garbage(){
   if [[ "${FILES_DELETE}" != "" ]]
   then
      echo '################################'
      echo 'DELETE GARBAGE'
      echo '################################'

      #echo rm -f ${FILES_DELETE}
      rm -f ${FILES_DELETE}
      #echo \[ $exitcode = 0 \] \&\& have_receiver \&\& echo rm -f ${BASENAME}
   fi
   [ $exitcode = 0 ] && have_receiver && rm -f ${BASENAME}
}

info_open(){
   NOW_HOST=`hostname`
   if [ $exitcode = 0 ]
   then
      echo You can open your browser to http://${NOW_HOST}.local into local network
   else
      echo exitcode = $exitcode Check bugs in this output
   fi
}

info_bug(){
   if [ $exitcode != 0 ]
   then
      echo exitcode = $exitcode Check bugs in this output
   fi
}

HAVE_RECEIVER=0
HAVE_PHASE1=0
HAVE_FULL=0
HAVE_RPI=0
CAN_REBOOT=0

have_receiver(){
   return ${HAVE_RECEIVER}
}
have_phase1(){
   return ${HAVE_PHASE1}
}
have_full(){
   return ${HAVE_FULL}
}
have_rpi(){
   return ${HAVE_RPI}
}
can_reboot(){
   return ${CAN_REBOOT}
}

BASE_EXTRACT="${NMEACONF} ${CONF980} ${CONF982} ${CONFBYNAV} ${UNICORE_CONFIGURE} \
              ${RUNCAST_PATCH} ${SET_BASE_POS} ${UNICORE_SETTIGNS} ${SETTINGS_CONF_PATCH} \
              ${RTKBASE_INSTALL} ${SYSCONGIG} ${SYSSERVICE} \
              ${SERVER_PATCH} ${STATUS_PATCH} ${TUNE_POWER} ${CONFIG} \
              ${RTKLIB}/* ${VERSION} ${SETTING_JS_PATCH} ${BASE_PATCH} \
              ${CONFSEPTENTRIO} ${TESTSEPTENTRIO} ${SETTING_HTML_PATCH} \
              ${PPP_CONF_PATCH} ${CONFIG_ORIG} ${TAILSCALE_GET_HREF} \
              ${SYSTEM_UPGRADE} ${EXEC_UPDATE} ${NETWORK_EVENT} \
              ${CHECK_INTERNET} ${CHECK_INTERNET_SERVICE} \
              ${SEPTENTRIO_NAT} ${SEPTENTRIO_NAT_SERVICE} \
              ${DHCP_CONF} ${DHCP_SERVICE} ${FAVICON} \
              ${ELT0x33_RULES} ${START_ELT0x33} ${ONOFF_ELT0x33} \
              ${STR2STR_RTCM_SVR_PATCH} ${STR2STR_TCP_PATCH} ${NTRIP_LED} \
              ${STR2STR_NTRIP_A_PATCH} ${RTCM3LED} ${CHECK_SATELITES} \
              ${CHECK_SATELITES_SERVICE} ${PBC} ${SEPTENTRIO_LINK} \
              ${SEPTENTRIO_MODEM} ${REBOOT_SH} ${RESET_RECEIVER} \
              ${AUTOCONNECT_CONF} ${MOBILE_LINK} ${GNSS_RPROXY_PATCH} \
              ${MODEM_WEB_PROXY_SERVICE} ${RTKBASE_CONFIG_MANAGER_PATCH}"
FILES_EXTRACT="${BASE_EXTRACT} uninstall.sh"
FILES_DELETE="${CONFIG} ${CONFIG_ORIG}"

check_phases(){
   if [[ ${1} == "-1" ]]
   then
      HAVE_RECEIVER=1
      HAVE_PHASE1=0
      HAVE_FULL=1
      FILES_EXTRACT="${BASE_EXTRACT}"
   elif [[ ${1} == "-2" ]]
   then
      HAVE_RECEIVER=0
      HAVE_PHASE1=1
      HAVE_FULL=1
      FILES_EXTRACT=
      FILES_DELETE=
   elif [[ ${1} == "-u" ]]
   then
      FILES_EXTRACT="${BASE_EXTRACT}"
   elif [[ ${1} == "-U" ]]
   then
      FILES_EXTRACT="${BASE_EXTRACT}"
      ONLINE_UPDATE=UPDATE
      CAN_REBOOT=1
   elif [[ ${1} == "-s" ]]
   then
      exit 0
   elif [[ ${1} == "-w" ]]
   then
      HAVE_RPI=1
   elif [[ ${1} != "" ]]
   then
      echo Invalid argument \"${1}\"
      exit 1
   fi

   #echo HAVE_RECEIVER=${HAVE_RECEIVER} HAVE_PHASE1=${HAVE_PHASE1} HAVE_FULL=${HAVE_FULL}
   #echo FILES_EXTRACT=${FILES_EXTRACT}
   #echo FILES_DELETE=${FILES_DELETE}
}

have_rpi && check_platform
restart_as_root ${1}
check_phases ${1}
have_phase1 && export LANG=C
unpack_files
have_phase1 && check_version
have_rpi && have_phase1 && check_boot_configiration
have_rpi && have_full && can_reboot && do_reboot
have_phase1 && install_tailscale
have_phase1 && install_additional_utilies
have_rpi && have_receiver && delete_pi_user
have_receiver && change_hostname ${HAVE_FULL}
stop_rtkbase_services
have_phase1 && add_rtkbase_user
have_phase1 && install_rtkbase_system_configure
have_phase1 && install_tune_power
#echo cd ${RTKBASE_PATH}
cd ${RTKBASE_PATH}
have_phase1 && install_rtklib
have_phase1 && copy_rtkbase_install_file
have_phase1 && rtkbase_install
! ischroot && configure_for_septentrio
! ischroot && configure_for_mobile
have_phase1 && configure_for_unicore
have_phase1 && configure_settings
have_receiver && configure_gnss
have_receiver && start_rtkbase_services
#echo cd ${BASEDIR}
cd ${BASEDIR}
delete_garbage
cd ${ORIGDIR}
have_full || info_reboot
have_receiver && info_open
have_receiver || info_bug
can_reboot || do_reboot END
#echo exit $exitcode
exit $exitcode

__ARCHIVE__
