#!/bin/bash
# Gammu configuration generator
# Copyright (C) 2003 Michal Cihar <cihar at email dot cz>

DIALOG_SIZE="7 70"
MENU_SIZE="0 0 0"
INPUT_SIZE="0 0"
FILE_SIZE="20 60"
CONFIG=~/.gammurc
NAME="Gammu configurator"
VERSION="0.2"
COPYRIGHT="Copyright (C) 2003 Michal Cihar <cihar at email dot cz>"
FORCE=0

# Some defaults:

D_PORT=/dev/mobile
D_MODEL=at
D_CONNECTION=at19200
D_SYNCHRONIZETIME=yes
D_LOGFILE=
D_LOGFORMAT=nothing
D_LOCKING=
D_GAMMULOC=


TEMPFILE=`mktemp /tmp/gammu-config.XXXXXXXXXX`

showhelp() {
    echo "$NAME $VERSION"
    echo "$COPYRIGHT"
    echo
    echo "Usage: `basename $0` [-f|--force] [-c|--config config]"
    echo
    exit 100
}

cfgread() {
    grep '^[[:space:]]*'$1'[[:space:]]*=' "$CONFIG"| sed 's/^[^=]*=[[:space:]]*\([^#;]*\)[#;]\?.*$/\1/'
}

while [ "$#" -ge 1 ] ; do
    case "$1" in
        -f|--force)
            FORCE=1
            ;;
        -c|--config)
            if [ "$2x" != "x" ] ; then
                CONFIG="$2"
                shift
            else
                showhelp
            fi
            ;;
        *)
            showhelp
            ;;
    esac
    shift
done

dialog 2> /dev/null
if [ $? -eq 127 ] ; then
    echo "You need dialog installed to make this work"
    exit 1
fi

if [ -f "$CONFIG" ] ; then
    if [ ! -w "$CONFIG" ] ; then
        dialog --msgbox "Configuration file \"$CONFIG\" is not writable!" $DIALOG_SIZE
        exit 2
    fi

    if [ $FORCE -ne 1 ] ; then
        dialog --yesno "Configuration file \"$CONFIG\" exists, do you still wish to configure Gammu?" $DIALOG_SIZE
        if [ $? -eq 1 ] ; then
            exit 0
        fi
    fi

    dialog --yesno "Configuration file \"$CONFIG\" exists, do you wish to read defaults from it?" $DIALOG_SIZE
    if [ $? -eq 0 ] ; then
        echo "Parsing current Gammu configuration form \"$CONFIG\""
        PORT=`cfgread port`
        MODEL=`cfgread model`
        CONNECTION=`cfgread connection`
        SYNCHRONIZETIME=`cfgread synchronizetime`
        LOGFILE=`cfgread logfile`
        LOGFORMAT=`cfgread logformat`
        LOCKING=`cfgread use_locking`
        GAMMULOC=`cfgread gammuloc`
    fi
fi

mainmenu() {
     dialog --help-button --ok-label Edit --cancel-label Exit --extra-button --extra-label Save --menu "Current Gammu configuration" $MENU_SIZE \
        P "Port                 (${PORT:-$D_PORT})" \
        C "Connection           (${CONNECTION:-$D_CONNECTION})" \
        M "Model                (${MODEL:-$D_MODEL})" \
        D "Synchronize time     (${SYNCHRONIZETIME:-$D_SYNCHRONIZETIME})" \
        F "Log file             (${LOGFILE:-$D_LOGFILE})" \
        O "Log format           (${LOGFORMAT:-$D_LOGFORMAT})" \
        L "Use locking          (${LOCKING:-$D_LOCKING})" \
        G "Gammu localisation   (${GAMMULOC:-$D_GAMMULOC})" 2>"$TEMPFILE"
    BUTTON=$?
    ITEM=`cat "$TEMPFILE"`
}

