#!/bin/bash

FIRSTUID=30000
LASTUID=31000

if [[ "$#" != 1 || "$1" == -* ]]; then
cat << EOF
This script should be used by Samba. Add the following line to your
smb.conf (under [global] section):
   add machine script = $0 '%u'

If you plan to execute this script manually, remember to add $ at end
of machine name, and to execute "smbpasswd -a -m" afterwards.
EOF
exit 1
fi

NEXTUID=`awk 'BEGIN{FS=":";m='$FIRSTUID'-1}{if($3>='$FIRSTUID' && $3<'$LASTUID' && $3>m)m=$3}END{print m+1}' < /etc/passwd`

/usr/sbin/useradd -d /dev/null -g winmachines -c 'Windows Machine Account' -s /bin/false -u "$NEXTUID" "$1"
