#!/bin/bash

# Update the dev site with the latest data from the live site
# which is backed up to /home/live/new-live-data/

LIVE_ROOT=/home/live/quince
LIVE_LATEST_MYSQL_DUMP=`ls -at $LIVE_ROOT/var/backups/mysql/* | head -1`
WORKING_DIR=/tmp/live2dev-$$
DEV_DOMAIN=dev.transitionnetwork.org
TEST_DOMAIN=test.transitionnetwork.org
DEV_WORKSPACES_DB=devworkspace
TEST_WORKSPACES_DB=test_workspaces

# 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
  
  # create a working directory
  if [[ -d $WORKING_DIR ]]; then
    echo "Oops $WORKING_DIR already exists"
    exit
  else 
    mkdir $WORKING_DIR 
  fi 
  
  # check if the site to update has been passed on the command line
  if [[ $1 ]]; then
    if [[ $1 = "dev" || $1 = "test" ]]; then
      SITE=$1
      echo "Updating the $1 site"
    else
      echo "You need to specify either dev or test as the first argument"
      exit
    fi
  fi
  
  # ask the user which site to update 
  if [[ -z $1 ]]; then
    printf "Which site would you like to update, dev or test? (dev|test)?"
    read SITE
    if [[ $SITE = "dev" || $SITE = "test" ]]; then
      echo "Updating the $SITE site"
    else
      echo "You need to type dev or test"
      exit
    fi
  fi
  
  # set the domain vairable
  if [[ $SITE = "dev" ]]; then
    DOMAIN=$DEV_DOMAIN
  fi
  if [[ $SITE = "test" ]]; then
    DOMAIN=$TEST_DOMAIN
  fi
  
  
  # get the latest backup for the live server
  if [[ -f $LIVE_LATEST_MYSQL_DUMP ]]; then
    echo "Unzipping $LIVE_LATEST_MYSQL_DUMP to $WORKING_DIR"
    # change to the working directory and unzip the live sql tar ball 
    cd $WORKING_DIR
    tar -zxvf $LIVE_LATEST_MYSQL_DUMP 
    # change to the dir with the sql files in 
    DB_DUMP=`echo $LIVE_LATEST_MYSQL_DUMP | sed -e 's/\/home\/live\/quince\/var\/backups\/mysql\///' | sed -e 's/_\.tar\.gz//'`
    cd $DB_DUMP
  else
    echo "There was a problem finding the live MySQL dump file"
    exit 1
  fi
  
  # update the databases, the root users passwd is in /root/.my.cnf
  echo " "
  echo "Now updating the $SITE database, this will take some time..."
  LIVE_LATEST_MYSQL_FILES=`ls *_live* | grep -v workspaces`

  #echo $LIVE_LATEST_MYSQL_FILES > sqlfiles.list
  #echo "check $WORKING_DIR/$DB_DUMP/sqlfiles.list" 

  cat $LIVE_LATEST_MYSQL_FILES | mysql $SITE

#exit 1

  # apply some fixes for running it as a dev server
  echo "UPDATE system SET status=0 WHERE name LIKE '%messag%' OR name LIKE '%notif%' OR name LIKE '%mailchimp%';" | mysql $SITE
  echo "UPDATE users SET mail=\"transition-$SITE@webarchitects.co.uk\";" | mysql $SITE
  echo "UPDATE users SET mail=\"edmitchell@transitionnetwork.org\" WHERE users.name=\"Ed Mitchell\";" | mysql $SITE
  echo "UPDATE users SET mail=\"jim@i-jk.co.uk\" WHERE users.name=\"Jim Kirkpatrick\";" | mysql $SITE
  echo "UPDATE users SET mail=\"john_mcgeechan@yahoo.co.uk\" WHERE users.name=\"John McGeechan\";" | mysql $SITE
  echo "UPDATE users SET mail=\"laura@popokatea.co.uk\" WHERE users.name=\"Laura Whitehead\";" | mysql $SITE
  echo "UPDATE users SET mail=\"chris@webarchitects.co.uk\" WHERE users.name=\"Chris Croome\";" | mysql $SITE
  echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh http://$DOMAIN` where name=\"securepages_basepath\";" | mysql $SITE
  echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh https://$DOMAIN` where name=\"securepages_basepath_ssl\";" | mysql $SITE
  echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh /tmp` where name=\"file_directory_temp\";" | mysql $SITE
  echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh sites/default/files` where name=\"file_directory_path\";" | mysql $SITE

  # update workspaces database
  LIVE_WORKSPACES_LATEST_MYSQL_FILES=`ls *_live_workspaces*`

  if [[ $SITE = "dev" ]]; then
    cat $LIVE_WORKSPACES_LATEST_MYSQL_FILES | mysql $DEV_WORKSPACES_DB 
    echo "UPDATE system SET status=0 WHERE name LIKE '%messag%' OR name LIKE '%notif%' OR name LIKE '%mailchimp%';" | mysql $DEV_WORKSPACES_DB 
    echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh /tmp` where name=\"file_directory_temp\";" | mysql $DEV_WORKSPACES_DB 
    echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh sites/workspaces.$DOMAIN/files` where name=\"file_directory_path\";" | mysql $DEV_WORKSPACES_DB 
  fi
  if [[ $SITE = "test" ]]; then
    cat $LIVE_WORKSPACES_LATEST_MYSQL_FILES | mysql $TEST_WORKSPACES_DB 
    echo "UPDATE system SET status=0 WHERE name LIKE '%messag%' OR name LIKE '%notif%' OR name LIKE '%mailchimp%';" | mysql $TEST_WORKSPACES_DB
    echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh /tmp` where name=\"file_directory_temp\";" | mysql $TEST_WORKSPACES_DB
    echo "UPDATE variable SET value=`/usr/local/bin/serialize.sh sites/workspaces.$DOMAIN/files` where name=\"file_directory_path\";" | mysql $TEST_WORKSPACES_DB
  fi
  
  # copy the files across
  echo "Updating the uploaded files"
  if [[ -d $LIVE_ROOT/web/transitionnetwork.org/www/sites/default/files/ ]]; then
    rsync -qv --exclude=cache --exclude=filecache --exclude=imagecache \
              $LIVE_ROOT/web/transitionnetwork.org/www/sites/default/files/ \
              /web/$SITE.transitionnetwork.org.webarch.net/www/sites/default/files/
    chown -R www-data:www-data /web/$SITE.transitionnetwork.org.webarch.net/www/sites/default/files/
  else
    echo "The source directory doesn't exist, $LIVE_ROOT/web/transitionnetwork.org/www/sites/default/files/"
  fi
  
  if [[ -d $LIVE_ROOT/web/transitionnetwork.org/www/sites/workspaces.transitionnetwork.org/files/ ]]; then
    rsync -qv --exclude=cache --exclude=filecache --exclude=imagecache \
              $LIVE_ROOT/web/transitionnetwork.org/www/sites/workspaces.transitionnetwork.org/files/ \
              /web/$SITE.transitionnetwork.org.webarch.net/www/sites/workspaces.$SITE.transitionnetwork.org.webarch.net/files/
    chown -R www-data:www-data /web/$SITE.transitionnetwork.org.webarch.net/www/sites/workspaces.$SITE.transitionnetwork.org.webarch.net/files/ 
  else
    echo "The source directory doesn't exist, $LIVE_ROOT/web/transitionnetwork.org/www/sites/workspaces.transitionnetwork.org/files/"
  fi
  
  echo "Cleaning up $WORKING_DIR directory"
  rm -rf $WORKING_DIR

else
  exit 1
fi

