
BINARY_OUTNAME=useradd-$$.zip

if [ -f /usr/bin/tput ]; then
  NORMAL=`tput sgr0 2> /dev/null`
  BOLD=`tput bold 2> /dev/null`
else
  NORMAL=""
  BOLD=""
fi
# usage
usage_single() {
  case "$2" in
    "adduser")
       echo "  ${BOLD}$1 --single <username> [<groupname>]${NORMAL}"
       echo "    generate a single user <username>"
       ;;
    "deluser")
       echo "  ${BOLD}$1 --single <username>${NORMAL}"
       echo "    delete a single user <username>"
       ;;
  esac
}

usage_range() {
   case "$2" in
    "adduser")
       echo "  ${BOLD}$1 --range <name_prefix> <start> <end> <passwd_opt> [<group>]${NORMAL}"
       echo "  generate a range of users from <name_prefix><start> to <name_prefix><end>"
       echo "  passwd_opt:"
       echo "    If one digit, it's the length of randomly created password."
       echo "    Other setting is the password itself."
       echo "  example:" 
       echo "    $1 --range student 2 5 8 g3c2"
       echo "    or" 
       echo "    $1 --range student 2 5 drblnice g3c2"
       ;;
    "deluser")
       echo "  ${BOLD}$1 --range name_prefix start_no end_no${NORMAL}"
       echo "  delete a range of users from <name_prefix><start_no> to <name_prefix><end_no>"
       echo "  example: "
       echo "    $1 --range student 2 5"
       ;;
  esac
}

usage_file() {
   case "$2" in
    "adduser")
       echo "  ${BOLD}$1 --file <filename>${NORMAL}"
       echo "    generate users that are listed in <filename>."
       echo 
       echo "    the format of the file <filename>:  PREFIX START END PASSWORD_OPT GROUP"
       echo "    for example: "
       echo "    # account for student"
       echo "    s89		101	129	 10 g3c9"
       echo "    # account for teacher"
       echo "    tckps	01	99  AGoodTeacher"
       echo
       echo "    if GROUP is not assigned, only the default group \"users\" will be assigned"
       ;;
    "deluser")
       echo "  ${BOLD}$1 --file <filename>${NORMAL}"
       echo "    delete users that are listed in <filename>."
       echo 
       echo "    the format of the file <filename>:  PREFIX START END"
       echo "    for example: "
       echo "    # account for student"
       echo "    s89		101	129"
       echo "    # account for teacher"
       echo "    tckps	01	99"
       ;;
  esac
}

usage_list() {
   case "$2" in
    "adduser")
      echo "  ${BOLD}$1 --list <filename>${NORMAL}"
      echo "    generate users that are listed in <filename>"
      echo 
      echo "    the format of the file <filename>: USERNAME PASSWORD_OPT GROUP"
      echo "    for example:"
      echo "    # account for goodman"
      echo "    blake  8  goodname"
      echo "    steven ilovesteven goodname"
      echo "    # account for normal users"
      echo "    drbl ivotedrbl"
      echo
      echo "    if GROUP is not assigned, only the default group \"users\" will be assigned"
 
      ;;
    "deluser")
      echo "  ${BOLD}$1 --list <filename>${NORMAL}"
      echo "    delete users that are listed in <filename>"
      echo 
      echo "    the format of the file <filename>: USERNAME"
      echo "    for example:"
      echo "    # account for goodman"
      echo "    blake"
      echo "    steven"
      echo "    # account for normal users"
      echo "    drbl"
      ;;
   esac 
}

usage_misc() {
  case "$2" in
    "adduser")
      ;;
    "deluser")
      echo "  ${BOLD}$1 --group <groupname>${NORMAL}"
      echo "    delete the user that belong to the group."
      echo "    if <groupname> is the default group (users), NO users will be deleted"
      ;;
  esac
}

usage_all() {
  usage_single "$0" "$mode"
  echo
  usage_range "$0" "$mode"
  echo
  usage_file "$0" "$mode"
  echo 
  usage_list "$0" "$mode"
  echo 
  usage_misc "$0" "$mode"
}

# mode
mode="adduser"
if [ "$(echo "$0" | grep "useradd")" != "" ]; then
  mode="adduser"
elif [ "$(echo "$0" | grep "userdel")" != "" ]; then
  mode="deluser"
fi

# $1 = ""
if [ "$1" = "" ]; then echo "Usage:"; usage_all "$0" "$mode"; exit; fi

prog=""
args=""

##
case "$1" in
  "--single"|"-s")
    if [ "$2" = "" ]; then echo "Usage:"; usage_single "$0" "$mode"; exit; fi
    prog="useradd/$mode.sh"
    args="--single $2 $3"
    ;;
  "--range"|"-r")
    if [ "$2" = "" -o "$3" = "" -o "$4" = "" ]; then echo "Usage:"; usage_range "$0" "$mode"; exit; fi
    case "$mode" in
      "adduser")
         prog="useradd/adduser.sh"
         args="$2 $3 $4 $5 $6"
         ;;
      "deluser")
         prog="useradd/deluser.sh"
         args="--range $2 $3 $4"
         ;;
    esac
    ;;
  "--file"|"-f")
    if [ "$2" = "" ]; then echo "Usage:"; usage_file "$0" "$mode"; exit; fi
    prog="useradd/$mode.sh"
    args="--file $2"
    ;;
  "--list"|"-l")
    if [ "$2" = "" ]; then echo "Usage:"; usage_list "$0" "$mode"; exit; fi
    prog="useradd/$mode.sh"
    args="--list $2" 
    ;;
