#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#   send the comand to all drbl clients
#

drblroot="/var/lib/diskless/default"

## --help -h
if [ "$1" = "-h" -o "$1" = "--help" ]; then
  echo "Usage:"
  echo "  $0 --wol                   use WOL to wake up all drbl clients"
  echo "  $0 [-u user] [-b] command  execute command on all drbl clients"
  exit 0
fi

## Wake on LAN (--wol)
if [ "$1" = "--wol" ]; then
  trap 'rm -f doit.zip; rm -rf doit; exit 1' HUP INT QUIT TERM
  tail +${LINENO} $0 > doit.zip
  unzip doit.zip
  broadcast_address="255.255.255.255"
  while read line
  do
    if [ "$(echo "$line" | grep "option broadcast-address")" != "" ]; then
      broadcast_address=$(echo "$line" | cut -d\; -f1 | awk '{ print $3; }')
    elif [ "$(echo "$line" | grep "hardware ethernet")" != "" ]; then
      ethernet_address=$(echo "$line" | cut -d\; -f1 | awk '{ print $3; }')
      echo "== $ethernet_address =="
      doit/wakeonlan -i $broadcast_address $ethernet_address
    fi
  done < /etc/dhcp3/dhcpd.conf
  rm -rf doit
  rm -rf doit.zip
  exit 0
fi

##
resshkeygen=0
if [ ! -f $HOME/.ssh/id_rsa ]; then
  trap 'rm -f doit.zip; rm -rf doit; exit 1' HUP INT QUIT TERM
  tail +${LINENO} $0 > doit.zip
  unzip doit.zip
  doit/autosshkeygen $USER
  mv $HOME/.ssh/id_rsa.pub $HOME/.ssh/authorized_keys
  rm -rf doit 
  rm -f doit.zip
  resshkeygen=1
fi

# check if the user is root
id=`id -u`
if [ "$id" == "0" ]; then
  for host in `ls $drblroot`
  do
    if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then
      continue
    fi
    if [ $resshkeygen -eq 1 -o ! -f $drblroot/$host/root/.ssh/id_rsa ]; then
      mkdir -p $drblroot/$host/root/.ssh
      cp $HOME/.ssh/id_rsa $drblroot/$host/root/.ssh/id_rsa
      cp $HOME/.ssh/authorized_keys $drblroot/$host/root/.ssh/authorized_keys
    fi
  done
fi

## -u || --user
RUSER=$USER
if [ "$1" = "-u" -o "$1" = "--user" ]; then
  shift
  if [ "$1" = "" ]; then
    echo "Usage: $0 [-u|--user] username command"
    exit
  else
    RUSER="$1"
    shift
  fi
fi

## -b || --batch
batch_mode=0
if [ "$1" = "-b" -o "$1" = "--batch" ]; then
  shift
  batch_mode=1
fi

# 2003/08/15 before execute the command, rm ~/.ssh/known_hosts
rm -f $HOME/.ssh/known_hosts

# execute the command
for host in `ls $drblroot`
do
  if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then
    continue
  fi
  # check if $host is myself
  me=`/sbin/ifconfig | grep "inet addr:$host" | cut -d: -f2 | cut -d' ' -f1 | grep "^$host$"`
  if [ "$me" = "" -o "$me" != "$host" ]; then
    echo "=== $host ==="
    if [ $batch_mode -eq 0 ]; then
      ## interactive mode, used for status check
      if /bin/ping -c 2 $host > /dev/null 2>&1; then
        if [ $# -lt 1 ]; then
          echo "$host is alive.."
        else
          ssh -o StrictHostKeyChecking=no $RUSER@$host $@
        fi
      else
        echo "$host is unreachable.." 
      fi
    else
      ## batch mode, used for sending command 
      ssh -o StrictHostKeyChecking=no $RUSER@$host $@ &
    fi
  fi
done
exit 0
