#!/bin/bash

# Copyright 2013-present Facebook
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# default configs
CONFLINK='/etc/chef/client.rb'
PRODCONF='/etc/chef/client-prod.rb'
CERTLINK='/etc/chef/client.pem'
PRODCERT='/etc/chef/client-prod.pem'
STAMPFILE='/etc/chef/test_timestamp'

# let the config file overwrite them
CONFIG_FILE="${CONFIG_FILE:-/etc/taste-untester-config}"
if [ -e "$CONFIG_FILE" ]; then
  source "$CONFIG_FILE"
fi

MYSELF=$0
DRYRUN=0
DEBUG=0

debug() {
  [ "$DEBUG" -eq 1 ] && echo $*
}

set_server_to_prod() {
  ME=$(basename $MYSELF)
  if [ -s $STAMPFILE ]; then
    kill -- -$(cat $STAMPFILE)
  fi
  rm -f $CONFLINK
  ln -s $PRODCONF $CONFLINK
  # Legacy FB stuff, will go away
  if [ -h $CERTLINK ]; then
    rm $CERTLINK
    ln -s $PRODCERT $CERTLINK
  fi
  rm -f $STAMPFILE
  logger -p user.warning -t $ME Reverted to production Chef.
  if [ -e '/usr/bin/wall' ]; then
    echo "Reverted $(hostname) to production Chef." | wall
  fi
}

check_server() {
  if [ ! -h $CONFLINK ]; then
    return
  fi
  current_config=$(readlink $CONFLINK)
  if [ "$current_config" = $PRODCONF ]; then
    if [ -f "$STAMPFILE" ]; then
      rm -f $STAMPFILE
    fi
    return
  fi

  now=$(date +%s)
  if [ "$(uname)" = 'Darwin' ]; then
    stamp_time=$(stat -f "%m" -t "%z" $STAMPFILE)
    stamp=$(date -r $stamp_time)
  else
    stamp=$(stat -c %y $STAMPFILE)
    stamp_time=$(date +%s -d "$stamp")
  fi

  debug "$now vs $stamp_time"
  if [ "$now" -gt "$stamp_time" ]; then
    if [ $DRYRUN -eq 0 ]; then
      set_server_to_prod
    else
      echo "DRYRUN: Would return server to prod"
    fi
  fi
}

while getopts 'dn' opt; do
  case "$opt" in
    d)
      DEBUG=1
      ;;
    n)
      DRYRUN=1
      ;;
  esac
done

check_server
