#!/bin/bash


#!/bin/bash

#########################################
# Name: skimm
#
# Summary: Multiple skim
#
# Description: use skim to display multiple IDs
#
# Author: k. headley
#
# Copyright 2015 MBARI
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="[use skim to display multiple IDs]"

#################################
# Script variable initialization
#################################
VERBOSE="N"

declare -a IDS
unset period_opt
arch_opt="-a arch"

#################################
# Function Definitions
#################################
#################################
# name: sigTrap
# description: signal trap callback
# will interrupt
# args: none
#################################
sigTrap(){
exit 0;
}

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
echo
echo "Description: $description"
echo
echo "usage: `basename $0` [options]"
echo "Options:"
echo "-H <hours> : hours"
echo "-M <min>   : min"
echo "-S <sec>   : sec"
echo "-a <arch>  : archive directory"
echo "-s <id...> : source ids"
echo "-V             : verbose output          [$VERBOSE]"
echo "-h             : print use message"
echo ""
echo
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
vout(){
if [ "${VERBOSE}" == "Y" ] || [ "${VERBOSE}" == "TRUE" ]
then
echo "$1" >&2
fi
}

########################################
# name: imsg
# description: echos with indent
# args:
#     msg: message
#  indent: indent level
########################################
imsg(){
OIFS=$IFS
IFS=""
msg_pad="                                        "
let "msg_indent=${1}"
shift
all="${@}"
printf "%s%s\n" ${msg_pad:0:${msg_indent}} ${all}
IFS=$OIFS
}

########################################
# name: msg
# description: echos with default indent
# args:
#     msg: message
########################################
msg(){
imsg $MSG_INDENT "${*}"
}

########################################
# name: exitError
# description: print use message to stderr
# args:
#     msg:        error message
#     returnCode: exit status to return
########################################
exitError(){
echo >&2
echo "`basename $0`: error - $1" >&2
echo >&2
exit $2
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
processCmdLine(){
OPTIND=1
vout "`basename $0` all args[$*]"

while getopts a:s:hH:M:S:v Option
do
vout "processing $Option[$OPTARG]"
case $Option in
a ) arch="$OPTARG"
    arch_opt="-a $OPTARG"
;;
s ) ids="$OPTARG"
;;
H ) period=$OPTARG
    period_opt="-H $OPTARG"
;;
M ) period="$OPTARG"
    period_opt="-M $OPTARG"
;;
S ) period="$OPTARG"
    period_opt="-S $OPTARG"
;;
V ) VERBOSE="TRUE"
;;
h) printUsage
exit 0
;;
*) exit 0 # getopts outputs error message
;;
esac
done
}

##########################
# Script main entry point
##########################


# Argument processing
# Accepts arguments from command line
# or pipe/file redirect
# Comand line settings override config
# file settings

if [ -t 0 ]
then
    if [ "$#" -eq 0 ];then
    printUsage
    exit -1
    else
    processCmdLine $*
    let "i=$OPTIND-1"
    #while [ "${i}" -gt 0  ]
    #do
    #	shift
    #	let "i-=1"
    #done
    fi
fi

vout "X_ARG   : $X_ARG"
vout "VERBOSE : $VERBOSE"

# call sigTrap on INT,TERM or EXIT
# trap sigTrap INT TERM EXIT

# reset trapped signals
# trap - INT TERM EXIT


IFS="," read -a IDS <<< "$ids"

vout "arch[$arch_opt] per[$period_opt]"

for id in "${IDS[@]}"
do
vout "skim $arch_opt $period_opt $id"
echo "ID: $id"
skim $arch_opt $period_opt -s $id
done
