#!/bin/csh -f

# Simple script for adding users, coordinating UIDs.

# daw; 9/15/16

switch (`uname -s``uname -r`)
case Linux[234]*:
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

set ARGS = "$argv"

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG = /bin/echo
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	default:
	    if ("$1" == "sudo") then
		set SUDO = "WHEEL=1"
		shift
		breaksw
	    endif
	    if (! $?U2ADD) then
		set U2ADD = $1
		shift
		breaksw
	    endif
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

set DQ = '"'

set path = ( /bin /usr/bin /usr/sbin /sbin )

if (! $?SUDO) set SUDO
if (! $?DEBUG) set DEBUG

if (! $?U2ADD) then
    echo ""
    echo usage: $0 "netid [sudo]"
    echo "  where ${DQ}netid$DQ is the netid of the user to add"
    echo "    and ${DQ}sudo$DQ is specified if the user is to have sudo privileges"
    echo ""
    exit
endif

if (`/usr/bin/whoami` != "root") then
    echo "   " You must be root to run this
    exit 1
endif

set BASENAME = /usr/bin/basename
if (! -e $BASENAME) set BASENAME = /bin/basename
if (! -e $BASENAME) exec echo where is basename\?
set BASE = `$BASENAME $0`
set TMP = /tmp/$BASE.$$

if ($?VERBOSE) echo `date +%T` Collecting info for $U2ADD
/usr/bin/curl -s -m 60 -o $TMP "https://services.cs.rutgers.edu/cgi-bin/showUID?NetID=$U2ADD&useradd=2"
set STATUS = $status
if ($STATUS) then
    echo Collecting info for $U2ADD exited with status $STATUS
    exit $STATUS
endif

grep -w useradd $TMP > /dev/null
set UASTATUS = $status
egrep -w "allusers|users" $TMP > /dev/null
set AUSTATUS = $status
if ($UASTATUS || $AUSTATUS) then
    /bin/sed 's;.;    &;' $TMP
    echo ""
    exit 1
endif

set USERADD = useradd
which $USERADD | grep -v ":" > /dev/null
if ($status) set USERADD = user-add
which $USERADD | grep -v ":" > /dev/null
if ($status) exec echo Where is useradd\?

set ALLUSERS = allusers
grep "${ALLUSERS}:" /etc/group > /dev/null
if ($status) set ALLUSERS = users
grep "${ALLUSERS}:" /etc/group > /dev/null
if ($status) echo What users group should I use\?

# determine privileged groups

set FILES = /etc/sudoers
set IFILES = `grep "^#include[ 	]" /etc/sudoers | awk '{print $2}'`
set FILES = ( $FILES $IFILES )
set IDIR = `grep "^#includedir[ 	]" /etc/sudoers | awk '{print $2}'`
if ("$IDIR" != "") then
    /bin/ls $IDIR | grep . > /dev/null
    if (! $status) then
	set FILES = ( $FILES $IDIR/* )
    endif
endif

# determine which is most used
@ PGMEMBERS = -1
set PGROUPS = `sed 's;#.*;;' $FILES | egrep "%.*ALL.*ALL.*ALL|%.*ALL.*root.*ALL|%.*ALL.*ALL.*ALL" | awk '{print $1}' | sed 's;%;;' | sort -u`

	if ("$DEBUG" != "") echo PGROUPS = $PGROUPS
foreach PGROUP ( $PGROUPS )
	@ PGCOUNT = `grep ^${PGROUP}: /etc/group | sed -e 's;.*:;;' -e 's;,; ;g' | wc -w`
	if ("$DEBUG" != "") echo PGROUP = $PGROUP\; PGCOUNT = $PGCOUNT
	if ($PGCOUNT > $PGMEMBERS) then
	    set GROUP = $PGROUP
	    @ PGMEMBERS = $PGCOUNT
	endif
end

/bin/sed -e "s;useradd;$USERADD;" \
	 -e "s;allusers;$ALLUSERS;" $TMP | \
    /usr/bin/awk '{print;if($1=="--uid"&&WHEEL)print "    -G '$GROUP' \\"}' $SUDO > $TMP.new
/bin/mv $TMP{.new,}

if ($?VERBOSE) echo `date +%T` Adding user $U2ADD
$DEBUG /bin/sh $TMP
if ("$DEBUG" != "") then
    echo " " where $TMP contains
    /bin/sed 's;.;    &;' $TMP
else
    logger -t$BASE -- $ARGS
endif

/bin/rm -f $TMP

if ($?VERBOSE) echo `date +%T` Done
