#! /bin/sh
# wakeup script
#
# Copyright (C) 2007 Kevin Cody Jr. <kcody@vegaresearch.com>
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

# only applicable when ipsec is running
[ -e /var/run/pluto.pid ] || exit

# loop through any erouted tunnels in the HOLD state
for f in  `ipsec status | awk '/erouted HOLD/{ print $2 }' | cut -f1 -d\: | cut -f2 -d\"` ; do

        # only whack if no pending events at all exists
        ipsec status | grep STATE | grep -q $f ||
                ipsec whack --name $f --initiate --asynchronous

done

# loop through any tunnels that don't quite exist
for f in `ipsec status | awk '/prospective erouted/{ print $2 }' | cut -f1 -d: | grep -v \# | cut -f2 -d\"` ; do

        ipsec status | grep STATE_QUICK | grep -q $f || {
                ipsec status | grep STATE_MAIN | grep -q $f && ipsec down $f
                ipsec up $f
        }

done

