#!/bin/sh 
#
# Copyright 1996 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)ss_copy_key.sh	3.3 02/01/99 "Sun Microsystems"
#

PATH="/opt/SUNWicg/SunScreen/lib:/opt/SUNWicg/SunScreen/bin"
export PATH

error() {
  echo>&2 "$0: $1 -- Exiting..."
  exit 1
}

install_icg_keys() {

  dirname=$1

  # need to make this script more intelligent, to cover all uppercase
  # file names and all lower case (diff. between Solaris 2.6 and 2.5.1)
  if [ -f "$dirname/dh.key" ]
  then
    # This is from a 2.5.1 admin
    if [ ! -f "$dirname/dh.crt" ]
    then
      error "File dh.crt is missing from $dirname"
    fi
    if [ ! -f "$dirname/sunicg_c" ]
    then
      error "File sunicg_c is missing from $dirname"
    fi
    skipca add $dirname/sunicg_c
    if [ $? -eq 1 ]
    then
      error "Unable to add Trusted CA"
      elsif [ $? -eq 2]
      echo "Non-fatal error, continuing..."
    fi
    if [ -f $dirname/secret_i ]
    then
      sf=$dirname/secret_i
      so=-s
    else
      sf=$dirname/dh.key
      so=-S
    fi
    skiplocal add $so $sf -c $dirname/dh.crt -t x509 -o soft -n 1
    if [ $? -eq 1 ]
    then
      error "Unable to create local identity"
    fi
    skipdb add -n 1 -t x509 $dirname/dh.crt
    if [ $? -eq 1 ]
    then 
      error "Unable to add my own certificate to the database"
    fi
    
  elif [ -f "$dirname/DH.KEY" ]
  then
    # This is from 2.6 admin station.
    if [ ! -f "$dirname/DH.CRT" ]
    then
      error "File dh.crt is missing from $dirname"
    fi
    if [ ! -f "$dirname/SUNICG_C" ]
    then
      error "File sunicg_c is missing from $dirname"
    fi
    skipca add $dirname/SUNICG_C
    if [ $? -eq 1 ]
    then
      error "Unable to add Trusted CA"
      elsif [ $? -eq 2]
      echo "Non-fatal error, continuing..."
    fi
    if [ -f $dirname/SECRET_I ]
    then
      sf=$dirname/SECRET_I
      so=-s
    else
      sf=$dirname/DH.KEY
      so=-S
    fi
      skiplocal add $so $sf -c $dirname/DH.CRT -t x509 -o soft -n 1
    if [ $? -eq 1 ]
    then
      error "Unable to create local identity"
    fi
    
    skipdb add -n 1 -t x509 $dirname/DH.CRT
    if [ $? -eq 1 ]
    then 
      error "Unable to add my own certificate to the database"
    fi
    else
    # This is not a CA disk!
    error "File dh.key is missing from $dirname"
  fi



  return 0
}


#
#  Install the keys with SKIP
#

install_icg_keys $1
if [ $? != 0 ] ; then
  error "Unable to run install_skip_keys"
fi

exit 0
