#!/bin/sh
#-------------------------------------------------------------------------------
# passgen
#       
# Generates users file with username and password from an input file. 
#
# Example:
#         
# Input file 'names.sample' containing:
#
#   Duck, Donald
#   Capone, Al
#   Duck, Al
#
# Processed by this command... 
#
#   ./passgen names.sample group users.sample
#
# Produces the output file 'users.sample' containing: 
#
#   donald,qw25t,Donald Duck,group
#   al,vT3a3,Al Capone,group
#   ald,dkR53,Al Duck,group
#
# The users can then be created using the command:
#
#   createusers users.sample
#
# Contributed to the Linux for Schools Project by Per Ulrik Arntsen 14.09.2002 
# par@hinesna.no 
#
# Minor changes by Phil Jones 20.09.2002
# phil@lfsp.org
#
#-------------------------------------------------------------------------------

infile=$1
group=$2

if [ $# -ne 3 ] ; then
  echo "Usage: passgen INPUTFILE GROUP OUTPUTFILE"
  echo "Example: passgen names.sample users users.sample"
  exit
fi

outfile=$3

# Start with some cleanup
if test -e $outfile 
then
  rm $outfile
fi
chmod 777 $outfile
touch $outfile

####################
# Define function: #
####################

# Random password generator for bash 2.x by Antek Sawicki <tenox@tenox.tc>,
# who generously gave permission for its use here.
# From "Advanced Bash-Scripting Guide" slightly altered 
# Digits [0,1] and  char [O,l] left out for clarity.
passgen() {
  MATRIX="0123456789"
  LENGTH="2"   # ==> You may change 'LENGTH' for a longer password.
  n=1
  while [ "${n:=1}" -le "$LENGTH" ] 
  do
    PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
    let n+=1
  done
  pwd="$PASS"
  PASS=""
} 

passgen2() {
DICTIONARY="./createusers.dict"

# compte le nombre de lignes du fichier
nb_lines=`cat $DICTIONARY | wc -l`
number=$RANDOM
let "number %= $nb_lines"

num=0

while [ "$num" -lt "$number" ]
do
   read mot
   let "num += 1"
done < $DICTIONARY
let "pos=${#mot}"
let "pos -= 2"
mot=${mot:1:$pos}

number=$RANDOM
let "number %= 89"
let "number += 9"

pwd=$mot$number
} 


# grep "/etc/passwd" and "$outfile" to check if "user" already exists.
# The '-c' parameter in the grep command counts the hits and gives $found 
# a value other than 0.
check_duplicates() {
  found=$((`grep -c ^$user: /etc/passwd` + `grep -c $user, $outfile` ))
}


#############
# Main code #
#############

while read a ; do

# if we have a non-blank line... 
 if [ "$a" != "" ] ;  then  
    # Convert the firstname to lowercase, remove blanks, translate national 
    # characters "" and put i variable
    user=`echo $a | cut -f1 -d"," | dd conv=lcase \
                  |  tr -d [:blank:] | tr [] [aoaAOAeeeuoiic]` 
    found=0             
    check_duplicates

    if [ $found != 0 ] ; then
       # first take the entire surname 
       surname=`echo $a | cut -f2 -d" " | dd conv=lcase` 
       # nr of letters from  $surname
       declare -i i=1                                  
    fi

    # if exists, add one letter from surname at a time to 
    # $user -loop and translate [] into [aoaAOAeeeuoiic]
    while [ $found != 0 ] ; do     
          user="$user"`echo $surname | cut -c"$i" | tr [] [aoaAOAeeeuoiic]`
          check_duplicates                 
          i="$i"+1
    done

    # Make password
    passgen2
  
    # Replace commas, for example, 'Joe, User' becomes 'Joe User'
    name=`echo $a | sed "s|, | |"`

    # Output results
    echo    "$user,$pwd,$name,$group" >> $outfile   

 fi 

done  < $infile
