dns/scripts/virtual.sh

57 lines
1.4 KiB
Bash
Executable File

#!/bin/bash -e
##########################################################################
# where am I ?
SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd)"
DYNAMICPATH="$(dirname "$SCRIPTPATH")/dynamic"
##########################################################################
name="$1"
ipv6="$2"
ipv4="$3"
if [ -z "$name" ] || [ -z "$ipv6" ]
then
echo "Usage: $0 name ipv6 <ipv4>"
exit 1
fi
# strip .burble.dn42 from name if it was included
name=${name%.burble.dn42}
##########################################################################
# the IPv4 arg is optional
if [ -z "$ipv4" ]
then
echo "Updating DNS: $name -> $ipv6"
line=$(printf " [ '%s', '%s' ]," "$name" "$ipv6" )
else
echo "Updating DNS: $name -> $ipv6 / $ipv4"
line=$(printf " [ '%s', '%s', '%s' ]," "$name" "$ipv6" "$ipv4")
fi
##########################################################################
pushd "$DYNAMICPATH" >/dev/null
# don't update if the line already exists
if grep -Fx "$line" virtual.js >/dev/null 2>&1
then
echo "DNS: Already exists, no change"
exit 0
fi
# remove any existing lines
sed -i -e "/^ \[ '$name',/d" virtual.js
# add a new line for this host
sed -i -e "s!^ // insert here! // insert here\n${line}!" virtual.js
# commit
git commit -a -m "Update $name"
popd > /dev/null
##########################################################################
# end of file