#!/bin/bash 

set -e

if [ -f /var/lib/openvpn-server/ca-store ]
then
  /usr/lib/openvpn-server/ovs-commands/mount-ca-store
    if [ $? = 1 ]
    then
            echo "ca-store mount failed"
            exit 1
    fi

fi

. /usr/share/openvpn-server/functions.sh
. /etc/openvpn-server/config.sh


cd /etc/openvpn/

if [ ! -d "$CCDREVOKEDDIR" ]; then
	mkdir "$CCDREVOKEDDIR"
fi

echo "Beginning ccd clean up..." 

for f in `ls "$CCDDIR"` ; do

	if grep -q "^R.*$f.*" /var/lib/openvpn-server/openssl/index.txt ; then 
		echo "$f moved"
		mv "$CCDDIR/$f" "$CCDREVOKEDDIR"
	fi 

done

echo "You can find moved files in $CCDREVOKEDDIR"
echo "" 

wtf=false
echo "Checking for ccd's without a valid key..."

for f in `ls "$CCDDIR"` ; do

	if ! grep -q "^V.*$f.*" /var/lib/openvpn-server/openssl/index.txt ; then 
		wtf=true
		echo "$f"
	fi 

done

if [ "$wtf" = true ] ; then
	echo "..wtfomgbbq!?! Son you've got a serious problem, the ccd file(s) above don't have a valid key nor are they revoked. Get it sorted will ya." 
else
	echo "...all good"

    create_shorewall_vars
    prompt_restart_shorewall

fi



/usr/lib/openvpn-server/ovs-commands/umount-ca-store