editP() {
    dialog --fselect "${PORT:-$D_PORT}" $FILE_SIZE 2>"$TEMPFILE"
    err=$?
    if [ $err = 255 ] ; then
        if ( dialog --inputbox "Enter phone port" $INPUT_SIZE "${PORT:-$D_PORT}" 2>"$TEMPFILE" ) ; then
            PORT=`cat "$TEMPFILE"`
        fi
    elif [ $err = 0] ; then
        PORT=`cat "$TEMPFILE"`
    fi
}

helpwin() {
    dialog --textbox /proc/self/fd/5 0 0 5<< EOT
# Port           : in Windows/DOS: "com*:",
#                  (instead of "*" please put "1", "2", etc.)
#                  in other (Linux/Unix) "/dev/ttyS%"
#                                     or "/dev/ircomm%" ("irda" connection)
#                  (instead of "%" please put "0", "1", "2", etc.)
# Model          : use only, when Gammu doesn't recognize your phone model.
#                  Put it here. Example values: "6110", "6150", "6210", "8210"
# Connection     : type of connection. Use "fbus" or "mbus" or "dlr3" or
#                  "irda" (Infrared over sockets) or "infrared" (DirectIR)
#                  or "at19200" (AT commands on 19200, 8 bits, None parity,
#                  1 stop bit, no flow control) or "at115200" (AT commands on
#                  115200, 8 bits, None parity, 1 stop bit, no flow control)
#                  or "atblue" (AT over BlueTooth) or "dlr3blue" (FBUS
#                  over BlueTooth)
# SynchronizeTime: if you want to set time from computer to phone during
#                  starting connection. Do not rather use this option when want
#                  to reset phone during connection (in some phones need to
#                  set time again after restart)
# Logfile        : Use, when want to have logfile from communication.
# Logformat      : What debug info and format should be used:
#                  "nothing" - no debug level, "text" - transmission dump in
#                  text format, "textall" - all possible info in text format,
#                  "errors"  - errors in text format, "binary" - transmission
#                  dump in binary format
# Use_Locking    : under Unix/Linux use "yes", if want to lock used device
#                  to prevent using it by other applications
# GammuLoc       : name of localisation file
EOT
}

editgeneral() {
    TITLE="$1"
    VAR="$2"
    shift
    shift

    if ( dialog --menu "$TITLE" $MENU_SIZE "$@" 2>"$TEMPFILE" ) ; then
        VALUE=`cat "$TEMPFILE"`
        eval "$VAR='$VALUE'"
    fi
}

editC() {
    editgeneral "Select your phone connection" CONNECTION \
        fbus "" \
        mbus "" \
        dlr3 "" \
        irda "" \
        infrared "" \
        at19200 "" \
        at115200 "" \
        atblue "" \
        dlr3blue "" 
}

editM() {
    editgeneral "Select your phone model" MODEL \
        at "AT Generic phones" \
        alcatel "Alcatel BE5 (501,701)" \
        7110 "Nokia 6210|6250|7110|7190" \
        9210 "Nokia 9210" \
        6110 "Nokia 3210|33x0|3410|51x0|5210|5510|61x0|82x0|88x0" \
        6510 "Nokia 3510|6310|6310i|6510|8310|8910" \
        NAUTO "Nokia with autodetection"
}

editD() {
    editgeneral "Synchronize time with PC on each connect?" SYNCHRONIZETIME \
        yes "" \
        no ""
}

editF() {
    dialog --fselect "${LOGFILE:-$D_LOGFILE}" $FILE_SIZE 2>"$TEMPFILE"
    err=$?
    if [ $err = 255 ] ; then
        if ( dialog --inputbox "Enter log file" $INPUT_SIZE "${LOGFILE:-$D_LOG_FILE}" 2>"$TEMPFILE" ) ; then
            LOGFILE=`cat "$TEMPFILE"`
        fi
    elif [ $err = 0] ; then
        LOGFILE=`cat "$TEMPFILE"`
    fi
}

editO() {
    editgeneral "Select log format" LOGFORMAT \
        nothing "no debug level" \
        text "transmission dump in text format" \
        textdate "transmission dump in text format with date" \
        textall "all possible info in text format" \
        textalldate "all possible info in text format with date" \
        errors "errors in text format" \
        errorsdate "errors in text format with date" \
        binary "transmission dump in binary format"
}

