#!/bin/sh -e
#===============================================================================
#
#          FILE:  stategrid-custom-ask
# 
#         USAGE:  ./stategrid-custom-ask
# 
#   DESCRIPTION:  
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: bqin (), bqin@linx-info.com
#       COMPANY: info
#       CREATED: 2013年10月16日 09时50分03秒 CST
#      REVISION:  ---
#===============================================================================

. /usr/share/debconf/confmodule

db_capb backup
db_capb escape

STATE=1

while true; do
    case $STATE in
        1)
            db_input critical stategrid-custom/user_name || true
            ;;
        2)
            #check user name
            db_get stategrid-custom/user_name
            USER="$RET"

            # follow pick up from user-setup-ask
            if ! LC_ALL=C expr "$USER" : '[a-z][-a-z0-9]*$' >/dev/null; then
            #    db_fset stategrid-custom/username-bad seen false
                db_input critical stategrid-custom/username-bad
                STATE=1
                continue
            fi

            if test -e /usr/lib/stategrid-custom-udeb/reserved-usernames && \
                grep -v '^#' /usr/lib/stategrid-custom-udeb/reserved-usernames | \
                grep -q "^$USER\$"; then
                db_fset stategrid-custom/username-reserved seen false
                db_subst stategrid-custom/username-reserved USERNAME "$USER"
                db_input critical stategrid-custom/username-reserved
                STATE=1
                continue
            fi
            ;;
        3)
            db_input critical stategrid-custom/user_home || true
            ;;
        4)
            #check user home

            db_get stategrid-custom/user_home
            USER_HOME="$RET"
            tmp="$(echo $USER_HOME|sed "s%/$%%")"

            if test "$tmp" == "/home/sysadmin" -o \
                "$tmp" == "/home/secadmin" -o \
                "$tmp" == "/home/netadmin" -o \
                "$tmp" == "/home/audadmin"  ;then
                db_fset stategrid-custom/userhome-reserved seen false
                db_subst stategrid-custom/userhome-reserved USERHOME "$USER_HOME"
                db_input critical stategrid-custom/userhome-reserved
                STATE=3
                continue
            fi
            ;;
        5)
            db_input critical stategrid-custom/user_shell|| true
            ;;
        *)
            break
            ;;
    esac
    if db_go; then
        STATE=$(($STATE + 1))
    else
        STATE=$(($STATE - 1))
    fi
done

# back up to menu
if [ "$STATE" -eq 0 ]; then
    exit 10
fi

