#!/bin/bash

TMP_DIR="/tmp/$$"
FILE_TO_RESTORE=$1

DB_NAME="pos"
CUR_DIR=`pwd`

if [ "$FILE_TO_RESTORE" = "" ]; then
  echo "Usage: $0 <file_to_restore>"
  exit
fi

disk_rw

BACKUP_DIR=`dirname $FILE_TO_RESTORE`

mkdir $TMP_DIR
cd $TMP_DIR
unzip -q -o "$FILE_TO_RESTORE"

if [ -d "$TMP_DIR/server/data/product_images" ]; then
  rm -rf "$TMP_DIR/server/data/product_images/*"
fi

if [ -d "$TMP_DIR/server/data/group_images" ]; then
  rm -rf "$TMP_DIR/server/data/group_images/*"
fi

if [ -d "$TMP_DIR/common/bin" ]; then
  chmod 755 -R $TMP_DIR/common/bin/*
fi

if [ -d "$TMP_DIR/server/apps" ]; then
  chmod 755 -R $TMP_DIR/server/apps/*
fi

if [ -d "$TMP_DIR/station/apps" ]; then
  chmod 755 -R $TMP_DIR/station/apps/*
fi

mv $TMP_DIR/common/bin/pos /tmp
cp -r $TMP_DIR/* /opt/pos
rm -rf $TMP_DIR

cd /opt/pos

if [ -e "/opt/pos/server/data/database/db.dump" ]; then
  /etc/init.d/postgresql stop

  if [ -e "/etc/postgresql/9.1/main/postgresql.conf.close" ]; then
    cp /etc/postgresql/9.1/main/postgresql.conf.close /etc/postgresql/9.1/main/postgresql.conf
  fi

  /etc/init.d/postgresql start

  dropdb -U root pos
  createdb -U root pos
  pg_restore -U root -Fc -d pos "/opt/pos/server/data/database/db.dump"

  # reindex most used tables
  reindexdb -U root pos -t sales_headers
  reindexdb -U root pos -t sales_payments
  reindexdb -U root pos -t sales_details
  reindexdb -U root pos -t sales_headers_aux
  reindexdb -U root pos -t sales_hashes
  reindexdb -U root pos -t counters

  if [ -e "/opt/pos/doc/casts.postgres" ]; then
    psql -U root pos < /opt/pos/doc/casts.postgres
  fi

  if [ -e "/opt/pos/doc/db_changes.postgres" ]; then
    psql -U root pos < /opt/pos/doc/db_changes.postgres
  fi

  rm -rf "/opt/pos/server/data/database/db.dump"

  if [ -e "/etc/postgresql/9.1/main/postgresql.conf.open" ]; then
    cp /etc/postgresql/9.1/main/postgresql.conf.open /etc/postgresql/9.1/main/postgresql.conf
    /etc/init.d/postgresql stop
    /etc/init.d/postgresql start
  fi

  rm /tmp/server_*.txt
fi

# restore archived files
if [ -d "$BACKUP_DIR/archive" ]; then
  for archive in `ls $BACKUP_DIR/archive`; do
    echo "Will restore from ARCHIVE: $archive"
    date_str=`echo $archive | cut -f1 -d"."`
    pg_restore --table=sales_headers_$date_str -U root -Fc -d $DB_NAME $BACKUP_DIR/archive/$archive
    pg_restore --table=sales_payments_$date_str -U root -Fc -d $DB_NAME $BACKUP_DIR/archive/$archive
    pg_restore --table=sales_hashes_$date_str -U root -Fc -d $DB_NAME $BACKUP_DIR/archive/$archive
    pg_restore --table=sales_hashes_products_$date_str -U root -Fc -d $DB_NAME $BACKUP_DIR/archive/$archive
    pg_restore --table=sales_details_$date_str -U root -Fc -d $DB_NAME $BACKUP_DIR/archive/$archive
  done
fi

if [ -e "/opt/pos/etc/config.xml" ]; then
  cp "/opt/pos/etc/config.xml" /root/weckOFFICE/etc/config.xml
fi

if [ -e "/opt/pos/etc/machine/settings.txt" ]; then
  cp "/opt/pos/etc/machine/settings.txt" /etc/network-config/settings.txt
  /opt/bin/network.sh
fi

if [ -e "/tmp/pos" ]; then
  mv /tmp/pos /opt/pos/common/bin
fi

cd "$CUR_DIR"

