#!/bin/bash
#This is new ca 


#include config
. "./ca.config.sh"

#call mkrnd first time !
#mkrnd

echo ""
echo "##############################"
echo "# pre-Beta!!                 #"
echo "##############################"
echo ""

echo ""
echo "Creating a new CA in $CA"
echo "------------------------------------------------"
echo ""

echo stop: $STOP

if [ "$STOP" != "" ]
then
  echo "Please edit ca.config.sh and template.cnf"
  echo "  (and Makefile.tmpl if desired)"
  echo "for your requirements. Then remove the last line from ca.config.sh"
  echo "and call this script again."
  echo ""
  exit 1;
fi

if [ ! -e $SSL ]
then
  echo "FATAL: $SSL not found!"
  echo "Press a key..."
  read
fi

#creating dirs...
if [ -e $CA ]
then
  echo "FATAL: $CA exists!!"
  echo "Remove or rename it!"
  echo ""
  echo "in $CA you can try 'make help'."
  echo "if somthing went wrong creating CA Certs, you may try:"
  echo "'make newCA', if it fails try 'make CA-WIPEOUT newCA'"
  echo ""
  exit 1;
fi

mkdir $CA
mkdir $CPATH
mkdir $CA/private
chmod 700 $CA/private
mkdir $CRLDIR
mkdir $CA/newcerts

if [ -d $CA_WWW_DIR ]
then
  echo "WARNING: $CA_WWW_DIR is not a directory!"
  echo "(you may create manually if dir is correct)"
  echo "Press a key..."
  read
fi

if [ ! -r template.cnf ] 
then
  echo "template.cnf not readable!"
  exit 1;
fi

if [ -e $CNF ] 
then
  echo "$CNF does already exists - preserving existing data..."
else
  #generate OpenSSL- configfile
  touch $CNF
  if [ ! -e $CNF ]
  then
    echo "error creating: $CNF"
    exit 1;
  fi
  echo "generating new OpenSSL configfile $CNF..."
  echo "#OpenSSL Configuration for $CANAME-CA"	>> $CNF
  echo ""					>> $CNF
  echo "#################"			>> $CNF
  echo "# Globals"				>> $CNF
  echo "#############################"		>> $CNF
  echo ""                                       >> $CNF
  echo "RANDFILE		= $CA/private/.rnd"	>> $CNF
  echo "#oid_file		= $CA/.oid"		>> $CNF
  echo ""					>> $CNF
  echo "#dir			= $CA"			>> $CNF
  echo "certs			= $CPATH"		>> $CNF
  echo "crl_dir			= $CRLDIR"		>> $CNF
  echo "database		= $CA/index.txt"	>> $CNF
  echo "new_certs_dir		= $NEW_CERTS_DIR"	>> $CNF
  echo ""                                       >> $CNF
  echo "certificate		= $CACERT.pem"		>> $CNF
  echo "serial			= $CA/serial"		>> $CNF
  echo "crl			= $CRL"			>> $CNF
  echo "private_key		= $CAKEY.pem"		>> $CNF
  echo ""                                       >> $CNF
  echo "nsBaseUrl_def		= $NS_BASE_URL"		>> $CNF
  echo "admin_email		= $CAADMIN_MAIL"	>> $CNF
  echo "nsRevocationUrl_def	= cgi/check-rev.cgi?"	>> $CNF
  cat template.cnf				>> $CNF
fi

if [ ! -r Makefile.tmpl ]
then
  echo "Makefile.tmpl not readable!"
  exit 1;
fi

#generate CA-Makefile
touch $CA/Makefile
if [ ! -e $CA/Makefile ]
then
  echo "error creating: $CA/Makefile"
  exit 1;
fi

echo "generating Makefile ..."

ECHO=`which echo`
test -x $ECHO || (echo "which echo failed!"; exit 1)

CHMOD=`which chmod`
test -x $CHMOD || (echo "which chmod failed!"; exit 1)

RM=`which rm`
test -x $RM || (echo "which rm failed!"; exit 1)

DATE=`which date`
test -x $DATE || (echo "which date failed!"; exit 1)

CP=`which cp`
test -x $CP  || (echo "which date failed!"; exit 1)

TEST=`which test`
test -x $TEST || (echo "which test failed!"; exit 1)

BASENAME=`which basename`
test -x $BASENAME || (echo "which basename failed!"; exit 1)

DIRNAME=`which dirname`
test -x $DIRNAME || (echo "which dirname failed!"; exit 1)

echo "SSL=$SSL"			>> $CA/Makefile
echo "CA=$CA"			>> $CA/Makefile
echo "CPATH=$CPATH"		>> $CA/Makefile 
echo "CRL=$CRL"			>> $CA/Makefile 
echo "CRLDIR=$CRLDIR"		>> $CA/Makefile
echo "CNF=$CNF"			>> $CA/Makefile 
echo "CACERT=$CACERT"		>> $CA/Makefile
echo "NEWCERTS=$NEW_CERTS_DIR"	>> $CA/Makefile
echo "CAKEY=$CAKEY"		>> $CA/Makefile
echo "CAREQ=$CAREQ"		>> $CA/Makefile
echo ""				>> $CA/Makefile
echo "ECHO=$ECHO" 		>> $CA/Makefile
echo "CHMOD=$CHMOD"		>> $CA/Makefile
echo "RM=$RM"			>> $CA/Makefile
echo "TEST=$TEST"		>> $CA/Makefile
echo "CP=$CP"			>> $CA/Makefile
echo "DATE=$DATE"		>> $CA/Makefile
echo "BASENAME=$BASENAME"	>> $CA/Makefile
echo "DIRNAME=$DIRNAME"		>> $CA/Makefile
echo ""				>> $CA/Makefile
cat Makefile.tmpl		>> $CA/Makefile

#Now generate a CA
echo ""
echo "------------------------------------------------------------"
echo "*** SetUp ready. in $CA you'll find a Makefile."
echo "*** try make help (in $CA) for help"
echo "***"
echo "*** We start with 'make CA-RESET newCA' right now"
echo "------------------------------------------------------------"
echo ""
make -f $CA/Makefile CA-RESET newCA
echo "now you can view the cert with:"
echo "make -f $CA/Makefile show-ca"
echo "or step in $CA directory and type 'make show-ca'"
echo ""
echo "[ installation done. ]"
echo ""



