#!/bin/bash

INSTALL=$1

DIR=`dirname $0`
OUTDIR="/opt/pos"
HAS_NETWORK=`/opt/bin/has_network`
STATION_NUMBER=`cat $OUTDIR/etc/config.ini | grep STATION_NUMBER | 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://www.iecr.com/api/software/releases 2> /dev/null`

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

    for rel in $arr; do
      if [ "$INSTALL" = "" ]; then
        echo "$count) $rel (NETWORK)"
      else
        if [ $count -eq $INSTALL ]; then
          wget -O /tmp/rel.tgz http://www.iecr.com/downloads/releases/$rel

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

          echo "Will install $rel from /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
        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
    if [ -d "/media/$pen/pos/releases" ]; then
      count=1

      for release in `ls -r "/media/$pen/pos/releases"`; do
        did_releases=1

        if [ "$INSTALL" = "" ]; then
          echo "$count) $release (PEN)"
        else
          if [ $count -eq $INSTALL ]; then
            echo "Will install $release from /media/$pen/pos/releases/$release in $OUTDIR"
            tar zxf "/media/$pen/pos/releases/$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 /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

            exit
          fi
        fi

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

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

