#!/bin/bash
PATH=.:$PATH
#-------------------------------------------------------------------------------
# 0.1 initial version by Khadiyd Idris
# 0.2 10/03 phonebook, info, complete backup added by Martin Pescador
# 0.3 09/04 adapt character sets, user defined editor, minor changes
#           by Jürgen Braukmann
#-------------------------------------------------------------------------------

############# this should read me45sync config
############# but not yet supported now, edit it!
DEVICE=/dev/ttyS0
SCMXX=/usr/bin/scmxx
EDITOR=/usr/bin/vi 
RECODE=/usr/bin/recode

############# Charset configuration
CHRSETMOBIL="windows-1250"
CHRSETHOST="UTF8"
HOSTTOMOBIL=$CHRSETHOST".."$CHRSETMOBIL
MOBILTOHOST=$CHRSETMOBIL".."$CHRSETHOST

############ preparation
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

############
DIALOG=${DIALOG=dialog}

$DIALOG --title "ME45Sync 0.3" --clear \
        --msgbox "Welcome to ME45Sync! \
  This is a program to manage your Siemens ME45/S45/*45 mobile phone. \n\n
  This program is written by Khadiyd Idris <khad@linuxindo.com>\
  and extended by Martin Pescador <Martin.Pescador@gmx.de>.\
  Further changes by Jürgen Braukmann <juergen.braukmann@ruhr-west.de> \
  This program is licensed with the GPL. See COPYING.\
  Use at your own risk! \n\nPress ENTER to continue." 14 55

case $? in
  0)
    echo "OK";;
  255)
    echo "ESC pressed.";;
esac

IMEI=`$SCMXX --info 2>/dev/null | grep IMEI | cut -f3`
ABDIR="$HOME/me45-AB-"$IMEI
PBDIR="$HOME/me45-PB-"$IMEI
mkdir -p $ABDIR
mkdir -p $PBDIR

retval=0
while [ $retval = 0 ]; do

$DIALOG --clear --title "ME45Sync" \
        --menu "Make sure your handphone is connected to PC.\n
                What would you do?" 22 51 14 \
  "Info"      "Get telefon information" \
  "Backup"    "Get all telefon entries" \
  "GetPB"     "Get all phonebook from mobile" \
  "EditPB"    "Edit imported phonebook" \
  "SendPB"    "Send phonebook to mobile" \
  "GetAB"     "Get all addressbook from mobile" \
  "EditAB"    "Edit imported addressbook" \
  "AddAB"     "Add new record to addressbook" \
  "SendAB"    "Send addressbook to mobile" \
  "GetBit"    "Get all bitmaps from mobile" \
  "SendBit"   "Send bitmaps to mobile" \
  "GetCal"    "Get all calendar from mobile" \
  "RemoveCal" "Remove calendar from mobile" \
  "SendCal"   "Send calendar to mobile" \
  "NewSMS"    "Send SMS" \
  "Messages"  "View SMS Messages" \
  "Time"      "Synchronize time on phone" \
  "SMSC"      "Set the SMSC number" \
  "Reset"     "Reset handphone if not responding" \
  "Exit"      "End this program" 2> $tempfile
retval=$?

choice=`cat $tempfile`

case $retval in
  0)
    echo "'$choice' chosen."
    case $choice in
