#!/bin/bash

# Usage:
#     servo-stat DUT ...
#
# Reports the status of the servo (if any) attached to the DUT.
# The DUT name is the host name without the .cros, or -servo.
# For each named DUT, reports a line something like this:
#     DUT ...ABCDEFG is up BOARD=swanky CHROMEOS_RELEASE_VERSION=5995.0.0
#
# The letters are just arbitrary tags printed before any
# long-running operation that might time out.  It allows you to see
# progress, and if things get hung up, you can see where.


# readlink -f $0, in case $0 is a symlink from somewhere else
REPO=$(dirname $(readlink -f $0))/../../../../..
REPO=$(readlink -f $REPO)
PYTHON=$(readlink -f $REPO/chroot/usr/bin/python2.7)
HDCTOOLS=$(readlink -f $REPO/chroot/usr/lib/python2.7/site-packages/servo)
KEYFILE=$REPO
KEYFILE=$KEYFILE/src/third_party/chromiumos-overlay
KEYFILE=$KEYFILE/chromeos-base/chromeos-ssh-testkeys/files/testing_rsa

# Need some temporary files to keep ssh happy:
#  + Just setting StrictHostKeyChecking=no won't silence all
#    possible errors about host keys, so we need a temporary file
#    where host keys can be cached.
#  + We don't want to require the user to edit or provide the
#    standard test keys, so we use the keys from the repo.  But...
#    The file must be user-readable only, so we need a copy with
#    the correct modes (mktemp is 600 by default).

TMPKEYS=$(mktemp)
TMPHOSTS=$(mktemp)

trap 'rm $TMPKEYS $TMPHOSTS' EXIT
cp $KEYFILE $TMPKEYS

_ssh() {
  local ssh_opts=( -n -o BatchMode=yes -o StrictHostKeyChecking=no
                   -o UserKnownHostsFile=$TMPHOSTS -i $TMPKEYS )
  local timeout=$1
  local servo=$2
  shift 2
  timeout "${timeout}" ssh "${ssh_opts[@]}" "root@${servo}" "$@"
}

dut_control() {
  local servo=$1
  shift
  _ssh 90 $servo dut-control "$@"
}

remote() {
  _ssh 45 "$@"
}

get_afe_host_attr() {
  local host=$1
  local attr=$2
  local default=$3
  atest host stat $host | awk "/^$attr *: / {count++; print \$3}
    END {if (count != 1) print \"$default\"}"
}

get_servo() {
  local host=$1

  # Get the servo host from the afe. If not present, infer it from the hostname.
  local servo_host=$(get_afe_host_attr $host servo_host ${host}-servo)
  echo ${servo_host}.cros
}

get_servo_port() {
  local host=$1

  # Get the servo port from the afe. If not present, default 9999.
  get_afe_host_attr $host servo_port 9999
}



for H in "$@"
do
  SERVO=$(get_servo $H)
  SERVO_PORT=$(get_servo_port $H)
  CONFIG=/var/lib/servod/config_$SERVO_PORT
  echo -n "$H ..."
  STATUS=()

  HAVE_SERVOD=1
  BOARD=
  VERSION=

  echo -n "A"
  if ping -c1 -w2 $SERVO >/dev/null 2>&1
  then
    echo -n "B"
    if BUTTON=$(dut_control $SERVO -p $SERVO_PORT pwr_button 2>/dev/null)
    then
      if [ "$BUTTON" != "pwr_button:release" ]
      then
        STATUS=("${STATUS[@]}" "pwr_button is '$BUTTON'")
      else
        echo -n "C"
        LID=$(dut_control $SERVO -p $SERVO_PORT lid_open 2>/dev/null)
        if [ "$LID" != "lid_open:yes" -a "$LID" != "lid_open:not_applicable" ]
        then
          STATUS=("${STATUS[@]}" "lid_open is '$LID'")
        fi
      fi
    else
      STATUS=("${STATUS[@]}" "not running servod")
      HAVE_SERVOD=0
    fi

    echo -n "D"
    if ! remote $SERVO true >/dev/null 2>&1
    then
      STATUS=("${STATUS[@]}" "ssh is down")
    else
      echo -n "E"
      VERSION=$(
          remote $SERVO grep CHROMEOS_RELEASE_VERSION /etc/lsb-release 2>&1)
      if [ -z "$VERSION" ]
      then
        STATUS=("${STATUS[@]}" "not running chromeos")
      fi
    fi

    if [ -n "$VERSION" ]
    then
      echo -n "F"
      if remote $SERVO test -f $CONFIG
      then
        echo -n "G"
        BOARD=$(remote $SERVO grep BOARD= $CONFIG)
      fi
      if [ $HAVE_SERVOD -eq 0 ]
      then
        if [ -z "$BOARD" ]
        then
          STATUS=("servod not configured")
        else
          echo -n "H"
          JOB=$(remote $SERVO status servod | sed 's/,.*//')
          if [ "$JOB" = "servod start/running" ]
          then
              STATUS=("servod failed")
          fi
        fi
      fi
    fi
  else
    STATUS=("${STATUS[@]}" "is down")
  fi

  if [ "${#STATUS}" -eq 0 ]
  then
    STATUS=("is up")
  fi

  if [ -n "$VERSION" ]
  then
    STATUS=("${STATUS[@]}" $BOARD $VERSION)
  fi
  echo " ${STATUS[@]}"
done