#!/bin/sh

##################################################################
# File: $Id: bastille,v 1.5 2001/10/15 21:54:07 tyler_e Exp $
# This script does the following:
#   1) Determines if Perl and the required modules are installed
#   2) Configures the paths to ensure Bastille runs correctly on
#      all supported operating systems.
##################################################################

export PATH="/bin:/usr/bin:/usr/sbin:$PATH"
export PERL5LIB="/opt/sec_mgmt/bastille/lib:/usr/lib/perl5/site_perl:/usr/lib/Bastille"


set +o privileged # perl will not accept -m options while suid otherwise
# minimum version of perl is 5.005
MIN_V_MAJ=5
MIN_V_MIN=005
PARSED_PATH="`echo $PATH | sed -e 's/:/ /g'`"
PERL_PATHS="/opt/perl/bin /usr/local/bin /usr/bin /bin /usr/contrib/bin $PARSED_PATH"

  ERRORWARN='WARNING: '
  ERRSPACES='         '

retval=0    # holds current error condition only values are 0 - Success,
            # and 2 - warning for install, and either error or
            # warning for run-time execution

printErr () {
  cat >&2 << EOF!!!
$ERRORWARN Unable to find perl version $MIN_V_MAJ.$MIN_V_MIN or greater
$ERRSPACES in either your path or in the standard places for perl.
$ERRSPACES Bastille cannot function without perl $MIN_V_MAJ.$MIN_V_MIN or higher.
$ERRSPACES You may to do one of the following to resolve this problem:
$ERRSPACES   1) Install perl. It is available from
$ERRSPACES      http://www.cpan.org/ports/index.html
$ERRSPACES   2) Create a symbolic link from the correct
$ERRSPACES      version of perl to /usr/local/bin or some element in your path
$ERRSPACES      with ln -s <correct_perl> <directory in your path>/perl
$ERRSPACES   3) If you can not insert a link to one of the above directories,
$ERRSPACES      and you are sure your Perl installation is correct, you may
$ERRSPACES      elect to run Bastille without its internal checks
$ERRSPACES      by typing: <correct_perl> <path to desired perl script>

EOF!!!
}

# Look under common locations for Perl, and then the user's path

for CURRENT_PERL_PATH in $PERL_PATHS
do
  if [ ! -x ${CURRENT_PERL_PATH}/perl ]; then
    FOUND=no;
  else
    FOUND=yes;break
  fi
done


if [ $FOUND = 'no' ]; then
   printErr
   exit 2
fi

# Now we have some version of perl
# We check that the version is at least the minimum

PERL_VERSION=`${CURRENT_PERL_PATH}/perl -version | head -2 | tr " "  "\n" |\
              sed -e "s/^v//" | grep "^[1-9]\." | sed -e "s/_*$//"`
PERL_V_MAJ=${PERL_VERSION%.*}
PERL_V_MIN=`echo ${PERL_VERSION#*.}| sed -e "s/_.*$//"` # remove _<patch level>
                                                        #can't do with "right
                                                        #pattern match"

if (( $PERL_V_MAJ < $MIN_V_MAJ )) ||  (( $PERL_V_MAJ == $MIN_V_MAJ && $PERL_V_MIN < $MIN_V_MIN )); then  # we hav
e an invalid version of Perl
  printErr
  retval=2
else
# We have a valid version of perl! Verify that all the required
# modules can be found.
if [[ "${1}x" != "-bx" && "${1}x" != "-ux" ]]; then
    missingmod=0 # flag to indicate if missing mod found.
    for mod in "Tk"
    do
	# see if perl can find the module
	${CURRENT_PERL_PATH}/perl -M$mod < /dev/null > /dev/null 2>&1
	if [ $? != 0 ]; then
	# Cannot find module
	retval=2
	if [ $missingmod = 0 ]; then
	    # First error message printed here
	    missingmod=1;
	    print >&2 "$ERRORWARN ${CURRENT_PERL_PATH}/perl cannot find Perl module $mod."
	else
	    print >&2 "$ERRSPACES ${CURRENT_PERL_PATH}/perl cannot find Perl module $mod."
	fi
	fi
    done
    if [ $missingmod = 1 ]; then # There were missing modules
    cat >&2 << EOF!!!
$ERRSPACES The above module(s) is/are required to correctly display the Bastille User Interface
$ERRSPACES Most Perl ports contain all the required modules, and are found at:
$ERRSPACES http://www.cpan.org/ports/index.html
$ERRSPACES if the port listed for your OS does not contain these modules, they can be 
$ERRSPACES found at http://www.cpan.org/modules/01modules.index.html


EOF!!!
    fi
  fi
fi


if [ -e /opt/sec_mgmt/bastille/bin/bastille ]; then
    scripts_location=/opt/sec_mgmt/bastille/bin
elif [ -e /usr/sbin/InteractiveBastille ]; then
    scripts_location=/usr/sbin
else
    echo "Can not find Bastille scripts..."
    retval=1
fi

if [ $retval != 0 ]; then  # exit if any problems earlier in script
    exit $retval
fi

if [ "${1}x" = "-bx" ]; then # run correct program
    shift
    ${CURRENT_PERL_PATH}/perl $scripts_location/BastilleBackEnd $@
elif [ "${1}x" = "-ux" ]; then
    shift
    ${CURRENT_PERL_PATH}/perl $scripts_location/UndoBastille $@
else
    ${CURRENT_PERL_PATH}/perl $scripts_location/InteractiveBastille $@
fi


