#!/bin/sh 
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)install_local_CA_key.sh	3.7 07/06/99 Sun Microsystems, Inc.
#
# Script for setting up the local 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 screen's
# certificate floppy is read and certificate key ID is echoed in the end.
#
# When executed from ss_install, the screen 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=132     # SI18N_INST_LOCAL_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---

SCREENCERTFILE=/tmp/screencert

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

if [ "$wizard" = "false" ]
then
  fmt=`LS 10 "Insert the Screen's secret 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
SCREEN_CERT_PATH="/floppy/floppy0"

SCREEN_CERT=`cd $SCREEN_CERT_PATH; ls 0*.*|grep -i 'crt$' | sed 's/.[Cc][rR][Tt]//g'`

if [ "$wizard" = "true" ]
then
  # do things quietly for wizard
  RESULT=`install_skip_keys -icg $SCREEN_CERT_PATH 2>&1`

  if [ $? -eq 0 ] 
  then
    # cert load suceeded
    skipd_restart > /dev/null 2>&1
    eject -p floppy0 > /dev/null 2>&1
    echo $SCREEN_CERT
  else
    # cert load failed
    eject -p floppy0 > /dev/null 2>&1
    echo $RESULT
    exit 1
  fi

else
  # cmdline install
  install_skip_keys -icg $SCREEN_CERT_PATH 

  if [ $? -eq 0 ] 
  then
    # cert load succeeded
    skipd_restart
    eject -p floppy0 
    echo $SCREEN_CERT > $SCREENCERTFILE
  else
    # cert load failed
    eject -p floppy0 
    exit 1
  fi
fi

exit 0
