#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/ask - ask user
#
# Copyright (c) 2003 Frank Meyer <frank@eisfair(dot)org>
#
# Creation:	    19.07.2003  fm
# Last Update:  $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
#
# Syntax:
#
# /var/install/bin/ask QUESTION [DEFAULT] [PATTERN] [PATTERN] ...
#
# QUESTION  -  Question to be asked.
# DEFAULT   -  Default answer
# PATTERN   -  Possible answers:
#              empty        -   y(es) or n(o)
#              *            -   string input
#              +            -   string input (not empty!)
#              min-max      -   numerical input (range given)
#              WORD|WORD    -   a list of possible values
#                               the first will be included into the question
#                               the last will be returned if choosen
#              WORD=WORD    -   a list of possible values
#                               all will be included into the question
#                               the first will be returned if choosen
#              ^$           -   ENTER
#
# Return Values:
#
# Exitcode:    Zero-Based Index of the Choosen PATTERN
#              255 ask was aborted by typing CTRL-C
# Output:      If Output is redirected, the output is either a part
#              of the matching pattern (if PATTERN is given) or the
#              entered text
#
# Examples:
#              ask "Kernel löschen"
#              ask "Kernel löschen" "no"
#              ask "Cronzyklus" "" "d|daily" "w|weekly" "m|monthly"
#              ask "Hostname" "eistest" "*"
#              ask "Hostname" "" "+" >result
#              ask "Weiter" >result
#              ask "Anzahl" "1" "1-7" >result
#              ask "Anzahl" "" "1-15" >result
#
#----------------------------------------------------------------------------

trap 'echo >/dev/tty;exit 255' SIGINT

question="$1"; shift
default="$1";  shift

### parameter processing
if [ $# -eq 0 ]                     # use yes/no as default
then
    regexp_n=2
    regexp_1="y|yes"
    regexp_2="n|no"
    values="y/n"
    full_values=" y(es) or n(o)"
elif [ $# -eq 1 -a "$1" = "*" ]     # string (empty allowed)
then
    regexp_n=1
    regexp_1=".*"
    values=""
    full_values=""
elif [ $# -eq 1 -a "$1" = "+" ]     # string (not empty)
then
    regexp_n=1
    regexp_1=".+"
    values=""
    full_values=""
else                                # list of values
    ### Build strings of possible values for output
    values=""
    full_values=" "
    regexp_n=0

    while [ $# -gt 0 ]
    do
        regexp_n=`/usr/bin/expr $regexp_n + 1`

        if echo "$1" | grep -qE '[0-9]+\-[0-9]+' 2>/dev/null
        then
            eval regexp_$regexp_n="[0-9]+"
            from=`echo "$1" | sed -e 's/-.*$//'`
            to=`echo "$1" | sed -e 's/^.*-//'`
            eval from_$regexp_n="$from"
            eval to_$regexp_n="$to"
            full_values="$full_values$from-$to"
            values="$values$from-$to"
        elif echo "$1" | grep -qE '^\^[$]=' 2>/dev/null
        then
            eval regexp_$regexp_n="^$\|"
            full_values="`echo "$full_values$1" | sed -e 's/\^[$]/ENTER/'`"
            values="`echo "$values$1" | sed -e 's/\^[$]/ENTER/'`"
        elif echo "$1" | grep -qE '^\^[$]' 2>/dev/null
        then
            eval regexp_$regexp_n=\"$1\"
            full_values="`echo "$full_values$1" | sed -e 's/\^[$]/ENTER/'`"
            values="`echo "$values$1" | sed -e 's/\^[$]/ENTER/'`"
        elif echo "$1" | grep -qE '=' 2>/dev/null
        then
            eval regexp_$regexp_n=\"`echo "$1" | sed -e 's/=/|/'`\|`echo "$1" | sed -e 's/=.*$//'`\"
            full_values="$full_values$1"
            values="$values$1"
        else
            eval regexp_$regexp_n=\"$1\"
            full_values="`echo "$full_values$1" | sed -e 's/|/=/'`"
            values="`echo "$values$1" | sed -e 's/|.*//'`"
        fi

        shift

        if [ $# -gt 0 ]
        then
            full_values="$full_values or "
            values="$values, "
        fi
    done
fi

### build question
if [ -n "$values" ] ; then  question="$question ($values)"  ; fi
if [ -n "$default" ] ; then question="$question [$default]" ; fi
question="$question? \c"

while [ 1 ]
do
    ### ask the question
    echo -e "$question" >/dev/tty

    ### read the answer
    read a
    a=${a:-$default}

    ### is the answer allowed?
    idx=1
    while [ $idx -le $regexp_n ]
    do
        eval regexp='$'regexp_$idx
        if echo $a | grep -qE "^($regexp)$"
        then
            eval from='$'from_$idx
            eval to='$'to_$idx
            if [ "$from" = "" ] || [ "$from" -le "$a" -a "$to" -ge "$a" ]
            then
                if ! /usr/bin/tty -s <&1
                then
                    # if pattern was in '*' '+' '^$'
                    #    or $from is not empty
                    #    write INPUT VALUE to file
                    # else
                    #    write LAST PART of REGEXP to file
                    #    e.g. (d|daily)                    -> daily
                    #    e.g. (d|daily|d) former (d=daily) -> d
	            if [ "$regexp" = ".*" ] || [ "$regexp" = ".+" ] \
                       || [ "$from" != "" ] || [ "$regexp" = "^$" ]
	    	    then
		        echo "$a"
		    else
                        echo "$regexp" | sed -e "s/.*|//"
		    fi
                fi
                exit `/usr/bin/expr $idx - 1`
            fi
        fi
        idx=`/usr/bin/expr $idx + 1`
    done

    colecho "Please answer$full_values!" br x br >/dev/tty
done

exit 255 # not reached