#!/bin/sh
#
#     Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
#     Use is subject to license terms.
#

if [ "$1" != "register" -a "$1" != "proxy" ]; then
   echo "The current supported subcommands are register and proxy.";
   echo "The syntax for register: sconadm register -a -r <registration profile> -u <user name> -h <host name>  -e softwareUpdate  -p <proxy host>[:proxy port] -x <proxy user name> -l <location of log file>";
   echo "The syntax for proxy: sconadm proxy -r <registration profile>  -p <proxy host>[:proxy port] -x <proxy user name> -l <location of log file>";
   exit 1;
fi

######
#
# Basic Registration CLI starts here
#
######


#####
#
#  Setup environment
#
#####
MAIN_JAR="/usr/lib/breg/basicreg.jar"
LD_LIBRARY_PATH="/usr/lib/dc"
export LD_LIBRARY_PATH
AWK_EXEC=/usr/bin/awk
LS_EXEC=/usr/bin/ls
FILE_PERMISSION_400="-r--------"
FILE_PERMISSION_600="-rw-------"

REGISTRATION_PROFILE_TEMPLATE="/usr/lib/breg/data/RegistrationProfile.properties"
REGISTRATION_PROFILE=""
NEVER_REGISTER="false"
ACCEPT_TOU_BCL="false"
SUBCOMMAND=$1
LOG_LOCATION="/tmp"

if [ "$1" = "proxy" ]; then
   REGISTRATION_PROFILE=$REGISTRATION_PROFILE_TEMPLATE
fi

shift

######
#
# save arguments 
#
#####

ARGUMENTS=$*

######
#
# extract registration profile 
#
#####

while [ "$1" != "" ]; do
   case $1 in 
      -a )       
                 ACCEPT_TOU_BCL="true"
                 ;;
      -r )       shift
                 REGISTRATION_PROFILE=$1
                 ;;
      -l )       shift 
                 LOG_LOCATION=$1
                 ;;
      -N )        
                 NEVER_REGISTER="true"
                 REGISTRATION_PROFILE=$REGISTRATION_PROFILE_TEMPLATE
                 ;;
      * )        
                 ;;
  esac
  shift
done

######
#
# Check to see if accept_TOU_BCL is provided 
#
#####


if [ "$ACCEPT_TOU_BCL" = "false" -a "$NEVER_REGISTER" = "false" -a "$SUBCOMMAND" = "register" ]; then

    echo "You must accept terms of use (by passing in -a) in order to register."
    echo "The terms of use and binary code license agreement are located at:" 
    echo "Terms of Use :  "
    echo "http://sun.com/applink/sunuc/tou1en"
    echo ""
    echo "Binary Code License Agreement :"
    echo "http://sun.com/applink/sunuc/bcl1en"
    exit 1
fi


######
#
# Check to see if registration profile provided 
#
#####


if [ "$REGISTRATION_PROFILE" = "" -a "$SUBCOMMAND" = "register" ]; then

    echo "A registration profile is needed"
    exit 1
fi

######
#
# Check to see if registration profile exists 
#
#####


if [ -f "$REGISTRATION_PROFILE" ]; then
   : 
else

    echo "The registration profile $REGISTRATION_PROFILE doesn't exist"
    exit 1
fi

######
#
# Check to see if location of log exists 
#
#####


if [ -d "$LOG_LOCATION" ]; then
   : 
else

    echo "The location of log file $LOG_LOCATION doesn't exist"
    exit 1
fi


######
#
# Check to see if the ownership is root 
#
#####

OWNERSHIP="`$LS_EXEC -l $REGISTRATION_PROFILE | $AWK_EXEC '{print $3}'`"

if [ ${OWNERSHIP} != "root" ]; then

    echo "The ownership of ${REGISTRATION_PROFILE} must be root"
    exit 1;
fi

######
#
# Check to see if the file permission is 400 or 600 
#
#####

FILE_PERMISSION="`$LS_EXEC -l $REGISTRATION_PROFILE | $AWK_EXEC '{print $1}'`"

if [ ${FILE_PERMISSION} != ${FILE_PERMISSION_400} -a ${FILE_PERMISSION} != ${FILE_PERMISSION_600} ]; then

    echo "The file permission of ${REGISTRATION_PROFILE} must be either 400 or 600"
    exit 1;

fi

######
#
# Check to see if root is invoking this script
#
#####
set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
        echo "$0: must be run as root"
        exit 1
fi

#####
#
# Run the code
#
#####
exec java -classpath ${MAIN_JAR} com.sun.cns.basicreg.BasicRegCLI $SUBCOMMAND ${ARGUMENTS} 
