#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#   a wrapper of drbl-host-cp, drbl-user-cp

# 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

drbl_user_cp=`which drbl-user-cp`
drbl_host_cp=`which drbl-host-cp`
if [ "$drbl_user_cp" = "" ]; then drbl_user_cp="/opt/drbl/sbin/drbl-user-cp"; fi
if [ "$drbl_host_cp" = "" ]; then drbl_host_cp="/opt/drbl/sbin/drbl-host-cp"; fi

if [ ! -f $drbl_user_cp -o ! -f $drbl_host_cp ]; then
  echo "DRBL installation incomplete.. Abort.."
  exit 
fi

usage() {
  echo "Usage:"
  echo "  ${BOLD}$0 SOURCE DEST${NORMAL}"
  echo
  echo "Example:"
  echo "  If you would like to copy the file ./ABCD to all users' public_html/hw1.html,"
  echo "  use the command:"
  echo "  ${BOLD}# $0 ./ABCD [:ALL_USERS:]/public_html/hw1.html${NORMAL}"
  echo
  echo "  On the other hand,"
  echo "  If you would like to collect all the file public_html/hw1.html"
  echo "  from all users to ABCD/ at your home directory,"
  echo "  use the command:"
  echo "  ${BOLD}# $0 [:ALL_USERS:]/public_html/hw1.html ABCD${NORMAL}"
  echo
  echo "  Use ${BOLD}[:ALL_HOSTS:]/${NORMAL} instead of ${BOLD}[:ALL_USERS:]/${NORMAL} means" 
  echo "  copying/collecting to/from all DRBL clients"
  echo
  echo "  Use ${BOLD}[:GROUP_XXX:]/${NORMAL} instead of ${BOLD}[:ALL_USERS:]/${NORMAL} means"
  echo "  only the users of the group XXX choosen to copy/collect"
  exit 0
}

#if [ $# -lt 2 ]; then usage; fi

group=""
mode=""
cmd=""
cmdopt=""
src=$1
dest=$2

if [ "$(echo "$src" | grep -e "^\[:ALL_USERS:\]/")" != "" ]; then
  mode="get"; cmd="$drbl_user_cp";
  src=${src/[:ALL_USERS:/}; src=${src/]/}

elif [ "$(echo "$src" | grep -e "^\[:ALL_HOSTS:\]/")" != "" ]; then
  mode="get"; cmd="$drbl_host_cp"
  src=${src/[:ALL_HOSTS:/}; src=${src/]/} 

elif [ "$(echo "$src" | grep -e "^\[:GROUP_[^:]*:\]/")" != "" ]; then
  mode="get";
  cmd="$drbl_user_cp"
  cmdopt=$src; cmdopt=${cmdopt%]*}; cmdopt=${cmdopt/[:GROUP_/}; 
  cmdopt=${cmdopt%:*}
  src=${src/[:GROUP_$cmdopt:/}; src=${src/]/}

elif [ "$(echo "$src" | grep -e "^\[:[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:\]")" ]; then
  host=${src%]*}
  host=${host/[/}
  src=${src/$host/}
  src=${src/[/}
  src=${src/]/}
  if [ ! -e /var/lib/diskless/default/$host ]; then 
    echo "No such client..Abort"; exit; 
  fi
  if [ "$src" != "" ]; then
    cp -a /var/lib/diskless/default/$host/$src $dest
  fi
  exit

elif [ "$(echo "$dest" | grep -e "^\[:ALL_USERS:\]/")" != "" ]; then
  mode="put"; cmd="$drbl_user_cp"
  dest=${dest/[:ALL_USERS:/}; dest=${dest/]/} 

elif [ "$(echo "$dest" | grep -e "^\[:ALL_HOSTS:\]/")" != "" ]; then
  mode="put"; cmd="$drbl_host_cp"
  dest=${dest/[:ALL_HOSTS:/}; dest=${dest/]/} 

elif [ "$(echo "$dest" | grep -e "^\[:GROUP_[^:]*:\]/")" != "" ]; then
  mode="put";
  cmd="$drbl_user_cp"
  cmdopt=$dest; cmdopt=${cmdopt%]*}; cmdopt=${cmdopt/[:GROUP_/}
  cmdopt=${cmdopt%:*}
  dest=${dest/[:GROUP_$cmdopt:/}; dest=${dest/]/}

elif [ "$(echo "$dest" | grep -e "^\[:[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:\]")" ]; then
  host=${dest%]*}
  host=${host/[/}
  dest=${dest/$host/}
  dest=${dest/[/}
  dest=${dest/]/}
  if [ ! -e /var/lib/diskless/default/$host ]; then 
    echo "No such client..Abort"; exit; 
  fi
  if [ "$dest" != "" -a -e $src ]; then
    cp -a $src /var/lib/diskless/default/$host/$dest
  fi
  exit
fi

if [ "$mode" = "" -o "$src" = "" ]; then 
  #echo "[$mode][$src][$dest]"; exit
  usage
fi
if [ "$cmdopt" != "" ]; then cmdopt="-g $cmdopt"; fi

case "$mode" in
  "put")
    if [ "$dest" = "" ]; then dest="."; fi

    if [ "$(echo "$src" | grep -e "^\[:ALL_USERS:\]/")" != "" -o \
         "$(echo "$src" | grep -e "^\[:ALL_HOSTS:\]/")" != "" -o \
         "$(echo "$src" | grep -e "^\[:GROUP_[^:]*:\]")" != "" ]; then usage; fi
    echo "$cmd $cmdopt $src $dest"
    eval "$cmd $cmdopt $src $dest"
    ;;
  "get")
    if [ "$dest" = "" ]; then usage; fi    

    if [ "$(echo "$dest" | grep -e "^\[:ALL_USERS:\]/")" != "" -o \
         "$(echo "$dest" | grep -e "^\[:ALL_HOSTS:\]/")" != "" -o \
         "$(echo "$dest" | grep -e "^\[:GROUP_[^:]*:\]")" != "" ]; then usage; fi
    echo "$cmd $cmdopt -c $src $dest"
    eval "$cmd $cmdopt -c $src $dest"
    ;;
esac
