#!/bin/bash

set -e

###############################################################################
#
# Check the 'gnats' userid in the password file
#

if [ "$1" = configure ]; then

  PWFOUND=$(protect grep -c "^$GNATSUSER:" $PASSWDFILE)

  if [ $PWFOUND -gt 1 ]; then
    db_subst gnats/user_multiple PASSWDFILE "$PASSWDFILE"
    db_input high gnats/user_multiple || true
    db_go
  fi

  if [ $PWFOUND -gt 0 ]; then
    if [ $(protect grep -c "^$GNATSUSER:.*:$GNATSOLDHOME:" $PASSWDFILE) -gt 0 ]
    then
      if [ -e $GNATSOLDHOME/.profile ]; then
	mv $GNATSOLDHOME/.profile $GNATSHOME/
      fi
      usermod -d $GNATSHOME $GNATSUSER
    fi
    if [ $(protect grep -c "^$GNATSUSER:[^:]*:$GNATSID:$GNATSGID:" $PASSWDFILE) \
	 -eq 0 ]
    then
      usermod -u $GNATSID -G $GNATSGID $GNATSUSER
    fi
  else
    adduser --quiet --system --home $GNATSHOME --no-create-home \
            --gid $GNATSGID --shell /bin/sh --disabled-login \
	    --gecos 'GNU GNATS Bug-Reporting System' $GNATSUSER
  fi

fi
