#
# includes
# jGameBase script includes
# Copyright (C) 2006, 2007 F. Gerbig (fgerbig@users.sourceforge.net)
# 
# 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.
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General public License for more details.
# 
# You should have received a copy of the GNU General public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#


### CONSTANTS ###
YES=0
NO=1


### FUNCTIONS ###

# searchs in the first argument for the other arguments ignoring case
# $1: string in which to search
# $2..$n: strings to search
# returns: $YES (=0) or $NO (=1)
contains() {
    search=`toUpper "$1"`

    shift # remove $search from argument list
    for find in $*; do 
        find=`toUpper "$find"`
        if expr "$search" : ".*$find.*" >/dev/null; then
            return $YES
        fi
    done

    return $NO
}

# converts a string to upper case
# $1: string to convert
# returns: the converted string
toUpper() {
    echo "$1" | tr [:lower:] [:upper:] 
}

# converts a string to lower case
# $1: string to convert
# returns: the converted string
toLower() {
    echo "$1" | tr [:upper:] [:lower:]  
}


add_clp() {
    CLP=`echo "$CLP $1"`
}

# kill all running nstances of the given command
# $1: the name of the program to kill
kill_all() {
    for RUNNING in `ps | grep $1 | grep -v $$ | grep -v grep | awk '{ print $1 }'`
    do
    kill "$RUNNING"
    done
}


# display an info dialog
# $1: the message to display
info_dialog() {
    if [ -x /usr/bin/kdialog ]; then
        /usr/bin/kdialog --title Info --msgbox "$1" 
    elif [ -x /usr/bin/zenity ]; then
        /usr/bin/zenity --info --text "$1"
    elif [ -x /usr/bin/Xdialog ]; then
        /usr/bin/Xdialog --msgbox "Info:\n$1" 0 0
    elif [ -x /usr/bin/dialog ]; then
        /usr/bin/dialog --msgbox "Info:\n$1" 0 0
    else
        echo Info: "$1"
    fi
}

# display a warning dialog
# $1: the message to display
warning_dialog() {
    if [ -x /usr/bin/kdialog ]; then
        /usr/bin/kdialog --title Warning --sorry "$1" 
    elif [ -x /usr/bin/zenity ]; then
        /usr/bin/zenity --warning --text "$1"
    elif [ -x /usr/bin/Xdialog ]; then
        /usr/bin/Xdialog --msgbox "Warning:\n$1" 0 0
    elif [ -x /usr/bin/dialog ]; then
        /usr/bin/dialog --msgbox "Warning:\n$1" 0 0
    else
        echo Warning: "$1"
    fi
}

# display an error dialog
# $1: the message to display
error_dialog() {
    if [ -x /usr/bin/kdialog ]; then
        /usr/bin/kdialog --title ERROR --error "$1" 
    elif [ -x /usr/bin/zenity ]; then
        /usr/bin/zenity --error --text "$1"
    elif [ -x /usr/bin/Xdialog ]; then
        /usr/bin/Xdialog --msgbox "ERROR:\n$1" 0 0
    elif [ -x /usr/bin/dialog ]; then
        /usr/bin/dialog --msgbox "ERROR:\n$1" 0 0
    else
        echo ERROR: "$1"
    fi
}

# display a yes-no question dialog
# $1: the message to display
# returns: $YES (=0) or $NO (=1)
yesno_dialog() {
    if [ -x /usr/bin/kdialog ]; then
        /usr/bin/kdialog --title Question --yesno "$1" 
    elif [ -x /usr/bin/zenity ]; then
        /usr/bin/zenity --question --text "$1"
    elif [ -x /usr/bin/Xdialog ]; then
        /usr/bin/Xdialog --yesno "$1" 0 0
    elif [ -x /usr/bin/dialog ]; then
        /usr/bin/dialog --yesno "$1" 0 0
    else
        while :
        do
            echo "$1"
            echo "Answer (y)es or (n)o:"
            read ans
            case "$ans" in
                y)   return $YES ;;
                Y)   return $YES ;;
                yes) return $YES ;;
                Yes) return $YES ;;
                YES) return $YES ;;
                n)   return $NO  ;;
                N)   return $NO  ;;
                no)  return $NO  ;;
                No)  return $NO  ;;
                NO)  return $NO  ;;
            esac
        done
    fi
}

debug_display_gamebase_variables() {
    info_dialog "WORK_DIR=$WORK_DIR\n \
        GAME=$GAME\n \
        GAME_TYPE=$GAME_TYPE\n \
        GAME_PATH_0=$GAME_PATH_0\n \
        GAME_FILE_0=$GAME_FILE_0\n \
        GAME_FILE_NOEXT_0=$GAME_FILE_NOEXT_0\n \
        GAME_INDEX=$GAME_INDEX\n \
        FILE=$FILE\n \
        C64_FILE=$C64_FILE\n\n \
        GAME_PATH_1=$GAME_PATH_1\n \
        GAME_FILE_1=$GAME_FILE_1\n \
        GAME_FILE_NOEXT_1=$GAME_FILE_NOEXT_1\n\n \

        GAME_PATH_2=$GAME_PATH_2\n \
        GAME_FILE_2=$GAME_FILE_2\n \
        GAME_FILE_NOEXT_2=$GAME_FILE_NOEXT_2\n\n \

        GAME_PATH_3=$GAME_PATH_3\n \
        GAME_FILE_3=$GAME_FILE_3\n \
        GAME_FILE_NOEXT_3=$GAME_FILE_NOEXT_3\n\n \

        GAME_PATH_4=$GAME_PATH_4\n \
        GAME_FILE_4=$GAME_FILE_4\n \
        GAME_FILE_NOEXT_4=$GAME_FILE_NOEXT_4\n\n \

        GAME_FILECOUNT=$GAME_FILECOUNT\n \
        COMMENT=$COMMENT\n \
        CONTROL=$CONTROL\n \
        PALNTSC=$PALNTSC\n \
        TRUEDRIVE=$TRUEDRIVE\n\n \
        HARDWARE_JOYSTICK=$HARDWARE_JOYSTICK"
}
