#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: put/get the file to/from all user

# 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 [-g group] src_file dest_on_all_users${NORMAL} [-m MODE]"
  echo "  ${BOLD}$0 [-g group] -c file_on_all_usrs dest_dir${NORMAL}"
  echo "  ${BOLD}$0 [-g group] -d dir_that_contains_file_on_all_users dest_on_all_users${NORMAL}" 
  echo "  ${BOLD}$0 -h${NORMAL}"
  echo
  echo "Example: "
  echo "  If you would like to copy the file ./sample.html to all users'"
  echo "  public_html/ subdirectory, use this command:"
  echo "  ${BOLD}# $0 ./ABCD public_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 hw1/ at your home directory,"
  echo "  use this command:"
  echo "  ${BOLD}# $0 -c public_html/hw1.html hw1${NORMAL}"
  echo
  echo "  You can modify each file in hw1/ and copy to each user"
  echo "  using this command:"
  echo "  ${BOLD}# $0 -d hw1 public_html/${NORMAL}"
  echo 
  echo "  If only the users of the group 'ckps83' choosed to be processed:"
  echo "  ${BOLD}# $0 -g ckps83 ./ABCD public_html/${NORMAL}"
  echo "  ${BOLD}# $0 -g ckps83 -c public_html/hw1.html hw1${NORMAL}"
  echo "  ${BOLD}# $0 -g ckps83 -d hw1 public_html/${NORMAL}"
  exit 0
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then usage; fi
if [ $# -lt 2 ]; then usage; fi
if [ "$1" = "-g" -a "$2" != "" ]; then
  group=$2
  shift; shift
fi
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

LC_ALL=C
TMP=`mktemp drbl-user.XXXXXX`
ls -l --time-style=long-iso /home > $TMP
while read x x usr grp x x x dir; do
  if [ "$dir" = "" -o "$dir" = "partimag" ]; then continue; fi
  if [ "$group" != "" ]; then
    group_users=$(grep -e "^$group:" /etc/group | cut -d: -f4)
    if [ "$(echo "$group_users" | grep "$usr")" = "" ]; then continue; fi
  fi
  case "$mode" in
    "put")
      if [ "$src" != "/home/$dir/$dest" ]; then
        # destpath
        destpath=${dest%/*}
        if [ "$destpath" != "$dest" ]; then
          mkdir -p /home/$dir/$destpath
          if [ "$mopt" = "-m" -a "$mval" != "" ]; then 
            chmod $mval /home/$dir/$dest
          fi
        fi
        if [ "$(echo "$dest" | grep -e "/$" )" != "" ]; then
          mkdir -p /home/$dir/$dest
          if [ "$mopt" = "-m" -a "$mval" != "" ]; then 
            chmod $mval /home/$dir/$dest
          fi 
        fi

        cp -a $src /home/$dir/$dest
        chown -R $usr.$grp /home/$dir/$dest
        if [ "$mopt" = "-m" -a "$mval" != "" ]; then 
          chmod -R $mval /home/$dir/$dest
        fi
      fi
      ;;
    "get")
      mkdir -p $dest
      cp -a /home/$dir/$src $dest/$usr
      ;;
    "dispatch")
      if [ -f $src/$usr ]; then
        if [ -d /home/$dir/$dest ]; then
          cp -a $src/$usr /home/$dir/$dest/$src
          chown $usr.$grp /home/$dir/$dest/$src
        else
          cp -a $src/$usr /home/$dir/$dest
          chown $usr.$grp /home/$dir/$dest
        fi
      fi
      ;;
  esac
done < $TMP
rm -f $TMP
