#!/bin/bash

# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
if [[ $HOME != "/root" ]]; then
   echo "This script must be run as root user not via sudo"  1>&2
   exit 1
fi

printf "Have you run /usr/local/bin/backup2kiwi on quince.webarch.net? (y): "
read REMOTE_SCRIPT_RUN
if [[ "$REMOTE_SCRIPT_RUN" = "y" ]]; then

  # copy files across
  rsync -av /home/live/quince/web/wiki.transitionnetwork.org/www/images/ /web/wiki.dev.transitionnetwork.org/www/images/

  # update database
  cd /home/live/quince/var/backups/mysql/
  LATEST_MYSQL_DUMP=`ls -t1 *.gz | head -n1`
  tar -zxvf $LATEST_MYSQL_DUMP
  LATEST_MYSQL_DUMP_DIR=`echo $LATEST_MYSQL_DUMP | sed -e s/_.tar.gz//`
  cd $LATEST_MYSQL_DUMP_DIR
  cat *transwiki* | mysql transwiki  

else
  exit 1
fi



