#!/bin/sh
shout()  { echo "$0: $@" >&2; }
barf() { shout "fatal: $@"; exit 111; }
safe()  { "$@" || barf "cannot $@"; }

here=`env - PATH=$PATH pwd`
mypwd=${here%package}
mypwd=${mypwd%/}
home=`head -1 $mypwd/conf-home`
queue=`head -1 $mypwd/conf-queue`

if [  -f $mypwd/conf-ids ]
then
  sqmailids=`grep -v "^#" $mypwd/conf-ids | grep ":" | tr ' ' '_'`
else
  barf "Can't open file '$mypwd/conf-ids' with sqmail ids!"
fi

unix=`uname -a | cut -d' ' -f1 | tr [a-z] [A-Z]`

if [ "$unix" = "LINUX" -o "$unix" = "SUNOS" ]
then
  qshl="/bin/false"
  shout "Linux/OmniOS (Shell: $qshl):"

  for line in $sqmailids
  do
    if [ `echo $line | grep "group"` ]
    then
      qgid=`echo "$line" | cut -d":" -f1`
      qgrp=`echo "$line" | cut -d":" -f2`
      if [ `grep -c ^$qgrp /etc/group` -eq 0 ]
        then
          safe groupadd -g $qgid $qgrp
        else
	  shout "Group '$qgrp' already existing!"
        fi
    fi

    if [ `echo $line | grep "user"` ]
    then
      quid=`echo "$line" | cut -d":" -f1`
      qusr=`echo "$line" | cut -d":" -f2`
      qcom=`echo "$line" | cut -d":" -f3 | tr "_" " "`
      qgrp=`echo "$line" | cut -d":" -f4`
      if [ "$qusr" = "qmailq" ]
      then 
        qdir="$queue/`echo "$line" | cut -d":" -f5`"
      else
        qdir="$home/`echo "$line" | cut -d":" -f5`"
      fi
      if [ `grep -c $qusr /etc/passwd` -eq 0 ]
      then
        shout "Generating user: $qusr:$quid - $qgr - $qdir - $qshl - $qcom"
        safe useradd -u $quid -g $qgrp -d $qdir -s $qshl -c "$qcom" $qusr
      else
        shout "User '$qusr' already existing!"
      fi
    fi
  done

elif [ `echo "$unix" | grep "BSD"` ]
then
  qshl="/sbin/nologin"
  shout "BSD ($unix) (Shell: $qshl):"

  for line in $sqmailids
  do
    if [ `echo $line | grep "group"` ]
    then
      qgid=`echo "$line" | cut -d":" -f1`
      qgrp=`echo "$line" | cut -d":" -f2`
      if [ `grep -c ^$qgrp /etc/group` -eq 0 ]
        then
          if [ `echo "$unix" | grep "OPENBSD"` ]
          then
            safe groupadd -g $qgid $qgrp 
          else
            safe pw groupadd -n $qgrp -g $qgid 
          fi
        else 
          shout "Group '$qgrp' already existing!"
        fi
    fi
    if [ `echo $line | grep "user"` ]
    then
      quid=`echo "$line" | cut -d":" -f1`
      qusr=`echo "$line" | cut -d":" -f2`
      qcom=`echo "$line" | cut -d":" -f3 | tr "_" " "`
      qgrp=`echo "$line" | cut -d":" -f4`
      if [ "$qusr" = "qmailq" ]
      then 
        qdir="$queue/`echo "$line" | cut -d":" -f5`"
      else
        qdir="$home/`echo "$line" | cut -d":" -f5`"
      fi
      if [ `grep -c $qusr /etc/passwd` -eq 0 ]
      then
        shout "Generating user: $qusr:$quid - $qgrp - $qdir - $qshl - $qcom"
        if [ `echo "$unix" | grep "OPENBSD"` ]
        then
          safe useradd -u $quid -g $qgrp -d $qdir -s $qshl -c "$qcom" $qusr
        else
          safe pw useradd $qusr -u $quid -g $qgrp -d $qdir -s $qshl -c "$qcom"
        fi
      else
        shout "User '$qusr' already existing!"
     fi
    fi
  done

else
   shout "Unix OS not recognized; please install s/qmail user/groups manually \\
 and continue with installation step-by-step."
fi

exit 0
