#!/bin/ksh
# 
# ident   "@(#)scsnapshot.ksh 1.1     03/12/12 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# 
# Shell front-end for the 'scsnapshot' tool.
# Reads and check command-line arguments and invokes the Perl tool.
#

# Error code definition
OK=0
ENOENT=2
EACCES=13
EINVAL=22

# Locale path is 
CLUSTER_DIR=/usr/cluster
NLSPATH=$NLSPATH:$CLUSTER_DIR/lib/locale
export NLSPATH

SCSNAPSHOT_PGM_NAME=scsnapshot

SCSNAPSHOT_LIB_DIR=$CLUSTER_DIR/lib/$SCSNAPSHOT_PGM_NAME
LIB_PATH=$SCSNAPSHOT_LIB_DIR:$CLUSTER_DIR/lib:$CLUSTER_DIR/lib/perl
export SCSNAPSHOT_LIB_DIR

duplicate_option() {
    /usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: Only one occurence of the -%s command line option is allowed.'`\n" $1 >&2
}

no_such_file() {
    /usr/bin/printf "`/usr/bin/gettext 'FATAL ERROR: No such file: %s'`\n" $1 >&2
}

write_denied() {
    /usr/bin/printf "`/usr/bin/gettext 'FATAL ERROR: Not authorized to write to file: %s'`\n" $1 >&2
}

print_usage() {
    /usr/bin/printf "\n`/usr/bin/gettext 'Usage: %s [ -s <scriptfile> ] [ -o <imagefile> ]'`" $SCSNAPSHOT_PGM_NAME
    /usr/bin/printf "\n`/usr/bin/gettext '       %s [ -s <scriptfile> ] <oldimage> <newimage>'`\n" $SCSNAPSHOT_PGM_NAME
}

# Read command-line args
sflag=
sval=
oflag=
oval=
Nflag=
Nval=
Pflag=
Pval=

while getopts s:o: name
do
  case $name in
    s) if [ -n "$sflag" ]; then
	    duplicate_option $name
	    print_usage >&2
	    exit $EINVAL
       fi
       sflag=1
       sval="$OPTARG";;
    o) if [ -n "$oflag" ]; then
	    duplicate_option $name
	    print_usage >&2
	    exit $EINVAL	
       fi
       oflag=1
       oval="$OPTARG";;
    ?) /usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR.'`\n" >&2
       print_usage >&2
       exit $EINVAL;;
  esac
done

shift `expr $OPTIND - 1`
REMAINING="$*"
CHECK_AUTHS=

# Determine and check usage scenario
if [ -z "$REMAINING" ]; then
    CHECK_AUTHS=1
