#!/bin/sh

#   Copyright 2011 Red Hat, Inc.
#
#   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.

usage()
{
cat << EOF

USAGE:
aeolus-configure [-d|--debug] [-h|--help] [-v|--verbose] [-p|--profile]

OPTIONS:
   -h | --help       Show this message.
   -d | --debug      Debug logging mode.
   -v | --verbose    Verbose logging mode.
                     Note that you can only set one of the previous two options per run. If both are set,
                     the last one passed in will be honored.
   -p | --profile    Name of profile to use. A comma separated list can be used to specify multiple profiles.
   -s | --skip-missing  Skip missing profiles provided by -p option.
EOF
}

args=`getopt -o hdvip:s --long help,debug,verbose,interactive,profile:,skip-missing -- "$@"`
if test $? != 0
     then
         usage
         exit 1
fi

INTERACTIVE=1

PUPPET_NODE='default'
SKIP_MISSING_NODE=0

eval set -- $args
while true ; do
        case "$1" in
                -h|--help) usage ; exit 1 ;  shift ;;
                -d|--debug) LOGLEVEL="--debug" ; shift ;;
                -v|--verbose) LOGLEVEL="--verbose" ; shift  ;;
                -p|--profile) PUPPET_NODE=$2 ; shift ; shift ;;
                -s|--skip-missing) SKIP_MISSING_NODE=1 ; shift ;;
                --) shift ; break ;;
                *)  usage ; exit 1 ;;
        esac
done
echo $args | grep -e '--\ .*' -q
if [ $? == 0 ]; then
  echo "Invalid options supplied."
  usage
  exit 1
fi
echo "Launching aeolus configuration recipe..."

export FACTER_AEOLUS_ENABLE_HTTPS=true
export FACTER_AEOLUS_ENABLE_SECURITY=false
export FACTER_RAILS_TOKEN=`</dev/urandom tr -dc a-f0-9 | head -c128`

HAS_FAILURES=0

NODE_ARRAY=(`echo $PUPPET_NODE | tr "," "\n"`)
for x in "${NODE_ARRAY[@]}"
do
    if [ -f /etc/aeolus-configure/nodes/${x}_configure ]; then
      /usr/share/aeolus-configure/modules/aeolus/aeolus-node-check /etc/aeolus-configure/nodes/${x}_configure
    else
      echo Error: ${x} configure node is missing
      echo \"/etc/aeolus-configure/nodes/${x}_configure\" does not exist
      if [ $SKIP_MISSING_NODE == 1 ]; then
        echo ignoring
        continue
      else
        echo exiting
        exit
      fi
    fi
    if [ $? != 0 ]; then
      exit 1
    fi
    puppet /usr/share/aeolus-configure/modules/aeolus/aeolus.pp \
       --modulepath=/usr/share/aeolus-configure/modules/ \
       --external_nodes "/bin/sh /usr/share/aeolus-configure/modules/aeolus/aeolus-node ${x}_configure" --node_terminus exec \
       --logdest=/var/log/aeolus-configure/aeolus-configure.log \
       --logdest=console \
       $LOGLEVEL \
       --detailed-exitcodes
    if [ $? != 0 -a $? != 2 ] ; then
      HAS_FAILURES=1
    fi
done

if [ $HAS_FAILURES == 1 ]; then
    exit 1
fi
