#!/bin/bash
# Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

SCRIPT_DIR=$(dirname $(readlink -f $0))
cd ${SCRIPT_DIR}/..

LOGDIR=logs/stable-version
OPTIONS=()

# STATUS is part hack, part safety valve.  The cron job that invokes
# this script may be installed and enabled on both a primary and backup
# server.  We skip running on the backup, but really, the backup
# shouldn't have invoked this script in the first place...
#
# If we're not invoked on an autotest server at all, we assume this is
# for testing, and invoke a dry run instead.
#
SERVER_STATUS=$(cli/atest server list $(hostname) 2>&1 |
                  awk '/^Status *:/ { print $NF }')
if [ "${SERVER_STATUS}" = "primary" ]; then
  mkdir -p ${LOGDIR}
  NOTIFY=(
    chromeos-infra-eng@grotations.appspotmail.com
  )
else
  if [ -n "${SERVER_STATUS}" ]; then
    # must be backup
    exit 0
  fi
  OPTIONS=( --dry-run )
  NOTIFY=( ${LOGNAME}@google.com )
fi

# Redirect onto a log file.  For debug purposes, skip redirection if
# there's a command line argument (we ignore what the argument is), or
# if there's no log directory.
#
TAG=$(date '+%Y-%W')
if [ $# -eq 0 -a -d ${LOGDIR} ]; then
    LOGFILE="update-${TAG}.log"
    exec >>${LOGDIR}/${LOGFILE} 2>&1
fi

trap 'rm -f ${TMPFILE}' EXIT
TMPFILE=$(mktemp)

date
site_utils/assign_stable_images.py "${OPTIONS[@]}" | tee ${TMPFILE}
echo

# If we have a log directory, clean it up, and send e-mail notification.
# The log files change name each week, so by throwing out all but the
# most recent 14 files, we keep about 3 months of history, plus this
# week's log.
#
if [ -d ${LOGDIR} ]; then
    SUBJECT="Stable version update summary ${TAG}"
    site_utils/gmail_lib.py -s "${SUBJECT}" "${NOTIFY[@]}" <${TMPFILE}
    rm -f $(ls -r ${LOGDIR}/update-*.log | sed '1,14 d')
fi