#!/bin/sh
#  File: sbc_pre_event_group
#
#
# Sesam PRE event procedure for Linux servers
#
# Description: 
#    This script is called from SEP sesam, before 
#    tasks belonging to a task group are read from Sesam database.
#    It gives the user a possibility to add/delete/modify tasks
#    belonging to a task group.

usage()
{
echo "Usage: sm_pre_event_group <groupname> [options]"
echo "    groupname: task group name"
echo ""          
echo "    Options:"          
echo "        -a tags"
echo "        -b backup type"
echo "        -c client"
echo "        -f folder/instance"
echo "        -i <regex>:   include filter"
echo "        -k keys"
echo "        -o operating system"
echo "        -p power/online state"
echo "        -t tags"
echo "        -x <regex>:  exclude filter"
echo ""
echo "        -z options"
echo "        -e <prefix>: task name prefix"
}

#
# Examples:             
#    Create vSphere backup tasks: 
#

smlog()
{
   echo "$( date +"%Y-%m-%d %T:" )" Info: "$1" >&2
}

client=""
task_type=""
exclude=""
include=""
keys=""
tags=""
folder=""
task_prefix=""

if [ "$1" = "-h" ]
then
   usage
   exit 1
fi


group_name=$1
# shift one parameter because $1 is group name !
shift

smlog "Create backup tasks for task group: $group_name"
# getting all named options

while getopts a:b:c:e:f:o:i:k:p::t:x: opt
do
      case $opt in
         a) tags=$OPTARG;;
         b) backup_type=$OPTARG;;
         c) client=$OPTARG;;
         f) folder=$OPTARG;;
         i) include=$OPTARG;;
         k) keys=$OPTARG;;
         o) os=$OPTARG;;
         p) power_state=$OPTARG;;
         t) tags=$OPTARG;;
         x) exclude=$OPTARG;;
         e) task_prefix=$OPTARG;;
         z) options=$OPTARG;;
         h) usage
            exit 1;;
      esac
done
shift `expr $OPTIND - 1`

smlog "Client:          ${client}"
smlog "Task type:       ${backup_type}"
smlog "Folder:          ${folder}"
smlog "Include filter:  ${include}"
smlog "Exclude filter:  ${exclude}"
smlog "Keys:            ${keys}"
smlog "Tags:            ${tags}"

sm_cmd dir -F JSON client -o refresh,recursive -P "${power_state}" -x "${exclude}" -i "${include}" -k "${keys}" -A "${tags}" -F "${folder}" "${client}/${backup_type}:"|sm_cmd -A java add taskgen -v 1 "${task_prefix}{{client}}_{{source_last_item}}{{underPrefix count_optional}}" -t "${backup_type}" -G "${group_name}" -O "source_not_found,remove_non_existing" -f -
iRet=$?

sm_db "select task from task_group_relations where grp_name='$group_name' order by task"
exit $iRet
