#!/bin/sh
#
# Mega simple wrapper for batch control on nodes.
#
# Rick van der Zwet <rick@wirelessleiden.nl>
#

HOSTS=${*:-"`$(dirname $0)/gformat.py list up systems fqdn`"}
PREFIX=${PREFIX:-'ip'}
CMD=${CMD:-'md5 /usr/local/etc/rc.d/lvrouted'}

echo "# WARN: Going to run at: " $HOSTS; sleep 2
scp_files=""
concurrent=true

##
## Network differences
##
#cat << 'EOF' >/tmp/command
#sh -c ". /etc/rc.subr; load_rc_config "networking"; set" > /tmp/networking-env
#grep -e ^ifconfig -e ^ipv4 /tmp/networking-env
#{ grep -e ^ipv4 /tmp/networking-env | cut -d= -f2 | tr -d \' | xargs -n1 | cut -d/ -f1;
#  grep -e ^ifconfig /tmp/networking-env | grep -v -i 'dhcp' | cut -d= -f2 | tr -d \' | cut -d' ' -f2 | cut -d'/' -f1
#} | sort -u > /tmp/config
#ifconfig -a | grep 'inet ' | cut -d' ' -f2 | sort > /tmp/current
#echo "# Comparing IP differences /tmp/current vs /tmp/config"
#diff -u /tmp/current /tmp/config
#EOF

##
## Migrate SSH authorized_keys symlink to new location.
##
#cat <<'EOF' > /tmp/command
#mount -uwo noatime / && unlink /root/.ssh &&  mkdir /root/.ssh && ln -s /etc/ssh/authorized_keys /root/.ssh/ && mount -ur 
#EOF

##
## View lvrouted configured flags and processes
##
#cat <<'EOF' > /tmp/command
#/usr/local/etc/rc.d/lvrouted rcvar | grep flags
#pgrep -fl lvrouted
#EOF

##
## Restart lvrouted
##
#cat <<'EOF' > /tmp/command
#sleep 30
#nohup /usr/local/etc/rc.d/lvrouted restart
#EOF

##
## Sync some files
##
#scp_files="/tmp/wl-config /tmp/nameserver-shuffle"
#cat <<'EOF' > /tmp/command
#mount -uwo noatime / || exit 1
#trap "mount -ur /; exit 1" 1 2 3 15
#trap "mount -ur /; exit 0" 0
#
#cp -v /tmp/wl-config /tools || exit 1
#cp -v /tmp/nameserver-shuffle /tools || exit 1
#
#mount -ur / || exit 1
#/tools/wl-config -b || exit 1
#/tools/nameserver-shuffle || exit 1
#EOF

##
## Run /tools/wl-config with random scheduler to avoid gold-rush and killing the
## config generator server
##
#cat <<'EOF' > /tmp/command
#sleep `expr $$ % 300`
#/tools/wl-config -b || exit 1
#/tools/nameserver-shuffle || exit 1
#EOF

##
## Detect routing loops and find out the actual default route
##
## Process output with:
## a) Proxy per host:
##   for F in ip-*.txt; do printf "%-20s : %s\n" `echo "$F" | awk -F'[-.]' '{print $2}'` `grep -v '*' $F | tail -1 | awk '{print $2}'`; done
## b) Count per proxy:
##   for F in ip-*.txt; do printf "%-20s : %s\n" `echo "$F" | awk -F'[-.]' '{print $2}'` `grep -v '*' $F | tail -1 | awk '{print $2}'`; done | awk '{print $3}' | sed 's/^2[a-z0-9-]*\.//g' | sort | uniq -c
##
#cat << 'EOF' > /tmp/command
#traceroute -m 15 -w 1 -q 1 -I rvdzwet.nl
#EOF

# Forcefully updating all the SOA records.
cat << 'EOF' > /tmp/command
rndc refresh wleiden.net
rndc refresh 16.in-addr.arpa
rndc refresh 17.in-addr.arpa
rndc refresh 18.in-addr.arpa
rndc refresh 19.in-addr.arpa
rndc refresh 20.in-addr.arpa
rndc refresh 21.in-addr.arpa
rndc refresh 22.in-addr.arpa
rndc refresh 23.in-addr.arpa
rndc refresh 24.in-addr.arpa
rndc refresh 25.in-addr.arpa
rndc refresh 26.in-addr.arpa
rndc refresh 27.in-addr.arpa
rndc refresh 28.in-addr.arpa
rndc refresh 29.in-addr.arpa
rndc refresh 30.in-addr.arpa
rndc refresh 31.in-addr.arpa
EOF


# Cleanup old entries
rm $PREFIX-* stderr-*

for HOST in $HOSTS; do
  echo "# Working on $HOST"
  stdout_file="$PREFIX-${HOST%%.}.txt"
  stderr_file="stderr-${HOST%%.}.txt"
  : > $stdout_file
  : > $stderr_file
  if $concurrent; then
    {
      if [ -n "$scp_files" ]; then
         scp -o ConnectTimeout=3 -o BatchMode=yes $scp_files root@$HOST:/tmp || exit 1
      fi
      cat /tmp/command | ssh -o ConnectTimeout=3 -o BatchMode=yes root@$HOST 'cat >/tmp/command && sh /tmp/command'
    } 1>$stdout_file 2>$stderr_file &
  else
    if [ -n "$scp_files" ]; then
       scp -o ConnectTimeout=3 -o BatchMode=yes $scp_files root@$HOST:/tmp || exit 1
    fi
    cat /tmp/command | ssh -o ConnectTimeout=3 -o BatchMode=yes root@$HOST 'cat >/tmp/command && sh /tmp/command'
  fi
done

echo "# Wait for all processes to complete."
wait