else if [ -n "$oflag" ]; then
	/usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: Too many arguments.'`\n" >&2
	print_usage >&2
	exit $EINVAL
     else
	if [ $# -gt 2 ]; then
	    /usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: Too many arguments.'`\n" >&2
	    print_usage >&2
	    exit $EINVAL
	fi

	if [ $# -lt 2 ]; then
	    /usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: Missing arguments.'`\n" >&2
	    print_usage >&2
	    exit $EINVAL
	fi

	# We are sure to have two arguments
	Pflag=1
	Pval=$1
	Nflag=1
	Nval=$2
     fi
fi

# Access control
# Using, RBAC, access is granted to user having at least the resources.read 
# authorization.
if [ -n "$CHECK_AUTHS" ]; then

    AUTHS=`/usr/bin/auths`

    if [  \( -z "`echo $AUTHS | grep 'solaris\.cluster\.resource\.read'`" \) \
	 -a \( -z "`echo $AUTHS | grep 'solaris\.cluster\.resource\.\*'`" \) \
	 -a \( -z "`echo $AUTHS | grep 'solaris\.cluster\.\*'`" \) \
	 -a \( -z "`echo $AUTHS | grep 'solaris\.\*'`" \) ]; then
	/usr/bin/printf "`/usr/bin/gettext '%s: Not authorized to use this command.'`\n" \
	    $SCSNAPSHOT_PGM_NAME >&2
	exit $EACCES
    fi
fi

if [ -n "$oflag" -a -n "$sflag" ]; then
    if [ "$oval" = "$sval" ]; then
	/usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: File names specified for the -o and -s flags must not be the same.'`\n" >&2
	print_usage
	exit $EINVAL
    fi
fi

if [ -n "$Pflag" -a -n "$Nflag" ]; then
    if [ "$sval" = "$Pval" -o "$sval" = "$Nval" ]; then
	/usr/bin/printf "`/usr/bin/gettext 'USAGE ERROR: File names specified with the -s option flag must not be the same as the input image file name.'`/n" >&2
	print_usage
	exit $EINVAL
    fi
fi

# Check for file reading access
[ -n "$Pval" -a ! -r "$Pval" ] && no_such_file $Pval && exit $ENOENT
[ -n "$Nval" -a ! -r "$Nval" ] && no_such_file $Nval && exit $ENOENT

# Check for file write access
[ -n "$sval" -a -f "$sval" -a ! -w "$sval" ] && write_denied $sval && exit $EACCES
[ -n "$oval" -a -f "$oval" -a ! -w "$oval" ] && write_denied $oval && exit $EACCES

# export everything and invoke the Perl tool
#
export oflag
export oval
export sflag
export sval
export Nflag
export Pflag
export Nval
export Pval

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIB_PATH
export LD_LIBRARY_PATH

perlOpts=-I$LIB_PATH

# Locale checking with precedence order (LC_ALL then LANG) with automatic use
# of C locale if none are installed on the system
LOCALE_2_USE=C

INSTALLED_LOCALE=`/usr/bin/locale -a`

LOCALE_RESET_2_DEFAULT=""
LOCALE_SET_2_ALTERNATE=""

if [ -z "$LC_ALL" ]; then
    if [ -n "$LANG" ]; then
	if [ -n "`echo $INSTALLED_LOCALE | \grep $LANG`" ]; then
	    LOCALE_2_USE=$LANG
	else
	    /usr/bin/printf "`/usr/bin/gettext 'WARNING:\n  Locale setting specified with LANG (%s) is not supported on this system.'`" $LANG >&2
	    LOCALE_RESET_2_DEFAULT=YES
	fi
    fi
else
    if [ -n "`echo $INSTALLED_LOCALE | \grep $LC_ALL`" ]; then
	LOCALE_2_USE=$LC_ALL
    else
	/usr/bin/printf "`/usr/bin/gettext 'WARNING:\n  Locale setting specified with LC_ALL (%s) is not supported on this system.'`" $LC_ALL >&2
	if [ -n "$LANG" ]; then
	    if [ -n "`echo $INSTALLED_LOCALE | \grep $LANG`" ]; then
		/usr/bin/printf "\n  `/usr/bin/gettext 'The program will use the locale specified with $LANG (%s) instead.'`" $LANG >&2
		LOCALE_2_USE=$LANG
		/usr/bin/printf "\n\n`/usr/bin/gettext 'Current supported locales on the systems are:'`" >&2
		/usr/bin/printf "\n\n$INSTALLED_LOCALE\n" >&2
		echo "\n\n"
	    else
		/usr/bin/printf "\n`/usr/bin/gettext 'WARNING:\n  Locale setting specified with LANG (%s) is not supported on this system.'`" $LANG >&2
		LOCALE_RESET_2_DEFAULT=YES
	    fi
	else
	    LOCALE_RESET_2_DEFAULT=YES 
	fi
    fi
fi

if [ -n "$LOCALE_RESET_2_DEFAULT" ]; then
    /usr/bin/printf "\n  `/usr/bin/gettext 'The program will use default C locale instead\.'`" >&2
    /usr/bin/printf "\n\n`/usr/bin/gettext 'The following locales are currently supported on the system:'`" >&2
    /usr/bin/printf "\n\n$INSTALLED_LOCALE\n" >&2
    echo "\n\n"
fi

LC_ALL=$LOCALE_2_USE; export LC_ALL; /usr/bin/perl $perlOpts $SCSNAPSHOT_LIB_DIR/scsnapshot.pl