editL() {
    editgeneral "Use device locking?" LOCKING \
        yes "" \
        no ""
}

editG() {
    dialog --fselect "${GAMMULOC:-${D_GAMMULOC:-/}}" $FILE_SIZE 2>"$TEMPFILE"
    err=$?
    if [ $err = 255 ] ; then
        if ( dialog --inputbox "Enter localisation file" $INPUT_SIZE "${GAMMULOC:-${D_GAMMULOC:-/}}" 2>"$TEMPFILE" ) ; then
            LOGFILE=`cat "$TEMPFILE"`
        fi
    elif [ $err = 0] ; then
        LOGFILE=`cat "$TEMPFILE"`
    fi
}


mainmenu

while [ $BUTTON -eq 0 -o $BUTTON -eq 2 ] ; do
    if [ $BUTTON -eq 2 ] ; then
        helpwin
    else
        edit$ITEM
    fi
    
    mainmenu
done

if [ $BUTTON -eq 3 ] ; then
    if [ -f "$CONFIG" -a $FORCE -ne 1 ] ; then
        dialog --yesno "Do you really wish to overwrite file \"$CONFIG\", with selected Gammu configuration?" $DIALOG_SIZE
        if [ $? -eq 1 ] ; then
            exit 0
        fi
    fi
    echo save
    cat > "$CONFIG" << EOT
# This is a generated gammurc file.
# It was generated by $NAME $VERSION

# In Unix/Linux  : copy it into your home directory and name it .gammurc
#                  or into /etc and name it gammurc
# In Win32       : copy it into directory with Gammu.exe and name gammurc

# Port           : in Windows/DOS: "com*:",
#                  (instead of "*" please put "1", "2", etc.)
#                  in other (Linux/Unix) "/dev/ttyS%"
#                                     or "/dev/ircomm%" ("irda" connection)
#                  (instead of "%" please put "0", "1", "2", etc.)
# Model          : use only, when Gammu doesn't recognize your phone model.
#                  Put it here. Example values: "6110", "6150", "6210", "8210"
# Connection     : type of connection. Use "fbus" or "mbus" or "dlr3" or
#                  "irda" (Infrared over sockets) or "infrared" (DirectIR)
#                  or "at19200" (AT commands on 19200, 8 bits, None parity,
#                  1 stop bit, no flow control) or "at115200" (AT commands on
#                  115200, 8 bits, None parity, 1 stop bit, no flow control)
#                  or "atblue" (AT over BlueTooth) or "dlr3blue" (FBUS
#                  over BlueTooth)
# SynchronizeTime: if you want to set time from computer to phone during
#                  starting connection. Do not rather use this option when want
#                  to reset phone during connection (in some phones need to
#                  set time again after restart)
# Logfile        : Use, when want to have logfile from communication.
# Logformat      : What debug info and format should be used:
#                  "nothing" - no debug level, "text" - transmission dump in
#                  text format, "textall" - all possible info in text format,
#                  "errors"  - errors in text format, "binary" - transmission
#                  dump in binary format
# Use_Locking    : under Unix/Linux use "yes", if want to lock used device
#                  to prevent using it by other applications
# GammuLoc       : name of localisation file

[gammu]

port = ${PORT:-$D_PORT}
model = ${CONNECTION:-$D_CONNECTION}
connection = ${MODEL:-$D_MODEL}
synchronizetime = ${SYNCHRONIZETIME:-$D_SYNCHRONIZETIME}
logfile = ${LOGFILE:-$D_LOGFILE}
logformat = ${LOGFORMAT:-$D_LOGFORMAT}
use_locking = ${LOCKING:-$D_LOCKING}
gammuloc = ${GAMMULOC:-$D_GAMMULOC}
EOT
    dialog --msgbox "Configuration saved to \"$CONFIG\"" $DIALOG_SIZE
fi

rm -f "$TEMPFILE"
