#! /bin/sh
#----------------------------------------------------------------------------
# add-user - add user
#
# Copyright (c) 2001-2004 Frank Meyer <frank@eisfair.org>
#
# usage: add-user
#    or: add-user user encrypted-password uid gid name home shell
#    or: add-user -d user encrypted-password uid gid name home shell
#                  option -d : disable password
#
# if password is empty, user will be prompted
# if uid or gid is empty, values will be evaluated
#
# Creation:     04.11.2001  fm
# Last Update:  $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

### -------------------------------------------------------------------------
### get optional flags
### -------------------------------------------------------------------------
flags=''
while [ 1 ]
do
    case "$1"
    in
        -d)
            flags="$flags -d"
            shift
            ;;
        *)
            break
            ;;
    esac
done

### -------------------------------------------------------------------------
### get parameter
### -------------------------------------------------------------------------
case $#
in
    0)
        interactive='true'

        user=""
        password=""
        uid=""
        gid=""
        name=""
        home=""
        shell=""
        ;;
    7)
        interactive='false'

        user="$1"
        password="$2"
        uid="$3"
        gid="$4"
        name="$5"
        home="$6"
        shell="$7"
        ;;
    *)
        echo "usage: `basename $0`" >&2
        echo "   or: `basename $0` user encrypted-password uid gid name home shell" >&2
        exit 1
        ;;
esac

### -------------------------------------------------------------------------
### clear screen
### -------------------------------------------------------------------------
if [ "$interactive" = "true" ]
then
    clrhome
    mecho -info "Add user"
    echo
fi

### -------------------------------------------------------------------------
### create uid
### -------------------------------------------------------------------------
if [ "$uid" = "" ]
then
    _ifs="$IFS"
    IFS=':'
    uid=2000
    while read line
    do
        set -- $line
        if [ $3 -gt $uid -a $3 -lt 3000 ]
        then
            uid=$3
        fi
    done </etc/passwd
    IFS="$_ifs"

    uid=`expr $uid + 1`
fi

### -------------------------------------------------------------------------
### get user name
### -------------------------------------------------------------------------
if [ "$user" = "" ]
then
    mecho "Information: The User-ID should be written in lowercase"
    echo
    echo -e "User-ID (e.g. 'www'): \c"
    read user
fi


### -------------------------------------------------------------------------
### get password and append user to files
### -------------------------------------------------------------------------
case "$user"
in
    "")
        mecho -warn "no user added"
        ;;
    *)
        grep "^$user:" /etc/passwd >/dev/null
        if [ $? = 0 ]
        then
            echo
            mecho -error "user-id $user already exists" 
        else
            if [ "$name" = "" ]
            then
                echo -e "Name of user: \c"
                read name
            fi

            if [ "$gid" = "" ]
            then
                gid=100
            fi
 
            if [ "$home" = "" ]
            then
                home=/home/$user
            fi

            if [ "$shell" = "" ]
            then
                shell=/bin/bash
            fi
            
            # add user to files 
            echo "$user:x:$uid:$gid:$name:$home:$shell" >>/etc/passwd
            echo "$user:$password:11622:0:99999:7:::" >>/etc/shadow

            grep "^users:" /etc/group >/dev/null
            if [ $? != 0 ]
            then
                echo "users:x:100:" >>/etc/group
            fi

            case $flags 
            in
                *-d)
                    # disable password
                    passwd -d $user >/dev/null
                    ;;
                *)
                    # get / set password
                    if [ -z "$password" ]
                    then
                        idx=1
                        while [ $idx -le 3 ]
                        do
                            passwd $user 
                            if [ $? = 0 ]
                            then
                                break
                            fi
                            idx=`/usr/bin/expr $idx + 1`
                            if [ $idx -eq 4 ]
                            then
                                # disable password
                                passwd -d $user >/dev/null
                                echo
                                mecho -error "Failed to get a valid password. Login for $user disabled."
                                mecho -std "Please use 'passwd $user' to change password manually."
                                anykey
                            fi
                        done
                    fi
                    ;; 
           esac
           # create home directory
           if [ ! -e $home ]
           then
               mkdir $home
               chown $user $home
               chgrp $gid $home
               chmod 700 $home
           fi
       fi
       ;;
esac

if [ "$interactive" = "true" ]
then
    mecho
    anykey
fi