#---------------------------------------------------------------------------------
  Backup)
      $DIALOG --title 'Backup' --yesno "We make a backup of your mobile. Continue?" 7 51
      case $? in
      0)
        rm $PBDIR/* -f
        $SCMXX -d $DEVICE -i       -o "$PBDIR/Info"
        $SCMXX -d $DEVICE -g -PSM -o "$PBDIR/PhoneBookSM"
        $SCMXX -d $DEVICE -g -PON -o "$PBDIR/PhoneBookON"
        $SCMXX -d $DEVICE -g -PME -o "$PBDIR/PhoneBookME"

        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookSM"
        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookON"
        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookME"

        rm $ABDIR/* -f
        $SCMXX -d $DEVICE -g -Fall -o "$ABDIR/"
        $SCMXX -d $DEVICE -g -Ball -o "$ABDIR/"
        $SCMXX -d $DEVICE -g -Call -o "$ABDIR/"

	for ii in "$ABDIR/*.vcf"
	do
	  $RECODE $MOBILTOHOST $ii
	done

	for ii in "$ABDIR/*.cal"
	do
	  $RECODE $MOBILTOHOST $ii
	done
	
        $DIALOG --sleep 2 --title "Get" --infobox "Backup done." 3 51
        ;;

      esac
  ;;
#---------------------------------------------------------------------------------
  Info)
      $DIALOG --title 'GetInfo' --yesno "This will get general information of your mobile. Continue?" 7 51
      case $? in
      0)
        rm $PBDIR/Info -f
        $SCMXX -d $DEVICE -i -o "$PBDIR/Info" >/dev/null 2>/dev/null
        $DIALOG --sleep 2 --title "Get" --infobox "All information received." 3 51
        ;;
      esac
  ;;
#---------------------------------------------------------------------------------
  GetPB)
      $DIALOG --title 'GetPB' --yesno "This will replace your previously imported phonebook. Continue?" 7 51
      case $? in
      0)
        rm $PBDIR/PhoneBookSM -f
        rm $PBDIR/PhoneBookON -f
        rm $PBDIR/PhoneBookME -f
        $SCMXX --device $DEVICE --get --pbook=SM --out "$PBDIR/PhoneBookSM"
        $SCMXX --device $DEVICE --get --pbook=ON --out "$PBDIR/PhoneBookON"
        $SCMXX --device $DEVICE --get --pbook=ME --out "$PBDIR/PhoneBookME"

        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookSM"
        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookON"
        $RECODE $MOBILTOHOST "$PBDIR/PhoneBookME"

        $DIALOG --sleep 2 --title "Get" --infobox "All phonebooks imported," 3 51
        ;;
      esac
  ;;
#---------------------------------------------------------------------------------
  EditPB)
    $DIALOG --title 'EditPB' --yesno "This will edit imported phonebook. \
    Continue?" 7 51
    case $? in
    0)
      fname=$PBDIR/PhoneBookSM
      if [ -f "$fname" ]; then
        $EDITOR "$fname"
        $DIALOG --sleep 2 --title "Edit" --infobox "Phonebook SM changed in Computer." 3 51
      fi
      fname=$PBDIR/PhoneBookON
      if [ -f "$fname" ]; then
        $EDITOR "$fname"
        $DIALOG --sleep 2 --title "Edit" --infobox "Phonebook ON changed in Computer." 3 51
      fi
      fname=$PBDIR/PhoneBookME
      if [ -f "$fname" ]; then
        $EDITOR "$fname"
        $DIALOG --sleep 2 --title "Edit" --infobox "Phonebook ME changed in Computer." 3 51
      fi
    ;;
    esac
  ;;
#---------------------------------------------------------------------------------
  SendPB)
    $DIALOG --title 'SendPB' --yesno "This will update phonebook to mobile. \
    Continue?" 7 51
    case $? in
      0)

      $RECODE $HOSTTOMOBIL "$PBDIR/PhoneBookSM"
      $RECODE $HOSTTOMOBIL "$PBDIR/PhoneBookON"
      $RECODE $HOSTTOMOBIL "$PBDIR/PhoneBookME"

      $SCMXX --device $DEVICE --send --pbook=SM "$PBDIR/PhoneBookSM"
      $SCMXX --device $DEVICE --send --pbook=ON "$PBDIR/PhoneBookON"
      $SCMXX --device $DEVICE --send --pbook=ME "$PBDIR/PhoneBookME"

      $RECODE $MOBILTOHOST "$PBDIR/PhoneBookSM"
      $RECODE $MOBILTOHOST "$PBDIR/PhoneBookON"
      $RECODE $MOBILTOHOST "$PBDIR/PhoneBookME"

      $DIALOG --sleep 2 --title "Send" --infobox "All phonebook substituted on mobile." 3 51
      ;;
    esac
    ;;
#---------------------------------------------------------------------------------
  GetAB)
    $DIALOG --title 'GetAB' --yesno "This will replace your previously imported addressbook. Continue?" 7 51
    case $? in
    0)
      rm $ABDIR/*.vcf -f
      $SCMXX --device $DEVICE --get -Fall -o "$ABDIR/"

      for ii in "$ABDIR/*.vcf"
      do
        $RECODE $MOBILTOHOST $ii
      done

      $DIALOG --sleep 2 --title "Get" --infobox "All addressbook imported" 3 51
      ;;
    esac
    ;;
#---------------------------------------------------------------------------------
  EditAB)
    quit=0
    while [ $quit = 0 ]; do
      $DIALOG --clear --title "EditAB" \
      --menu "Pay attention to field codes:\n\
      N:last;first\n\
      ADR:;;street;city;;postcode;country\n\
      ORG: \n\
      TEL;HOME:\n\
      TEL;CELL:\n\
      TEL;WORK:\n\
      EMAIL;INTERNET:\n\
      URL:\n\
      Which record would you like to edit?" 25 51 9 \
      `grep -n "^N:" $ABDIR/*.vcf | awk -F "$ABDIR/" '{print $2}' | tr "\r" " " | awk '{print $1 $2 $3 $4 $5}' | awk -F '.vcf:.:N:' '{print $1 " " $2}'|sort -k 2 -f ` 2> $tempfile
      sel=`cat $tempfile`
      fname=$ABDIR/$sel.vcf
      if [ -f "$fname" ]; then
        cp "$fname" $tempfile
        $EDITOR "$fname"
        diff $tempfile "$fname"
        if [ $? = 1 ]; then
          $SCMXX -d $DEVICE -r -F$sel
          echo "Record #$sel deleted"
          sleep 1
          $SCMXX -d $DEVICE -s -F$sel "$fname"
          $DIALOG --sleep 2 --title "Edit" --infobox "Record #$sel updated." 3 51
        fi
      else
        quit=1
      fi
    done
    ;;
#---------------------------------------------------------------------------------
  AddAB)
    #REV:`date +%Y%m%dT%H%M%S`
    echo "BEGIN:VCARD
VERSION:2.1
N:lastname;firstname
ADR:;;street;city;;postcode;country
ORG:company
TEL;HOME:home
TEL;WORK:work
TEL;CELL:cellular
EMAIL;INTERNET:email
URL:http://website
END:VCARD" > $tempfile;
    $EDITOR $tempfile
    grep 'N:lastname;firstname' $tempfile
    if [ $? = 1 ]; then
      $SCMXX -d $DEVICE -sF500 $tempfile
      $DIALOG --sleep 4 --title "AddAB" --infobox "Record appended, please Get all addressbook to edit" 5 51
    fi
    ;;
#---------------------------------------------------------------------------------
  SendAB)
    $DIALOG --title 'SendAB' --yesno "This will append imported addressbook to handphone. \
    Make sure your addressbook empty. Continue?" 7 51
    case $? in
    0)

      for ii in "$ABDIR/*.vcf"
      do
        $RECODE $HOSTTOMOBIL $ii
      done
      
      for ii in "$ABDIR/*.vcf"
      do
        $SCMXX -d $DEVICE -s -F $ii
      done 
      
      for ii in "$ABDIR/*.vcf"
      do
        $RECODE $MOBILTOHOST $ii
      done

      sleep 2

      $DIALOG --sleep 2 --title "Send" --infobox "All addressbook appended" 3 51
      ;;
    esac
      ;;
#---------------------------------------------------------------------------------
  GetBit)
    $DIALOG --title 'GetBitmap' --yesno "This will replace your previously imported bitmaps. Continue?" 7 51
    case $? in
    0)
      rm $ABDIR/*.bmp -f
      $SCMXX -d $DEVICE -g -Ball -o "$ABDIR/"
      $DIALOG --sleep 2 --title "Get" --infobox "All bitmaps imported." 3 51
      ;;
    esac
    ;;
#---------------------------------------------------------------------------------
  SendBit)
    $DIALOG --title 'SendBitmap' --yesno "This will send bitmap to handphone. Continue?" 7 51
    case $? in
    0)
      $SCMXX -d $DEVICE -s -B $ABDIR/*.bmp
      $DIALOG --sleep 2 --title "Send" --infobox "All bitmaps sent." 3 51
      ;;
    esac
      ;;
#---------------------------------------------------------------------------------
  GetCal)
    $DIALOG --title 'GetCalendar' --yesno "This will replace your previously imported calendar. Continue?" 7 51
    case $? in
    0)
      rm $ABDIR/*.vsc -f
      $SCMXX -d $DEVICE -g -Call -o "$ABDIR/"
      $DIALOG --sleep 2 --title "Get" --infobox "Calendar imported." 3 51

      for ii in "$ABDIR/*.vsc"
      do
        $RECODE $MOBILTOHOST $ii
      done

      ;;
    esac
    ;;
#---------------------------------------------------------------------------------
  RemoveCal)
    $DIALOG --title 'RemoveCalendar' --yesno "This will remove your calendar in the mobile. Continue?" 7 51
    case $? in
    0)
      $SCMXX -d $DEVICE -r -Call
      $DIALOG --sleep 2 --title "Get" --infobox "Calendar removed." 3 51
      ;;
    esac
    ;;
#---------------------------------------------------------------------------------
  SendCal)
    $DIALOG --title 'SendAB' --yesno "This will append imported calendar to handphone. \
    Make sure your calendar empty. Continue?" 7 51
    case $? in
    0)

      for ii in "$ABDIR/*.vsc"
      do
        $RECODE $HOSTTOMOBIL $ii
      done

      $SCMXX -d $DEVICE -s -C $ABDIR/*.vsc

      for ii in "$ABDIR/*.vsc"
      do
        $RECODE $MOBILTOHOST $ii
      done
      $DIALOG --sleep 2 --title "Send" --infobox "Calendar updated." 3 51
      ;;
    esac
      ;;
#---------------------------------------------------------------------------------
  NewSMS)
    output=`Command not supported!`
    $DIALOG --sleep 2 --title "NewSMS" --infobox "New SMS:\n$output" 17 51
    ;;
#---------------------------------------------------------------------------------
  Messages)
    output=`Command not supported!`
    $DIALOG --sleep 2 --title "Message" --infobox "Message:\n$output" 17 51
    ;;
#---------------------------------------------------------------------------------
  SMSC)
    output=`Command not supported!`
    $DIALOG --sleep 2 --title "SMSC" --infobox "SMSC:\n$output" 17 51
    ;;
#---------------------------------------------------------------------------------
  Reset)
    output=`$SCMXX -d $DEVICE --reset 2>&1`
    $DIALOG --sleep 2 --title "Reset" --infobox "Handphone reset:\n$output" 17 51
    ;;
#---------------------------------------------------------------------------------
  Time)
    output=`$SCMXX -d $DEVICE --set-time 2>&1`
    $DIALOG --sleep 2 --title "Set Time" --infobox "Time sync:\n$output" 17 51
    ;;
#---------------------------------------------------------------------------------
  Exit)
    retval=1;;
    esac
    ;;
#---------------------------------------------------------------------------------
esac

done




