#!/bin/bash

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

  # 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

  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

