#!/bin/bash

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

FILES_TO_KEEP=5

IDX_TO_RESTORE=$1
BACKUP_FOLDER=""
TMP=""

if [ -e "/opt/pos/.hardlock" ]; then
  TMP=`cat /opt/pos/.hardlock`
fi

if [ "$TMP" = "" ]; then
  echo "No /opt/pos/.hardlock found... maybe POS isn't running with an hardlock..."
  exit
fi

FF=`dirname $TMP`
BACKUP_FOLDER="$FF/data/backups"
MACHINE_ID2=`/opt/pos/common/bin/machine_id 2`

if [ -e "/opt/pos/hardlock2" ] && [ "$MACHINE_ID2" != "" ]; then
  found=0

  for pen in `mount | grep "/media" | awk '{ print $3 }'`; do
    echo 1 > $pen/.check.$$

    if [ $? -eq 0 ]; then
      rm -f $pen/.check.$$

      if [ -d "$pen/backups" ]; then
        found=1
        BACKUP_FOLDER="$pen/backups"
        break
      fi
    fi
  done

  if [ $found -eq 0 ]; then
    BACKUP_FOLDER="/opt/backups"
  fi
fi

if [ "$BACKUP_FOLDER" = "" ]; then
  echo "Can't find backup folder to put backups in... check why /opt/pos/.hardlock isn't created..."
  exit
fi

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

PEN_PATH="$FF/data/backups"

if [ "$TMP" = "/opt/pos/hardlock" ]; then
  PEN_PATH="/opt/backups"
fi

if [ -e "/opt/pos/hardlock2" ] && [ "$MACHINE_ID2" != "" ]; then
  PEN_PATH="$BACKUP_FOLDER"
fi

has_cloud_backups=`cat /opt/pos/etc/config.ini | grep "INTERNET_BACKUP" | cut -f2 -d=`

#
# Cloud backups
#

if [ "$has_cloud_backups" = "1" ]; then
  has_network=`timeout 5s /opt/pos/common/bin/has_network`

  if [ "$has_network" = "YES" ]; then
    hardlock=`/opt/pos/common/bin/machine_id`

    if [ "$IDX_TO_RESTORE" = "" ]; then
      echo "Usage: $0 <idx_to_restore>"
      echo ""
      echo "Available Indexes:"
      echo ""

      files=`curl --silent --connect-timeout 5 --max-time 5 -F "list_files=1" -F "hardlock=$hardlock" http://www.ptcert.com/hardlock_backup`

      COUNT=1
      export IFS=","

      for file in $files; do
        touch /tmp/has_backups.txt
        echo "$COUNT - /from/cloud/$file"
        COUNT=$(($COUNT+1))
      done

      echo ""
      echo ""

      if [ ! -f "/tmp/has_backups.txt" ]; then
        echo "NO BACKUPS"
      else
        rm -rf /tmp/has_backups.txt
      fi

    else
      RESTORE_IDX=$1

      files=`curl --silent --connect-timeout 5 --max-time 5 -F "list_files=1" -F "hardlock=$hardlock" http://www.ptcert.com/hardlock_backup`

      COUNT=1
      export IFS=","

      for file in $files; do
        if [ "$COUNT" = "$RESTORE_IDX" ]; then
          curl "http://www.ptcert.com/hardlock_backup?hardlock=$hardlock&download=1&download_file=$file" --output /tmp/$$.backup.zip

          if [ $? -eq 0 ]; then
            echo "$file" > /tmp/done.txt

            mkdir /tmp/$$.backup
            unzip /tmp/$$.backup.zip -d /tmp/$$.backup

            cmd=""

            if [[ $file == *"backupv2_"* ]]; then
              cmd="/opt/pos/common/bin/restore"
            else
              if [ -e "/opt/pos/common/bin/restore.v1" ]; then
                cmd="/opt/pos/common/bin/restore.v1"
              else
                cmd="/opt/pos/common/bin/restore"
              fi
            fi

            $cmd /tmp/$$.backup/$file 1

            rm -f /tmp/$$.backup.zip
            rm -rf /tmp/$$.backup

            echo "OK ($file)"
            touch /tmp/restart.txt
          else
            echo "Error downloading $file... Aborting..."
            exit
          fi

        fi

        COUNT=$(($COUNT+1))
      done

    fi

    exit

  fi
fi

#
# Local backups
#

if [ "$IDX_TO_RESTORE" = "" ]; then
  echo "Usage: $0 <idx_to_restore>"
  echo ""
  echo "Available Indexes:"
  echo ""

  COUNT=1

  if [ -d "$PEN_PATH" ]; then
    find "$PEN_PATH" -type f | grep "\.zip" | sort -n -r | while read -r file
    do
      touch /tmp/has_backups.txt
      echo "$COUNT - $file"
      COUNT=$(($COUNT+1))
    done

    if [ ! -f "/tmp/has_backups.txt" ]; then
      echo "NO BACKUPS"
    else
      rm -rf /tmp/has_backups.txt
    fi

    echo ""
    echo ""
  else
    echo "NO BACKUPS"
  fi
else
  RESTORE_IDX=$1

  COUNT=1

  find "$PEN_PATH" -type f | grep "\.zip" | sort -n -r | while read -r file
  do
    if [ "$COUNT" = "$RESTORE_IDX" ]; then
      cmd=""

      if [[ $file == *"backupv2_"* ]]; then
        cmd="/opt/pos/common/bin/restore"
      else
        if [ -e "/opt/pos/common/bin/restore.v1" ]; then
          cmd="/opt/pos/common/bin/restore.v1"
        else
          cmd="/opt/pos/common/bin/restore"
        fi
      fi

      "$cmd" "$file" 1

      echo "$file" > /tmp/done.txt
    fi

    COUNT=$(($COUNT+1))
  done

  if [ ! -f "/tmp/done.txt" ]; then
    echo "Can't find backup with index $RESTORE_IDX..."
  else
    RESTORED_FILE=`cat /tmp/done.txt`
    rm -rf /tmp/done.txt
    echo "OK ($RESTORED_FILE)"
    touch /tmp/restart.txt
  fi
fi