esac

if [ "$mode" = "deluser" -a "$1" = "--group" -a "$2" != "" ]; then
  prog="useradd/deluser.sh"
  args="--group $2"
fi

if [ "$prog" = "" ]; then echo "Usage:"; usage_all "$0" "$mode"; exit; fi

## Blake, 2003/12/29
# add/delete single user
#if [ $# -gt 1 -a "$1" = "--single" ]; then
#  if [ $# -lt 2 -a "$mode" = "useradd" ]; then
#    echo "Usage: "
#    echo "  $0 --single <username>"
#    echo "    generate a single user <username>"
#    exit
#  elif [ $# -lt 2 -a "$mode" = "userdel" ]; then
#    echo "Usage: "
#    echo "  $0 --single <username>"
#    echo "    delete a single user <username>"
#    exit
#  else
#    if [ "$mode" = "userdel" ]; then
#      prog="useradd/deluser.sh"
#      args="--single $2"
#    else
#      prog="useradd/adduser.sh"
#  	  args="--single $2"
#    fi
#  fi  
#fi
#
## add a range of users
#if [ $# -gt 1 -a "$1" = "--range" ]; then
#  if [ $# -lt 4 -a "$mode" = "useradd" ]; then
#    echo "Usage: "
#    echo "  $0 --range <prefix> <start> <end> [<length_of_password>]"
#    echo "    generate a range of users from <prefix><start> to <prefix><end>,"
#    echo "    if <length_of_passwd> is assigned,"
#    ehco "    the username/password pairs are listed in useradd.gen."
#    echo "    Otherwise, the password is the same as the username"
#    exit
#  elif [ $# -lt 4 -a "$mode" = "userdel" ]; then
#    echo "Usage: "
#    echo " $0 --range <prefix> <start> <end>"
#    echo "    delete a range of users from <prefix><start> to <prefix><end>,"
#    exit
#  else
#    if [ "$mode" = "userdel" ]; then
#      prog="useradd/deluser.sh"
#      args="--range $2 $3 $4 $5"
#    else
#      prog="useradd/adduser.sh"
#  	  args="$2 $3 $4 $5 $6"
#    fi
#  fi  
#fi
#
## add users which are listed in file
#if [ $# -gt 1 -a "$1" = "--file" ]; then
#  if [ $# -lt 3 -a "$mode" = "useradd" ]; then
#    echo "Usage: "
#    echo "  $0 --file <filename> <length_of_password>"
#    echo "    generate users that are listed in <filename>."
#    echo "    the username/password pairs are listed in useradd.gen"
#    echo 
#    echo "    the format of the file <filename>"
#    echo "       PREFIX START END"
#    echo "    for example: "
#    echo "    # account for student"
#    echo "    s	89101	89129"
#    echo "    # account for teacher"
#    echo "    tckps	01	99"
#    exit
#  elif [ $# -lt 2 -a "$mode" = "userdel" ]; then
#    echo "Usage: "
#    echo "  $0 --file <filename>"
#    echo "    delete users that are listed in <filename>."
#    echo "    the filename is the one that is generated by drbl-useradd"
#    exit
#  else 
#    if [ "$mode" = "userdel" ]; then
#      prog="useradd/deluser.sh"
#      args="--file $2 $3"
#    else
#      prog="useradd/adduser.pl"
#	  args="$2 $3"
#    fi
#  fi  
#fi

# install some packages before installation
apt-get -y install zip unzip expect

outname=$BINARY_OUTNAME
trap 'rm -f $outname; rm -rf useradd; exit 1' HUP INT QUIT TERM
#echo "Unpacking..."
tail +$BINARY_LINENO $0 > $outname
#if [ -x /usr/bin/sum ]; then
#    echo "Checksumming..."
#
#    sum=`/usr/bin/sum $outname`
#    index=1
#    for s in $sum
#    do
#    case $index in
#    1)  sum1=$s;
#        index=2;
#        ;;
#    2)  sum2=$s;
#        index=3;
#        ;;
#    esac
#    done
#    if expr $sum1 != $BINARY_BSD_CHECKSUM1 ||
#       expr $sum2 != $BINARY_BSD_CHECKSUM2 ; then
#      echo "The download file appears to be corrupted."
#      exit 1
#    fi
#else 
#    echo "Can't find /usr/bin/sum to do checksum. Continuing anyway."
#fi

#echo "Extracting..."

if [ -x /usr/bin/unzip ]; then
  /usr/bin/unzip $outname > /dev/null
fi

#echo "Setuping..."

eval "$prog $args"

cpwd=`pwd`
cd /var/yp/
make > /dev/null
cd $cpwd

echo "Done."
rm -f $outname
rm -rf useradd
exit 0
