#!/bin/sh
#
# Wrapper to call rudder commands.
#
# All commands are taken from $BASEDIR (/opt/rudder/share/commands).
#
# To add a new command, just drop it in this directory with the name <role>-<command>
#  where <role> can be 'agent' or 'server' and <command> is your command name.
# The command file is executed with remaining arguments and can be written in any executable form.
# The command file should contain a line with a description of the form "# @description <your description>"
#


set -e
BASEDIR="/opt/rudder/share/commands"
export BASEDIR
REL_PATH=`dirname "$0"`
ABS_PATH=`cd "${REL_PATH}" && pwd`
RUDDER_BIN="${ABS_PATH}/`basename $0`"
export RUDDER_BIN
# Prepend the path containing rudder binaries to use them if available
# and prepend the gnu path on solaris for better tools
# and append the sbin that sometimes is not there ...
PATH="/opt/rudder/bin:/usr/gnu/bin:$PATH:/sbin:/usr/sbin"
export PATH
role_list() {
  ls "${BASEDIR}" | sed 's/\([[:alpha:]_]*\)-.*/\1/' | uniq
}

usage() {
  echo ""
  echo "Usage: rudder help"
  for role in `role_list`
  do
    echo "       rudder ${role} <command> [parameters ...]"
  done
  echo ""
  echo "  This is rudder generic command line interface."
  echo ""
  echo "  help: this help"
  for role in `role_list`
  do
    echo "  ${role}: rudder ${role} related commands"
  done
  echo ""
}

role_usage() {
  role="$1"
  echo ""
  echo "Usage: rudder ${role} help"
  echo "       rudder ${role} <command> [parameters ...]"
  echo "       rudder ${role} <command> --help"
  echo ""
  echo "  Run commands on ${role}."
  echo ""
  echo "  Available commands:"
  for file in `cd "${BASEDIR}"; find . -type f -name "${role}-*" | sort`
  do
    doc=`sed -ne "/#[ \t]*@description[ \t]*/s/#[ \t]*@description[ \t]*//p" "${BASEDIR}/${file}"`
    name=`echo "${file}" | sed -e "s/\.\/${role}-//"`
    printf "    %-10s\t%s\n" "${name}" "${doc}"
  done
  echo ""
}

command_help() {
   role="$1"
   command="$2"
   options="$3"
   echo ""
   echo "Usage: rudder ${role} ${command} [parameters ...]"
   echo ""
   echo "${options}"
   echo ""
}

# first parameter is role parameter
role=`echo "$1" | sed 's/[^[:alpha:]_-]*//g'`
if [ "${role}" = "help" ] || [ -z "${role}" ]
then
  if [ -z "$2" ]
  then
    usage
  else
    role=`echo "$2" | sed 's/[^[:alpha:]_-]*//g'`
    role_usage "${role}"
  fi
  exit 0
fi
shift

# test validity of role parameter
for installed_role in `role_list`
do
  if [ "${role}" = "${installed_role}" ]
  then
    role_ok="y"
  fi
done

if [ "${role_ok}" != "y" ]
then
  echo "Unknown role '${role}'"
  echo ""
  usage
  exit 1
fi

# Single binary commands support
if [ -x "${BASEDIR}/${role}" ]
then
  exec "${BASEDIR}/${role}" "$@"
fi

# second parameter is command parameter
command=`echo "$1" | sed 's/[^[:alpha:]_-]*//g'`
if [ "${command}" = "help" ] || [ "${command}" = "" ]
then
  role_usage "${role}"
  exit 3
fi
shift

# test validity of command parameter
if [ ! -f "${BASEDIR}/${role}-${command}" ]
then
  echo "Command ${command} on ${role} not found"
  echo ""
  role_usage "${role}"
  exit 3
fi

# test if third parameter is --help
option=`echo "$1" | sed 's/[^[:alpha:]_-]*//g'`
if [ "${option}" = "--help" ] || [ "${option}" = "-h" ] || [ "${option}" = "help" ]
then
  options=`sed -ne "/#[ \t]*@man[ \t]*/s/#[ \t]*@man[ \t]*//p" "${BASEDIR}/${role}-${command}" |
           sed "s/*//g" | sed "s/+//g" | sed 's/^-/  -/' | sed 's/^/  /'`
  command_help "${role}" "${command}" "${options}"
  exit 0
fi

# Test if we are root
# some commands need to be called from rudder-relayd
if [ "$(id -u)" != "0" ] && [ "$(id -un)" != "rudder-relayd" ]
then
  echo "WARNING: Not running as root, this may cause problem"
fi

# OK GO !
exec "${BASEDIR}/${role}-${command}" "$@"
