#!/bin/bash
# Gammu configuration generator
# Copyright (C) 2003 - 2009 Michal Cihar <michal@cihar.com>
# vim: expandtab sw=4 ts=4 sts=4:

DIALOG_SIZE="8 70"
MENU_SIZE="0 0 0"
INPUT_SIZE="0 0"
FILE_SIZE="20 60"
CONFIG=~/.gammurc
NAME="Gammu configurator"
VERSION="0.4"
COPYRIGHT="Copyright (C) 2003 - 2009 Michal Cihar <michal@cihar.com>"
FORCE=0

# Some defaults:

D_PORT=/dev/mobile
D_MODEL=
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 [ "x$2" != "x" ] ; then
                CONFIG="$2"
                shift
            else
                showhelp
            fi
            ;;
        *)
            showhelp
            ;;
    esac
    shift
done

if type dialog > /dev/null 2>&1 ; then
    DIALOG=dialog
elif type cdialog > /dev/null 2>&1 ; then
    DIALOG=cdialog
elif type whiptail > /dev/null 2>&1 ; then
    DIALOG=whiptail
else
    echo "You need dialog, cdialog or whiptail 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.\nDo you still wish to configure Gammu?" $DIALOG_SIZE
        if [ $? -eq 1 ] ; then
            exit 0
        fi
    fi

    $DIALOG --yesno "Configuration file \"$CONFIG\" exists.\nDo 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() {
    if [ $DIALOG = dialog ] ; then
        EXTRA="--help-button --ok-label Edit --cancel-label Exit --extra-button --extra-label Save"
        EXTRA_MENU=""
    else
        EXTRA=""
        EXTRA_MENU="H Help S Save"
    fi
    $DIALOG $EXTRA --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})" \
        $EXTRA_MENU \
        2>"$TEMPFILE"
    BUTTON=$?
    ITEM=`cat "$TEMPFILE"`
    if [ "$DIALOG" = whiptail -a "$BUTTON" = 0 ] ; then
        if [ "$ITEM" = H ] ; then
            BUTTON=2
        elif [ "$ITEM" = S ] ; then
            BUTTON=3
        fi
    fi
}

editP() {
    if [ $DIALOG = dialog ] ; then
        $DIALOG --fselect "${PORT:-$D_PORT}" $FILE_SIZE 2>"$TEMPFILE"
        err=$?
    else
        err=255
    fi
    if [ $err -eq 255 ] ; then
        if $DIALOG --inputbox "Enter phone port" $INPUT_SIZE "${PORT:-$D_PORT}" 2>"$TEMPFILE" ; then
            PORT=`cat "$TEMPFILE"`
        fi
    elif [ $err -eq 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 \
        at "" \
        at19200 "" \
        at115200 "" \
        fbus "" \
        fbus-nodtr "" \
        fbusirda "" \
        fbusdlr3 "" \
        fbusdku5 "" \
        mbus "" \
        irdaphonet "" \
        irdaat "" \
        irdaobex "" \
        dku2phonet "" \
        dku5fbus2 "" \
        dku5fbus2-nodtr "" \
        bluerffbus "" \
        bluerfphonet "" \
        bluephonet "" \
        bluerfat "" \
        blueat "" \
        phonetblue "" \
        fbusblue "" \
        fbuspl2303 "" \

}

editM() {
    editgeneral "Select your phone model" MODEL \
        "" "Automatic detection (use this if unsure)" \
        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() {
    if [ $DIALOG = dialog ] ; then
        $DIALOG --fselect "${LOGFILE:-$D_LOGFILE}" $FILE_SIZE 2>"$TEMPFILE"
        err=$?
    else
        err=255
    fi
    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() {
    if [ $DIALOG = dialog ] ; then
        $DIALOG --fselect "${GAMMULOC:-${D_GAMMULOC:-/}}" $FILE_SIZE 2>"$TEMPFILE"
        err=$?
    else
        err=255
    fi
    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 = ${MODEL:-$D_MODEL}
connection = ${CONNECTION:-$D_CONNECTION}
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"
