#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#  remove the file on all users' home directory

# 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

usage() {
  echo "Usage: $0 [-g group] file_on_all_users_home_dir_to_remove"
  exit 0
}

if [ $# -lt 1 ]; then usage; fi
if [ "$1" = "-g" ]; then
  group=$2
  shift;shift
fi
dest=$1
if [ "$dest" = "" ]; then usage; fi

LC_ALL=C
TMP=`mktemp drbl-user.XXXXXX`
ls -l /home > $TMP
while read x x usr grp x 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
  rm -rf /home/$dir/$dest
done < $TMP
rm -f $TMP
