#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#  copy file to all clients
#  2003/12/31 collect the files on all clients

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then
  echo "You must be root to use this script."
  echo "Use sudo to enable it for users."
  exit 1
fi

if [ -f /usr/bin/tput ]; then
  NORMAL=`tput sgr0 2> /dev/null`
  BOLD=`tput bold 2> /dev/null`
else
  NORMAL=""
  BOLD=""
fi

usage() {
  echo "Usage:"
  echo "  ${BOLD}$0 src_file dest_on_all_clients${NORMAL} [-m MODE]"
  echo "  ${BOLD}$0 -c file_on_all_clients dest_dir${NORMAL}"
  echo "  ${BOLD}$0 -d dir_that_contains_file_on_all_clients dest_on_all_clients${NORMAL}"
  echo "  ${BOLD}$0 -h${NORMAL}"
  echo 
  echo "Example: "
  echo "  If you would like to copy the file ./ABCD to /etc on all DRBL clients"
  echo "  use this command:" 
  echo "  ${BOLD}# $0 ./ABCD /etc${NORMAL}"
  echo
  echo "  On the other hand,"
  echo "  if you would like to collect all the file /etc/ABCD"
  echo "  from all DRBL clients to ABCD/ at your home directory,"
  echo "  use this command:"
  echo "  ${BOLD}# $0 -c /etc/ABCD ABCD${NORMAL}"
  echo
  echo "  You can modify each file in ABCD/ and then copy to each host"
  echo "  using this command:"
  echo "  ${BOLD}# $0 -d ABCD /etc"

  exit 0

}

if [ "$1" = "-h" -o "$1" = "--help" ]; then usage; fi
if [ $# -lt 2 ]; then usage; fi

## Blake, 2003/12/10, remove the option "push" from drbl-get
#if [ -f /opt/drbl/bin/drbl-get ]; then
#  /opt/drbl/bin/drbl-get push $1 $2
#else
#  ## assume drbl-get in $PATH
#  drbl-get push $1 $2
#fi

drblhost="/var/lib/diskless/default"
mode="put"
src=$1
dest=$2
mopt=$3
mval=$4
if [ "$src" = "-c" ]; then
  mode="get"
  src=$2
  dest=$3
elif [ "$src" = "-d" ]; then
  mode="dispatch"
  src=$2
  dest=$3
fi

if [ "$src" = "" -o "$dest" = "" ]; then usage; fi
if [ "$mode" = "dispatch" -a ! -d $src ]; then usage; fi

# Blake,2003/12/31, two modes: "get" and "put"
case "$mode" in
  "put")
    destpath=${dest%/*}

    if [ "$(echo "$dest" | grep -e "^/etc")" = "" -a \
         "$(echo "$dest" | grep -e "^/root")" = "" -a \
         "$(echo "$dest" | grep -e "^/var")" = "" ]; then

      mkdir -p $drblhost/root/$destpath
      cp -a $src $drblhost/root/$dest 

    else

      for host in `ls $drblhost 2> /dev/null`
      do
        if [ "$src" != "$host/$dest" ]; then
          ## destpath
          if [ "$destpath" != "$dest" ]; then
            mkdir -p $drblhost/$host/$destpath
            if [ "$mopt" = "-m" -a "$mval" != "" ]; then 
              chmod $mval /home/$dir/$dest
            fi
          fi
 
          cp -a $src $drblhost/$host/$dest
          if [ "$mopt" = "-m" -a "$mval" != "" ]; then
            chmod -R $mval $drblhost/$host/$dest
          fi
        fi
      done
    fi
    ;;
  "get")
    mkdir -p $dest
    for host in `ls $drblhost 2> /dev/null`
    do
      if [ "$host" != "root" ]; then
        cp -a $drblhost/$host/$src $dest/$host
      fi
    done
    ;;
  "dispatch")
    for host in `ls $src 2> /dev/null`
    do
      if [ -d $drblhost/$host ]; then
        if [ -d $drblhost/$host/$dest ]; then
          cp -a $src/$host $drblhost/$host/$dest/$src
        else
          cp -a $src/$host $drblhost/$host/$dest
        fi
      fi
    done
    ;;
esac
