#!/bin/sh 
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)install_remote_CA_key.sh	3.8 10/21/99 Sun Microsystems, Inc.
#
# Script for setting up the remote CA key from either the installer
# (Web Start Wizard) or the script ss_install. 
#
# When executed from the wizard (with the -w flag), the script
# doesn't print the messages and doesn't expect input. The admin's
# certificate floppy is read and certificate key ID is echoed in the end.
#
# When executed from ss_install, the admin certificate key ID is
# stored in temporary files for ss_install to read.
#
# Exit Codes:
#    0       success
#

#
# BugID 4210826: installer doesn't properly config remote admin
#
PATH="/usr/bin:/usr/sbin"
export PATH

wizard=false
msg() {
  if [ "$wizard" = "false" ]
  then echo "$*"
  fi
}

set_num=134     # SI18N_INST_REMOTE_CA_KEY
LS()		# localize a string
{
	msg_num=$1; shift
        if [ -x ${SS_LIBDIR}/catgets ]
        then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
        else echo "$*"
	fi
}

#---Main---

ADMINCERTFILE=/tmp/admincert

if [ $# = "1" ] && [ $1 = "-w" ]
then
  wizard=true
fi

if [ "$wizard" = "false" ]
then
  fmt=`LS 10 "Insert the administration station's public certificate disk into the floppy drive. "`
  printf "\n$fmt\n"
  fmt=`LS 11 "hit RETURN after inserting diskette"`
  printf "$fmt\n"

  read RETURN
fi

volcheck

ADMIN_CERT_PATH="/floppy/floppy0"
ADMIN_CERT=`cd $ADMIN_CERT_PATH; ls 0*.[Cc][Rr][Tt]`

skipdb -i >/dev/null 2>&1
RESULT=`skipdb -a -n 1 -t x509 -c $ADMIN_CERT_PATH/$ADMIN_CERT 2>&1`

echo $RESULT | grep -i expire >/dev/null 2>&1
if [ $? -eq 0 ] 
then
  # warning for expired cert
  eject -p floppy0 > /dev/null 2>&1
  echo $RESULT
  exit 1
else
  # no warning for expired cert; cert OK
  ADMIN_CERT=`echo $ADMIN_CERT | sed 's/.[Cc][Rr][Tt]//g'`
  if [ "$wizard" = "true" ]
  then
    eject -p floppy0 > /dev/null 2>&1
    echo $ADMIN_CERT
  else
    eject -p floppy0
    echo $ADMIN_CERT > $ADMINCERTFILE
  fi
fi

exit 0
