#!/bin/sh
# bin/sfindcerts -- an ITISSL program
# ITISSL - a Java 2 implementation for  Sun's reference SSL API  using SSLeay
# Copyright (C) 1999 Andrei Popovici (apopovic@iti.informatik.tu-darmstadt.de)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# sfindcerts			15 Feb 99
#------------------------------------------------------------

#
# find certificates
#
# (history at end)


# SYNOPSIS
#
#
# sfindcerts  -key | -cert | -ca [ -sum]
#
# find user certificates and keys, find ca certificates;

usage() {
echo " sfindcerts  -key | -cert | -ca [ -sum] ";
exit 1;
}

user_dir=${ITISSL_USER_DIR:-"~/.ssl"};
sys_dir=${ITISSL_SYS_DIR:-"/etc/ssl"};

user_cert_file=${ITISSL_USER_CERT:-"cert.pem"};
sys_cert_file=${ITISSL_SYS_CERT:-"cert.pem"};
user_key_file=${ITISSL_USER_KEY:-"key.pem"};
sys_key_file=${ITISSL_SYS_KEY:-"key.pem"};
user_ca_file=${ITISSL_USER_CA:-"ca.pem"};
sys_ca_file=${ITISSL_SYS_CA:-"ca.pem"};


# find locations
user_cert="$user_dir/$user_cert_file";
sys_cert="$sys_dir/$sys_cert_file";
user_key="$user_dir/$user_key_file";
sys_key="$sys_dir/$sys_key_file";
user_ca="$user_dir/$user_ca_file";
sys_ca="$sys_dir/$sys_ca_file";

#--------------------------------------------------
# operations
#--------------------------------------------------



set_key_result() {

  { [ -r "$KEY_FILE" ] &&  resultloc=$KEY_FILE; } ||
  { [ -r "$user_key" ] &&  resultloc=$user_key; } ||
  { [ -r "$sys_key"  ] &&  resultloc=$sys_key; }
}

set_cert_result () {
 { [ -r "$CERT_FILE" ] && resultloc=$CERT_FILE;} ||
 { [ -r "$user_cert" ] && resultloc=$user_cert;} ||
 { [ -r "$sys_cert"  ] && resultloc=$sys_cert;}
 }


 set_one_ca_result () {
  { [ -r "$CA_FILE" ] &&  resultloc=$CA_FILE; } ||
  { [ -r "$user_ca" ] &&  resultloc=$user_ca; } ||
  { [ -r "$sys_ca"  ] &&  resultloc=$sys_ca; }
  }

set_sum_ca_result () {
    if [ -d $ITISSL_TMP_DIR ]
    then
	resultloc=${ITISSL_TMP_DIR}/sfindcertpid$$;
    else
        resultloc=/tmp/sfindcertpid$$;
   fi;
   touch $resultloc;
   [ -r "$CA_FILE" ] &&  cat $CA_FILE >> $resultloc;
   [ -r "$user_ca" ] &&  cat $user_ca >> $resultloc;
   [ -r "$sys_ca"  ] &&  cat $sys_ca  >> $resultloc;
   }


# parse options

unset resultloc;


# don't print errors..


(while [ $# -gt 0 ]
do

  case "$1" in

    -key)
        set_key_result;
        break;
        ;;

    -cert)
        set_cert_result;
	break;
	;;
    -ca)
	if [ "$2" = "-sum" ]
	then
	    set_sum_ca_result;
	else
	    set_one_ca_result;
	fi;
	break;
	;;
     *)
        break
        ;;
  esac

  shift
done


[ -f $resultloc ] || exit 1;
echo $resultloc;
) 2>/dev/null

#======================================================================
#
# Log:
#
# 15 Feb 99 XXX Initial revison
