#!/bin/bash

if [[ "$EUID" -ne 0 ]]; then
  echo "Error: This script must be run as root." >&2
  exit 1
fi

function release_install_from_net {
  STATION_NUMBER=$1
  RELEASE_NUMBER=$2
  SOFTWARE_LANG=$3
  rel=$4

  OUTDIR="/opt/pos"

  if [ "$RELEASE_NUMBER" = "0" ]; then
    wget -O /tmp/rel.tgz http://releases.ptcert.com/downloads/$rel
  else
    if [ "$SOFTWARE_LANG" = "PT" ]; then
      wget -O /tmp/rel.tgz http://releases.ptcert.com/downloads/releases/$rel
    else
      wget -O /tmp/rel.tgz http://releases.ptcert.com/downloads/releases_skins/$SOFTWARE_LANG/$rel
    fi
  fi

  if [ "$?" != "0" ]; then
    echo "There was a problem downloading release, aborting..."
    exit
  fi

  # when we're on an arm distro (ipesa) fetch the gambas3 versions of these softwares
  IS_ARM=`uname -a | grep -E "arm|aarch64"`

  if [ "$IS_ARM" != "" ]; then
    wget -q -O /opt/pos/common/bin/pos.qt5 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/pos.qt5
    echo "#### download status: $? - pos.qt5"
    wget -q -O /opt/pos/common/addons/machine_config/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/machine_config.gb3
    echo "#### download status: $? - machine_config.gb3"
    wget -q -O /opt/pos/common/addons/functions_config/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/functions_config.gb3
    echo "#### download status: $? - functions_config.gb3"
    wget -q -O /opt/pos/common/addons/reports_config/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/reports_config.gb3
    echo "#### download status: $? - eports_config.gb3"
    wget -q -O /opt/pos/common/addons/saft-pt/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/saft-pt.gb3
    echo "#### download status: $? - saft-pt.gb3"
    wget -q -O /opt/pos/common/addons/graphical_display_v2/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/graphical_display.gb3
    echo "#### download status: $? - graphical_display.gb3"
    wget -q -O /opt/pos/common/addons/releases/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/releases.gb3
    echo "#### download status: $? - releases.gb3"
    wget -q -O /opt/pos/common/addons/edit_files/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/edit_files.gb3
    echo "#### download status: $? - edit_files.gb3"
    wget -q -O /opt/pos/common/addons/filetools/app.gb3 http://releases.ptcert.com/downloads/ptcert_ipesa/ipesa_ipx/filetools.gb3
    echo "#### download status: $? - filetools.gb3"
    chmod 755 /opt/pos/common/bin/pos.qt5
    chmod 755 /opt/pos/common/addons/machine_config/app.gb3
    chmod 755 /opt/pos/common/addons/functions_config/app.gb3
    chmod 755 /opt/pos/common/addons/reports_config/app.gb3
    chmod 755 /opt/pos/common/addons/saft-pt/app.gb3
    chmod 755 /opt/pos/common/addons/graphical_display_v2/app.gb3
    chmod 755 /opt/pos/common/addons/releases/app.gb3
    chmod 755 /opt/pos/common/addons/edit_files/app.gb3
    chmod 755 /opt/pos/common/addons/filetools/app.gb3
  fi

  # Generate releases update log
  v=`cat /opt/pos/version`
  d=`date`
  echo $d" - "$rel" - "$v >> /opt/pos/doc/versions.log

  # update stations first
  if [ "$STATION_NUMBER" = "0" ]; then
    for station in `ls /opt/pos/server/data/registered_stations`; do
      if [ $station -ge 80 ]; then
        continue
      fi

      STATION_IP=`cat /opt/pos/server/data/registered_stations/$station | head -4 | sed '4!d'`

      if [ "$STATION_IP" != "" ]; then
        echo "WILL UPDATE STATION - $station - IP: $STATION_IP"

        if [ ! -d "/tmp/release" ]; then
          mkdir /tmp/release
        fi

        umount /tmp/release 2> /dev/null

        #if [ -e "/usr/bin/sshfs" ]; then
        #  echo pos | sshfs root@$STATION_IP:/opt/pos /tmp/release -o reconnect,password_stdin,dev,suid,nonempty,cache=no,ServerAliveInterval=10,ServerAliveCountMax=3
        #else
          timeout 2 mount -t cifs -o guest,rw,noperm,noserverino //$STATION_IP/pos /tmp/release
        #fi

        if [ "$?" = "0" ]; then
          tar zxf /tmp/rel.tgz --directory /tmp/release
          echo "STATION $station ($STATION_IP) - UPDATED!!"
        else
          echo "STATION $station ($STATION_IP) - ERROR MOUNTING - $?!!"
        fi

        umount /tmp/release 2> /dev/null
      else
        echo "STATION $station - ERROR FINDING IP - CHECK /opt/pos/server/data/registered_stations/$station"
      fi

    done
  fi

  # update server after
  echo "Will install $rel into /tmp/rel.tgz in $OUTDIR"
  tar zxf /tmp/rel.tgz --directory $OUTDIR

  rm -rf /tmp/rel.tgz

  # when we're on an arm distro (ipesa) fetch the gambas3 versions of these softwares
  if [ "$IS_ARM" != "" ]; then
    sudo /opt/bin/kill_pos.sh
  fi

  exit
}

