#!/bin/sh
#  $Id: prepare_tclscript.sh 144933 2017-02-08 09:50:25Z justin.carveley@oracle.com $
#  Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information
#  and should be treated as confidential.
#
#  Component: Generic shell wrapper to tcl script
#  Author: barrys
#
#

. "common.sh"

case "$trace" in
x) set -x
esac

#load localisable strings into the shell
getmessages ()
{
    if [ -f $1/common -a -f $1/$2 ]; then
	. $1/common
	. $1/$2
    else
	# load message specific to group of commands
	if [ -f $1/tta_$3 ]; then
	    . $1/tta_$3
	else
	    exit_on_error "Unable to open message file: $1/$2"
        fi
    fi
}

if [ $# = 0 ];
then
    echo "$l_takesargs" 1>&2
    echo "$l_usage $who_i_am $lf_usage" 1>&2
    echo "$lf_usage2" 1>&2
    echo "$lf_usage3" 1>&2
    echo "$lf_usage4 "tarantella" $who_i_am $lf_usage5" 1>&2
    exit 1  
fi 

# all other arguments are forwarded (unvalidated) to <command>.tsh
# get localisable strings (string directory, merged command name (admin_new_user) and base command name (e.g admin)
getmessages $TTAMSG "$2" "$1"
shift
command=$1
script="$TTABINDIR/scripts/$1.tsh"

for arg in $@; do
    case "$arg" in
	--help )
	    file="$TTAMSG/$1.msg"
	    # check msg file exists
	    if [ -f $file ]; then

		## find tcl
		tcl=/usr/bin/tclsh
		# check exists?

		## locate the tcl script
		tclscript=$TTABINDIR/scripts/longusage.tcl
		# check exists?

		## run the script
		$tcl $tclscript $file

		# we're done
		exit 0
	    fi
    esac
done

if [ "$2" = "--help" ]; then
    echo "$l_usage $2 $who_i_am $lf_usage"
    echo "$lf_usage2"
    echo "$lf_usage3"
    echo "$lf_usage4 $2 $who_i_am $lf_usage5"
    exit 0
fi


if [ -f $script ]; then
    exec "$TTABINDIR/scripts/run_tclscript" "$@" 
else
  echo "" 1>&2
  echo "$lf_badarg $script " 1>&2
  echo "" 1>&2
  echo "$lf_usage6" 1>&2
  echo "$lf_usage3" 1>&2
  exit 1
fi



