#!/bin/sh

# steps to take following a release of new code to keep things working.
#
# the following scripts may be created to customize behavior:
# 
# site_utils/site_sync_code
#
# - pull code from a source repository
#
# site_utils/site_install_cli
#
# - install or update client code (new "atest" build?)
#
# site_utils/site_restart_apache
#
# - suid helper or similar?
# 
# site_utils/site_restart_final
# 
# - any finishing touches you may require.

# --- 

INIT_SCR=/etc/init.d/autotest

# ---

print_status() {
  STATUS=$1

  echo "--- $STATUS"
}

fatal() {
  echo "*** Fatal error.  Giving up."
  exit 1
}

# ---

MY_DIR=`dirname $0`

if (! test -f $INIT_SCR)
then
  echo "Error: $INIT_SCR must be installed."
  exit 1
fi

BECOME_USER=`grep ^BECOME_USER= $INIT_SCR`

if (test "$BECOME_USER" == "")
then
  echo "Error: BECOME_USER not defined in $INIT_SCR"
  exit 1
fi

BASE_DIR=`grep ^BASE_DIR= $INIT_SCR`

if (test "$BASE_DIR" == "")
then
  echo "Error: BASE_DIR not defined in $INIT_SCR"
  exit 1
fi

eval $BECOME_USER
eval $BASE_DIR

# --- stop autotest persistent code

print_status "Stopping autotest persistent code"
$INIT_SCR stop

# --- sync code (site-specific)

if (test -x $BASE_DIR/site_utils/site_sync_code)
then
  print_status "Syncing code"
  su $BECOME_USER -c $BASE_DIR/site_utils/site_sync_code || exit 1
fi

# --- run database migrations

# - AFE

print_status "Running AFE migrations"
( cd $BASE_DIR/frontend &&
  su $BECOME_USER -c "python ../database/migrate.py \
    --database=AUTOTEST_WEB safesync"
  su $BECOME_USER -c "python manage.py syncdb --noinput"
  su $BECOME_USER -c "python manage.py syncdb --noinput"
)

# - TKO

print_status "Running TKO migrations"
( cd $BASE_DIR/tko &&
  su $BECOME_USER -c "python ../database/migrate.py \
    --database=TKO safesync"
)

# - SITE_DB

print_status "Running site_db migrations"
( cd $BASE_DIR/site_db &&
  su $BECOME_USER -c "python ../database/migrate.py \
    --database=TKO safesync"
)

# - Django syncdb

print_status "Running syncdb on Django interface"
# Due to the way Django creates permissions objects, we sometimes need
# to run syncdb twice.
for i in 1 2; do
  ( cd $BASE_DIR/frontend && 
    su $BECOME_USER -c "python manage.py syncdb --noinput"
  )
done

# --- compile GWT

print_status "Compiling GWT code."
( cd $BASE_DIR &&
  su $BECOME_USER -c "$BASE_DIR/utils/compile_gwt_clients.py -a" || fatal
)

# --- fix gwt permissions

print_status "Fixing permissions"
( cd $BASE_DIR/frontend/client &&
  find | xargs chmod o+r &&
  find -type d | xargs chmod o+rx ) || fatal

# --- update cli repository (site-specific)

if (test -x $BASE_DIR/site_utils/site_install_cli)
then
  print_status "Updating cli repository"
  su $BECOME_USER -c $BASE_DIR/site_utils/site_install_cli || fatal
fi

# --- restart autotest persistent code

print_status "Restarting autotest persistent code"
$INIT_SCR start || fatal

# --- possibly restart Apache (site-specific)

if (test -x $BASE_DIR/site_utils/site_restart_apache)
then
  print_status "Restarting Apache"
  su $BECOME_USER -c $BASE_DIR/site_utils/site_restart_apache || fatal
fi

# --- do any site-specific finalization

if (test -x $BASE_DIR/site_utils/site_restart_final)
then
  print_status "Finalizing release"
  su $BECOME_USER -c $BASE_DIR/site_utils/site_restart_final || fatal
fi