#!/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

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

  if [ "$STATION_NUMBER" = "0" ]; then
    for station in `ls /opt/pos/server/data/registered_stations`; do
      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"

        umount /tmp/release 2> /dev/null
        mount -t cifs -o guest,timeout=1,rw,noperm,noserverino //$STATION_IP/pos /tmp/release

        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

  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"

  echo "Will install $release in $OUTDIR"
  tar zxf $release --directory $OUTDIR

  if [ "$STATION_NUMBER" = "0" ]; then
    for station in `ls /opt/pos/server/data/registered_stations`; do
      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"

        umount /tmp/release 2> /dev/null
        mount -t cifs -o guest,timeout=1,rw,noperm,noserverino //$STATION_IP/pos /tmp/release

        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
} 

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=`

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/*-$SOFTWARE_LANG-*.tgz 2> /dev/null`; do
      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))
    done

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

      for release in `ls -r /media/$pen/pos/releases/*-$SOFTWARE_LANG-*.tgz 2> /dev/null`; do
        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))
      done
    fi
  done

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

