#!/bin/bash

# Author: Vyatta <eng@vyatta.com>
# Date: 2007
# Description: command wrapper

# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006, 2007, 2008 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****

# note: this script MUST be running as the vyattacfg group, e.g., with "sg".
# otherwise there WILL be permission problems with the files created.

# some env variables are needed
export vyatta_sysconfdir=/opt/vyatta/etc
export vyatta_sbindir=/opt/vyatta/sbin

# allow env variable to override default session id (ppid). this enables
# the script to handle cases where the invocations can come from
# different parents.
SID=$PPID
if [ -n "$CMD_WRAPPER_SESSION_ID" ]; then
  SID=$CMD_WRAPPER_SESSION_ID
fi
# set up the session environment (get it from the unified lib)
session_env=$(${vyatta_sbindir}/my_cli_shell_api getSessionEnv $SID)
eval "$session_env"

RET_STATUS=0

case "$1" in
  begin)
    # set up the session
    ${vyatta_sbindir}/my_cli_shell_api setupSession
    RET_STATUS=$?
    ;;
  end)
    # tear down the session
    ${vyatta_sbindir}/my_cli_shell_api teardownSession
    RET_STATUS=$?
    ;;
  cleanup|discard)
    /opt/vyatta/sbin/my_discard
    RET_STATUS=$?
    ;;
  set)
    /opt/vyatta/sbin/my_set "${@:2}"
    RET_STATUS=$?
    ;;
  delete)
    /opt/vyatta/sbin/my_delete "${@:2}"
    RET_STATUS=$?
    ;;
  deactivate)
   /opt/vyatta/sbin/my_deactivate "${@:2}"
   RET_STATUS=$?
   ;;
  activate)
   /opt/vyatta/sbin/my_activate "${@:2}"
   RET_STATUS=$?
   ;;
  show)
   ${vyatta_sbindir}/my_cli_shell_api showCfg
   RET_STATUS=$?
   ;;
  comment)
   /opt/vyatta/sbin/my_comment "${@:2}"
   RET_STATUS=$?
   ;;
  commit)
    # debug file /tmp/bar should be deleted before release
    /opt/vyatta/sbin/my_commit -a >> /tmp/bar
    /opt/vyatta/sbin/my_commit -s >> /tmp/bar
    /opt/vyatta/sbin/my_commit -e -d >> /tmp/bar
    RET_STATUS=$?
    ;;
  commit_with_error)
    /opt/vyatta/sbin/my_commit -x
    RET_STATUS=$?
    ;;
  save)
    /opt/vyatta/sbin/vyatta-save-config.pl "${@:2}"
    RET_STATUS=$?
    ;;
  load)
    /opt/vyatta/sbin/vyatta-load-config.pl "${@:2}"
    RET_STATUS=$?
    ;;
  rule-rename)
    # this option is to be used for renaming firewall and nat rules only
    # usage for this option specified on the next two lines -
    #             2        3                 4    5         6  7    8
    # rule-rename firewall $firewall_ruleset rule $rule_num to rule $rename_rulenum
    #             2   3    4         5  6    7
    # rule-rename nat rule $rule_num to rule $rename_rulenum
    if [ "$2" == "firewall" ]; then
      /opt/vyatta/sbin/my_move firewall name "$3" rule "$5" to "$8"
      RET_STATUS=$?
    elif [ "$2" == "nat" ]; then
      /opt/vyatta/sbin/my_move service nat rule "$4" to "$7"
      RET_STATUS=$?
    fi
    ;;
  move)
    # this is similar to the CLI edit+rename command.
    # e.g., "move interfaces ethernet eth2 vif 100 to 200"
    # is similar to "edit interfaces ethernet eth2" plus
    # "rename vif 100 to vif 200".
    /opt/vyatta/sbin/my_move "${@:2}"
    RET_STATUS=$?
    ;;
  *)
    echo "Invalid command \"$1\" for vyatta-cfg-cmd-wrapper"
    RET_STATUS=1  
    ;;
esac

exit $RET_STATUS