#!/bin/bash
# Copyright (c) 2000,2004 Matthias S. Benkmann <article AT winterdrache DOT de>
# You may do everything with this code except misrepresent its origin.
# PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!

#
# This is a primitive script to serve as useradd until the real useradd
# has been installed. It has little error checking, so don't pass it anything
# stupid or it'll mess up your /etc/passwd and/or /etc/group file.
#

if [ $# -ne 13 -o z$1 != z-c -o z$3 != z-d -o z$5 != z-g -o z$7 != z-G -o z$9 != z-s -o z${11} != z-u ]; then
echo 1>&2 USAGE: useradd -c description -d home -g maingroup -G addgroup -s shell -u uid login 
exit 1
fi

#test if user already exists
grep "^${13}:.*" /etc/passwd 
if [ $? -eq 0 ]; then
  echo 1>&2 $0: User does already exist
  exit 1
fi       

g=`grep ^${6}:.\* /etc/group | cut -d : -f 3 -`
if [ z${g} = z ]; then
  echo 1>&2 $0: Group ${6} does not exist!
  exit 1
fi

grep ^${8}:.\* /etc/group >/dev/null || \
{
  echo 1>&2 $0: Group ${8} does not exist!
  exit 1
}


cp /etc/passwd /tmp/passwd123456
echo "${13}:x:${12}:$g:$2:$4:${10}" \
| sort -t : -k3,3n -m /tmp/passwd123456 - > /etc/passwd


cp /etc/group /tmp/group123456
sed  -e 's/^\('"${8}"':[^:]*:[0-9]*:..*\)$/\1,'"${13}"'/' \
     -e 's/^\('"${8}"':[^:]*:[0-9]*\):$/\1:'"${13}"'/' \
     						/tmp/group123456 >/etc/group
