Certs#!/bin/bash

#-----------------------------------------------------------------------------------------
# Make temp folder and move into it.
mytempdir=`mktemp -d -t 'mytmpdir'`
cd $mytempdir
#-----------------------------------------------------------------------------------------
# Download Portal certificate #1. If it fails to download, exit. 
curl -O http://it.ironk12.org/files/certs/cert_Captive-Portal.crt > /dev/null 2>&1
DOWNLOAD_RESULT=$?
if [ $DOWNLOAD_RESULT -eq 0 ] ; then
    :
else
	echo ""
    echo "Failed to download portal certificate #1."
    echo "Please contact your system administrator for help."
	echo ""
    exit
fi
#-----------------------------------------------------------------------------------------
# Download Portal certificate #2. If it fails to download, exit. 
curl -O http://it.ironk12.org/files/certs/cert_Root-CA.crt > /dev/null 2>&1
DOWNLOAD_RESULT=$?
if [ $DOWNLOAD_RESULT -eq 0 ] ; then
    :
else
	echo ""
    echo "Failed to download portal certificate #2."
    echo "Please contact your system administrator for help."
	echo ""
    exit
fi
#-----------------------------------------------------------------------------------------
# Download certificate #1. If it fails to download, exit. 
curl -O http://it.ironk12.org/files/certs/cert_Decryption_SSL.crt > /dev/null 2>&1
DOWNLOAD_RESULT=$?
if [ $DOWNLOAD_RESULT -eq 0 ] ; then
    :
else
	echo ""
    echo "Failed to download certificate #1."
    echo "Please contact your system administrator for help."
	echo ""
    exit
fi
#-----------------------------------------------------------------------------------------
# Download certificate #2. If it fails to download, exit. 
curl -O http://it.ironk12.org/files/certs/cert_SSL_Decryption.crt > /dev/null 2>&1
DOWNLOAD_RESULT=$?
if [ $DOWNLOAD_RESULT -eq 0 ] ; then
    :
else
	echo ""
    echo "Failed to download certificate #2."
    echo "Please contact your system administrator for help."
	echo ""
    exit
fi
#-----------------------------------------------------------------------------------------
# Install certificate and Portal certificate.  If it fails to install, exit.
echo " ___________________________________________________________________"
echo "| Welcome to the the ICSD SSL Certificate & Portal Certificate Installer.|"
echo "|___________________________________________________________________|"
security add-trusted-cert -d -r trustAsRoot -k /Library/Keychains/System.keychain cert_Captive-Portal.crt
PORTAL_INSTALL_RESULT=$?
security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert_Root-CA.crt
OTHER_PORTAL_INSTALL_RESULT=$?
security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert_Decryption_SSL.crt
CERT_INSTALL_RESULT=$?
security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert_SSL_Decryption.crt
OTHER_CERT_INSTALL_RESULT=$?
if [[ $PORTAL_INSTALL_RESULT -eq 0 && $CERT_INSTALL_RESULT -eq 0 && $OTHER_PORTAL_INSTALL_RESULT -eq 0 && $OTHER_CERT_INSTALL_RESULT -eq 0 ]] ; then
    :
else
	 echo ""
    echo "Failed to install certificates."
    echo "Please contact your system administrator for help."
	 echo ""
    exit
fi
#-----------------------------------------------------------------------------------------
# Cleanup
rm cert_Captive-Portal.crt
rm cert_SSL_Decryption.crt
rm cert_Root-CA.crt
rm cert_Decryption_SSL.crt
cd ..
rmdir $mytempdir
#-----------------------------------------------------------------------------------------
# Certificates installed correctly
echo ""
echo "Success! Certificates installed."
#-----------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------
#Terminate Installer
killall ICSDAllCertsandFirefoxInstaller
#-----------------------------------------------------------------------------------------

