#!/bin/bash

service perl-iptdaemon restart
EXIT_CODE=`echo $?`

echo "EXIT_CODE: $EXIT_CODE"

if [[ $EXIT_CODE == 10 ]]; then

        pids=`ps aux | grep "/usr/bin/iptdaemon.pl" | grep -v "grep" | awk '{ print $2 }'`
        echo "pids: $pids"

        echo "exit code is 10 we must fix it"
        /sbin/iptables -t nat -F

        for pid in $pids; do
                echo "we kill PID: $pid now to fix iptdaemon!"
                kill -9 $pid
        done

        service openvpn stop ; sleep 10 ; service openvpn start

        service perl-iptdaemon restart
        EXIT_CODE=`echo $?`

        sleep 3
        less /var/log/messages

else
        echo "seems all good with iptdaemon!"
fi
