#!/bin/sh -e
#===============================================================================
#
#          FILE:  xietong-custom-ask
# 
#         USAGE:  ./xietong-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 xietong-custom/user_name || true
            ;;
        2)
            #check user name
            db_get xietong-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 xietong-custom/username-bad seen false
                db_input critical xietong-custom/username-bad
                STATE=1
                continue
            fi

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

            db_get xietong-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 xietong-custom/userhome-reserved seen false
                db_subst xietong-custom/userhome-reserved USERHOME "$USER_HOME"
                db_input critical xietong-custom/userhome-reserved
                STATE=3
                continue
            fi
            ;;
        5)
            db_input critical xietong-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

