#!/bin/sh

if [ $# -ne 3 ]; then
    echo "Usage: setup-ipsec-tunnel <name> <remote net> <remote ip>"
    exit 1
fi

ipsecadm=/usr/local/bin/ipsecadm

name=$1
remotenet=$2
remote=$3

local_int=192.168.1.1
local_ext=201.202.203.204

# Try to remove the tunnel first in case it already exists
$ipsecadm tunnel del $name > /dev/null 2>&1

# Then create the new tunnel
$ipsecadm tunnel add $name --local=$local_ext --remote=$remote

# Configure the interface
ifconfig $name $local_int up

# Add a route to the remote network
route add -net $remotenet dev $name