function release_install_from_pen {
  release=$1
  STATION_NUMBER=$2
  RELEASE_NUMBER=$3
  RELEASES_PATH=$4

  OUTDIR="/opt/pos"

  # update stations first
  if [ "$STATION_NUMBER" = "0" ]; then
    for station in `ls /opt/pos/server/data/registered_stations`; do
      if [ $station -ge 80 ]; then
        continue
      fi

      STATION_IP=`cat /opt/pos/server/data/registered_stations/$station | head -4 | sed '4!d'`

      if [ "$STATION_IP" != "" ]; then
        echo "WILL UPDATE STATION - $station - IP: $STATION_IP"

        if [ ! -d "/tmp/release" ]; then
          mkdir /tmp/release
        fi

        umount /tmp/release 2> /dev/null

        #if [ -e "/usr/bin/sshfs" ]; then
        #  echo pos | sshfs root@$STATION_IP:/opt/pos /tmp/release -o reconnect,password_stdin,dev,suid,nonempty,cache=no,ServerAliveInterval=10,ServerAliveCountMax=3
        #else
          timeout 2 mount -t cifs -o guest,rw,noperm,noserverino //$STATION_IP/pos /tmp/release
        #fi

        if [ "$?" = "0" ]; then
          tar zxf $release --directory /tmp/release
          echo "STATION $station ($STATION_IP) - UPDATED!!"
        else
          echo "STATION $station ($STATION_IP) - ERROR MOUNTING - $?!!"
        fi

        umount /tmp/release 2> /dev/null
      else
        echo "STATION $station - ERROR FINDING IP - CHECK /opt/pos/server/data/registered_stations/$station"
      fi
    done
  fi

  # update server after
  echo "Will install $release in $OUTDIR"
  tar zxf $release --directory $OUTDIR

} 

INSTALL=$1

DIR=`dirname $0`
OUTDIR="/opt/pos"
HAS_NETWORK=`/opt/pos/common/bin/has_network`
STATION_NUMBER=`cat $OUTDIR/etc/config.ini | grep STATION_NUMBER | cut -f2 -d=`
SOFTWARE_LANG=`cat $OUTDIR/etc/config.ini | grep LANG= | cut -f2 -d=`
SSH_HAS_StrictHostKeyChecking=`cat /etc/ssh/ssh_config | grep "StrictHostKeyChecking no"`

if [ "$SSH_HAS_StrictHostKeyChecking" = "" ]; then
  echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config
fi

mkdir /tmp/release 2> /dev/null

if [ ! -d "$OUTDIR" ]; then
  mkdir -p "$OUTDIR"
fi

if [ "$HAS_NETWORK" = "YES" ]; then
  releases=`curl http://releases.ptcert.com/api/software/releases?lang=$SOFTWARE_LANG 2> /dev/null`

  if [ "$releases" != "" ]; then
    arr=$(echo $releases | tr "," "\n")
    count=1

    if [ "$INSTALL" = "0" ]; then
      release_install_from_net "$STATION_NUMBER" "$INSTALL" $SOFTWARE_LANG "devel-PT.tgz"
    fi

    for rel in $arr; do
      if [ "$INSTALL" = "" ]; then
        echo "$count) $rel (NETWORK)"
      else
        if [ $count -eq $INSTALL ]; then
          release_install_from_net "$STATION_NUMBER" "$INSTALL" $SOFTWARE_LANG "$rel"
        fi
      fi

      count=$(($count+1))
    done
  else
    echo "No releases found on the internet..."
  fi
else
  did_releases=0

  for pen in `ls /media`; do
    count=1

    for release in `ls -r /media/$pen/*.tgz 2> /dev/null`; do
      tmp_lang=`echo $release | cut -f3 -d-`

      if [ "$tmp_lang" = "$SOFTWARE_LANG" ]; then
        did_releases=1

        if [ "$INSTALL" = "" ]; then
          echo "$count) $release (PEN/root)"
        else
          if [ $count -eq $INSTALL ]; then
            release_install_from_pen "$release" "$STATION_NUMBER" "$INSTALL" "/media/$pen"
            exit
          fi
        fi

        count=$(($count+1))
      fi
    done

    if [ -d "/media/$pen/pos/releases" ]; then

      for release in `ls -r /media/$pen/pos/releases/*.tgz 2> /dev/null`; do
        tmp_lang=`echo $release | cut -f3 -d-`

        if [ "$tmp_lang" = "$SOFTWARE_LANG" ]; then
          did_releases=1

          if [ "$INSTALL" = "" ]; then
            echo "$count) $release (PEN/releases)"
          else
            if [ $count -eq $INSTALL ]; then
              release_install_from_pen "$release" "$STATION_NUMBER" "$INSTALL" "/media/$pen/pos/releases"
              exit
            fi
          fi

          count=$(($count+1))
        fi
      done
    fi
  done

  if [ $did_releases -eq 0 ]; then
    echo "Could't find any release..."
  fi
fi

