cArgs=0
help="install or remove \nUse this to install or remove the CA root certificate\n$0 -c <command> install, remove> -n <name> certificate file name with .crt.pem"
pid=
index=
while getopts ":h:c:n:" opt; do
  case $opt in
    h)
	    echo -e $help
	    exit 1
    ;;
    c)
    	command=$OPTARG
      	let cArgs++
     ;;
    n)
    	name=$OPTARG
      	let cArgs++
     ;;
    \?)
		echo "opt="$opt
		echo -e $help
		exit
	;;
    :)
		if [ $OPTARG = 'h' ]
		then
			echo -e $help
			exit
		else
	    	echo "Option -$OPTARG requires an argument. $opt"
	    	exit 1
    	fi
    ;;
  esac
done
if [ $cArgs -lt 2 ]
then
	echo -e $help
	exit
fi


# to see what's in the certiciate uncomment the following
#openssl x509 -text < root.crt

#obtain .pem from crt

case "$OSTYPE" in
  	darwin*)
        #os="OSX"
        case $command in
            install)
                # install the ca cert in keychain on Mac
                sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain root/ca/$name.crt.pem
            ;;
            remove)
                #remove certficate from keychain
                read -p "Please give the domain name for the CA to delete(if you don't know, you can give localhost?" domain
                sudo security delete-certificate -c $domain
            ;;
        esac
   ;;
  	linux*)
        #os="LINUX"
        case $command in
            install)
                sudo cp root/ca/$name.crt.pem /usr/local/share/ca-certificates/$name.crt.pem
                sudo update-ca-certificates
            ;;
            remove)
                #remove certficate from keychain
                sudo rm /usr/local/share/ca-certificates/$name.crt.pem
                sudo update-ca-certificates --fresh
            ;;
        esac
    ;;
esac
