#!/bin/sh

# SIM-Switcher, automated switching with a double (dual) SIM adapter.
# by peterleinchen
# http://talk.maemo.org/showthread.php?t=85722

confpath=/home/user/.config/sim-switcher
if [ -f $confpath/.dbg ]
then
  log_file=$confpath/.tmp/log
else
  log_file=/dev/null
fi
home_restored=""
verbose=""


read_config()
{
# $1, config file (without path)
confcfg=$1
conffile=$confpath/$1
local ret_val
output="read_config: ...
$conffile"
echo "$output"; echo "$output" >> $log_file

if [ ! -f $conffile ]
then
  output="Please give a valid config file 
($conffile) ! "
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32:0 string:"OK"
  fi
  ret_txt="no conf file"
  ret_val=11
else
  source $conffile
  if [ Z"$sim" == Z"" ]
  then
    output="Please give at least the switching code! 
Use vi, nano, txpad, leafpad, ... to edit the config file 
($conffile) ! "
    echo "$output"; echo "$output" >> $log_file
    if [ -n "$verbose" ]; then
      /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32:0 string:"OK"
    fi
    if [ -e /usr/bin/leafpad ]
    then
      ret_txt="no sim switching code, editor leafpad"
      ret_val=12
      exec leafpad $conffile
    elif [ -e /usr/bin/txpad ]
    then
      ret_txt="no sim switching code, editor txpad"
      ret_val=13
      exec txpad $conffile
    elif [ -e /usr/bin/nano ]
    then
      ret_txt="no sim switching code, editor nano"
      ret_val=14
      exec osso-xterm "nano $conffile"
    elif [ -e /usr/bin/vim ]
    then
      ret_txt="no sim switching code, editor vim"
      ret_val=15
      exec osso-xterm "vim $conffile"
    elif [ -e /usr/bin/vi ]
    then
      ret_txt="no sim switching code, editor vi"
      ret_val=16
      exec osso-xterm "vi $conffile"
    else
      ret_txt="no sim switching code, no editor at all"
      ret_val=17
    fi
  else
    ret_txt=""
    ret_val=0
  fi
fi

output="$ret_txt"
echo "$output"; echo "$output" >> $log_file
return $ret_val
} #end read_config


restore_home()
{
output="restore_home: ..."
echo "$output"; echo "$output" >> $log_file

# kill hildon-home (if requested) for updating the operator name in status area
#if [ Z"$2" == Z"home" ]; then 
if [ Z"$1" == Z"home" ]; then 
  sleep 1
  #source /home/user/home
  cp /home/user/.config/hildon-desktop/home.plugins     $confpath/.tmp/home.plugins
  killall hildon-home
  sleep 12; #was 7
  cp $confpath/.tmp/home.plugins /home/user/.config/hildon-desktop/home.plugins
  home_restored="true"
else
  home_restored="false"
fi

output="restore_home: $home_restored"
echo "$output"; echo "$output" >> $log_file
} #end restore_home


set_operator_name()
{
output="operator_name: ..."
echo "$output"; echo "$output" >> $log_file

if [ -n "$2" ]
then
  desktopFile=/usr/share/applications/hildon/$1.desktop
  if [ -f $desktopFile ]
  then
    chkName=$(awk '{if ( $1" "$2 == "Name=""'"$1"'"" (""'"$2"'"")") print $1" "$2}' $desktopFile) 
    if [ -z "$chkName" ]
    then
      #echo "echo 'Name=$1 ($2)' >> $desktopFile" | sudo su
      sudo /opt/sim-switcher/operator-helper "Name=$1 ($2)" $desktopFile
      ret_txt="operator name set to ($2) for $1.desktop"
      return 0
    fi
  else
    ret_txt="Holy sh.., no $1.desktop file. Fully messed up!"
    echo "$ret_txt"
    if [ -n "$verbose" ]; then
      /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$ret_txt"
    fi
    return 31
  fi
else
  ret_txt="no operator name given"
  return 32
fi 
} #end set_operator_name


get_SIM_details()
{
output="get_SIM_details: ..."
echo "$output"; echo "$output" >> $log_file

MNCMCC=$(dbus-send --system --print-reply=literal --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_registration_status | tr '\n' ' ' | awk '{print $8,$10}')
MNC=$(echo $MNCMCC | cut -f1 -d' ')
MCC=$(echo $MNCMCC | cut -f2 -d' ')
OP=$(dbus-send --system --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_operator_name byte:0 uint32:$MNC uint32:$MCC | grep string | awk '{print $2 " " $3 " " $4 " " $6 " " $7 " "}' | tr -d \" | tr -s \ )
output="$1 SIM details: $OP, MNC: $MNC, MCC: $MCC"
echo "$output"; echo "$output" >> $log_file
if [ -n "$verbose" ]; then
  #/usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Almost done, we are back online ..." uint32: string:
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output"
  sleep 3
fi
} #end get_SIM_details


back_online()
{
output="back_online: ..."
echo "$output"; echo "$output" >> $log_file

# are we back online after the switch?
if [ Z"`cat /var/run/ssc`" != Z"online" ]
then
  output="SIM switched, but still waiting for cell modem to get online ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output"
  fi
  while [ Z"`cat /var/run/ssc`" != Z"online" ]
  do
    sleep 5
  done
else
  output="we are/were already online"
  echo "$output"; echo "$output" >> $log_file
fi

sleep 1
} #end back_online


do_switch()
{
#$1,sim        SIM switching code, as given by your adapter
#              (001, 11*, **1, #1, ...) or likely
#$2,refhome    kill hildon home to safely refresh provider name
#              (home)
#$3,cellmode   GSM/UMTS/Dual cell modem/radio mode
#              (1,  2,   0)
#$4,connpol    connectivity policy, autoconnect to type of network
#              ([], *, WLAN_INFRA, GPRS) meaning (always ask, any, only WLAN, default GPRS conn (of switched-to SIM))
#$5,changepol  defines the auto-change to WLAN connection behaviour
#              (true/false)
#$6,iap        autoconnect to specific IC, created via fAPN, needs to be read out via 'gconftool -R /system/osso/connectivity/IAP'
#              (name of IC_IAP)
#$7,forw_bef   set call forwarding BEFORE change, negative value means cancel
#              (1 unconditional, 2 busy, 3 no answer, 4 out of reach, 5 not available (inc:2,3,4))
#$8,forw_n_bef number to forward calls to before switching, intl. format
#              (+49...)
#$9,forw_aft   set call forwarding AFTER change, negative value means cancel
#              (1 unconditional, 2 busy, 3 no answer, 4 out of reach, 5 not available (inc:2,3,4))
#$10,forw_n_aft number to forward calls to after switching, intl. format
#              (+49...)
#$11,oper      operator name to be displayed on desktop item
#              (VF_de) please do not use more than 9 chars and NO blanks
#$12,reactivate the already switched to SIM (if SIM switch did not work)
#              ("reactivate")

output="do_switch: ..."
echo "$output"; echo "$output" >> $log_file

# check for empty SIM request
output="check for empty SIM request"
echo "$output"; echo "$output" >> $log_file
if [ Z"$1" == Z"" ]; then
  output="Please do NOT call me without a SIM number !
(e.g. 002 or 22* or **2 or #22 or ...)"
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32:0 string:"OK"
  fi
  ret_txt="no switching code"
  return 21
fi

# check if requested SIM already active
output="check if requested SIM already active"
echo "$output"; echo "$output" >> $log_file
#tmpSIM=`cat /tmp/changeSIM.tmp`
#tmpSIM=`cat /opt/sim-switcher/switchSIM.tmp`
if [ -r $confpath/.tmp/currentSIM ]; then
  tmpSIM=`cat $confpath/.tmp/currentSIM`
else
  tmpSIM=""
fi
if [ Z"$1" == Z"$tmpSIM" ]; then
  output="Please do NOT call me on already selected SIM ($1) "
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output!" uint32:0 string:"OK"
  fi
  ret_txt="already active"
  return 22
fi

# check for online status before starting the switching acticity, 
# cancel only if requested via 'sim-switcher break' or 'sim-switcher cancel'
output="check for online status before starting the switch"
echo "$output"; echo "$output" >> $log_file
if [ -f $confpath/.onlinecheck ]
then
  if [ -f $confpath/.tmp/.break ]; then rm $confpath/.tmp/.break; fi
  scount=0
  while [ Z"`cat /var/run/ssc`" != Z"online" -a ! -f $confpath/.tmp/.break ] 
  do
    output="... we need the cell modem to be online for SIM switching ..."
    echo "$output"; echo "$output" >> $log_file
    if [ -n "$verbose" ]; then
      /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output"
    fi
    if [ $scount -lt 30 ]; then
      sleep 5
      let scount+=5
    elif [ $scount -lt 60 ]; then
      sleep 10
      let scount+=10
    elif [ $scount -lt 120 ]; then
      sleep 30
      let scount+=30
    else  
      sleep 60
      let scount+=60
    fi
  done
  if [ -f $confpath/.tmp/.break ]
  then 
    output="Current SIM switching activity was cancelled!"
    echo "$output"; echo "$output" >> $log_file
    if [ -n "$verbose" ]; then
      /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string: 
    fi
    rm $confpath/.tmp/.break
    ret_txt="cancelled"
    return 23
  fi
fi

# check there is no call in progress
output="check there is no call in progress"
echo "$output"; echo "$output" >> $log_file
oncall=`dbus-send --system --type=method_call --print-reply=literal --dest=com.nokia.csd.Call /com/nokia/csd/call/1 com.nokia.csd.Call.Instance.GetStatus`
if [ Z"$oncall" != Z"   uint32 0" ]; then
  output="SIM switching not possible while a call is ongoing!"
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string:
  fi
  ret_txt="on call"
  return 24
fi

# get and print SIM1 details
get_SIM_details "Old (switched off)"
MNC1=$MNC
MCC1=$MCC
OP1=$OP
#echo $OP, MNC: $MNC, MCC: $MCC

###
#new: check for param 11 (reactivate)
#and if set, do not execute old SIM forwarding functionality
if [ -z "$12" ]; then
###
# setting call forwarding for old SIM (before change)
output="setting call forwarding for old SIM"
echo "$output"; echo "$output" >> $log_file
if [ Z"$7" != Z"" ]; then
  if [ $7 -lt 0 ]; then
    let div=$7
    let div*=-1
    /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.csd.SS /com/nokia/csd/ss com.nokia.csd.SS.DivertCancel uint32:$div string: uint32:
  else 
    /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.csd.SS /com/nokia/csd/ss com.nokia.csd.SS.DivertActivate uint32:$7 string:"$8" uint32:
  fi
  sleep 1
fi
###
#new: check for param 11 (reactivate)
#and if set, execute only phone up functionality
#if [ -n "$12" ]; then
fi
###

# disconnect (GPRS) internet
output="disconnect (GPRS) internet"
echo "$output"; echo "$output" >> $log_file
dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
#sleep 1
sleep 3
#back_online

# set and save new SIM
output="set and save new SIM"
echo "$output"; echo "$output" >> $log_file
dbus-send --system --print-reply --type=method_call --dest=com.nokia.csd.Call /com/nokia/csd/call com.nokia.csd.Call.CreateWith string:"$1" uint32:0
sleep 1
dbus-send --system --print-reply --dest=com.nokia.csd.Call /com/nokia/csd/call com.nokia.csd.Call.Release
#
#echo 'echo '$1' > /tmp/changeSIM.tmp' |root
#echo 'echo '$1' > /opt/sim-switcher/switchSIM.tmp' |root
echo $1 > $confpath/.tmp/currentSIM
echo $confcfg > $confpath/.tmp/currentSIM.configFile

# dis-/enable phonet
output="disable phonet"
echo "$output"; echo "$output" >> $log_file
#sudo /sbin/ifconfig phonet0 down
sudo /opt/sim-switcher/phonet-helper down
sleep 10; #was 7
#
# set internet connectivity policy
output="set internet connectivity policy"
echo "$output"; echo "$output" >> $log_file
if [ Z"$4" != Z"" ]; then 
  gconftool -s --type list --list-type string /system/osso/connectivity/network_type/auto_connect [$4]
  sleep 1
fi
if [ Z"$5" != Z"" ]; then 
  gconftool -s --type bool /system/osso/connectivity/network_type/change_while_connected $5
  sleep 1
fi
#
#sudo /sbin/ifconfig phonet0 up
output="enable phonet"
echo "$output"; echo "$output" >> $log_file
sudo /opt/sim-switcher/phonet-helper up
#get_SIM_details "Directly after:"

# give some/infinite time to enter PIN (if SIM requests PIN) and only needed for IAP and call forwarding after switch
# this will wait until cell modem gets online after switching
output="give some/infinite time to enter PIN"
echo "$output"; echo "$output" >> $log_file
if [ Z"$6" != Z"" -o Z"$9" != Z"" ]; then
  output="We need to wait for cell modem to get online ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32:4 string:"ack"
  fi
  if [ Z"$2" != Z"home" ]; then sleep 3; fi
  scount=0
  while [ "`cat /var/run/ssc`" != "online" ] 
  do
    output="... still waiting for cell modem to get online ..."
    echo "$output"; echo "$output" >> $log_file
    if [ -n "$verbose" ]; then
      /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output"
    fi
    if [ $scount -lt 30 ]; then
      sleep 3
      let scount+=3
    elif [ $scount -lt 60 ]; then
      sleep 5
      let scount+=5
    else  
      sleep 10
      let scount+=10
    fi
    if [ -z "$home_restored" ]; then restore_home "$2"; fi
  done
  output="Almost done, we are back ONLINE ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    #/usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Almost done, we are back online ..." uint32: string:
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output"
  fi
fi
#get_SIM_details "After:"

# set call forwarding for new SIM (after change)
output="set call forwarding for new SIM"
echo "$output"; echo "$output" >> $log_file
if [ Z"$9" != Z"" ]; then
  back_online
  #get_SIM_details "Forwarding:"
  if [ $9 -lt 0 ]; then
    let div=$9
    let div*=-1
    /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.csd.SS /com/nokia/csd/ss com.nokia.csd.SS.DivertCancel uint32:$div string: uint32:
  else 
    /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.csd.SS /com/nokia/csd/ss com.nokia.csd.SS.DivertActivate uint32:$9 string:"$10" uint32:
  fi
  sleep 1
fi

# set cell mode
output="set cell mode"
echo "$output"; echo "$output" >> $log_file
if [ Z"$3" != Z"" ]; then
  back_online
  #get_SIM_details "Cell mode:"
  if [ $3 -ge 0 -a $3 -le 2 ]; then 
    dbus-send --system --print-reply --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:$3
    sleep 7
  fi
fi

# enable IC for specific IAP
output="enable IC for specific IAP"
echo "$output"; echo "$output" >> $log_file
if [ Z"$6" != Z"" ]; then
  back_online
  #get_SIM_details "IAP:"
  sleep 7
  /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:"$6" uint32:0
  sleep 1
fi

# finishing the switch
output="finishing the switch"
echo "$output"; echo "$output" >> $log_file
back_online
#get_SIM_details "Finishing:"
get_SIM_details "New (switched on)"
#
if [ -z "$home_restored" ]; then
  restore_home $2
else
  sleep 10
fi
#
# get and print SIM2 details
#sleep 20
#get_SIM_details "New (switched on)"
MNC2=$MNC
MCC2=$MCC
OP2=$OP
#echo $OP, MNC: $MNC, MCC: $MCC
#
#output2="Current SIM: `echo $name | tr -d " (operator)" | tr -s 12` ( $oper ).
#output2="Current SIM: `echo $name | cut -f1 -d"("`( $oper ).
output2="Current SIM: $(echo $name | sed "s/(operator.)/( `echo $oper | sed 's/\(&\)/\\\&/'` )/").

Switching code '$1' used,
from $OP1, MNC: $MNC1, MCC: $MCC1
to      $OP2, MNC: $MNC2, MCC: $MCC2"
echo "$output2" > $confpath/.tmp/currentSIM.txt
#
if [ Z"$home_restored" == Z"true" ]; then
  output="Done, SIM switched and hildon-home restored!


$output2"
else
  output="Done, SIM switched!


$output2"
fi
echo "$output"; echo "$output" >> $log_file
if [ -n "$verbose" ]; then
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string:
fi

output="end of do_switch"
echo "$output"; echo "$output" >> $log_file
return 0
} #end do_switch


###
###main script
###

# check for being excuted as root
if [ `id -u` == 0 ] ; then
  output="Do NOT call me as root, 
falling back to user!"
  echo "$output"; #echo "$output" >> $log_file
  run-standalone.sh /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32:0 string:"OK"
  exec su - user <<EOF
    exec sh $0 $*
EOF
    exit $?
fi

# move log file ring buffer
for log_num in 8 7 6 5 4 3 2 1
do
  if [ -f $log_file.$log_num ]
  then
    mv $log_file.$log_num $log_file.$(expr $log_num + 1)
  fi
done
if [ -f $log_file ]
then
  mv $log_file $log_file.1
fi
#create new log file
echo `date` > $log_file

# create tmp directory, if not exist
if [ ! -d $confpath/.tmp ]; then mkdir $confpath/.tmp; fi

# check for verbose state
if [ -n "$2" ]; then
  if [ Z"$2" == Z"-v" -o Z"$2" == Z"--verbose" ]; then
    verbose="true"
  fi
fi
echo "verbose=$verbose" >> $log_file

# check for params and do appropriate action
echo "check for params and do appropriate action" >> $log_file
if [ -z "$1" -o Z"$1" == Z"-h" -o Z"$1" == Z"--help" -o Z"$1" == Z"-v" -o Z"$1" == Z"--verbose" ]
then
  output="Usage: 

sim-switcher 'name_of_config_file'; #switch, e.g. 'SIM1'
sim-switcher 'show'/'current'; #display active SIM
#
sim-switcher 'savehome'; #save plugins/widgets
sim-switcher 'restorehome'; #restore plugins/widgets
###
sim-switcher 'break'/'cancel'; #break/cancel ...
. an ongoing switch (hanging, due to old SIM not online)
sim-switcher 'reset'; #resets current SIM switching ...
. activity (hanging, due to not deactivating old SIM)
sim-switcher 'csdreset'; #resets SIM csd processes
sim-switcher 'reactivate'; #(re)activate last chosen SIM"
  echo "$output"; echo "$output" >> $log_file
  #if [ -n "$verbose" ]; then
  if [ Z"$1" == Z"-v" -o Z"$1" == Z"--verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string:
  fi
  exit 1
elif [ Z"$1" == Z"show" -o Z"$1" == Z"current" -o Z"$1" == Z"showcurrent" ]
then
  if [ -f $confpath/.tmp/currentSIM.txt ]
  then
    output="`cat $confpath/.tmp/currentSIM.txt`"
  else
    output="No stored current SIM (not yet switched)!"
  fi
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string:
  fi
  exit 199
elif [ Z"$1" == Z"savehome" ]
then
  output="Hildon-home plugins saved!"
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  cp /home/user/.config/hildon-desktop/home.plugins     $confpath/.home.plugins.bkp
  exit 189
elif [ Z"$1" == Z"restorehome" ]
then
  output="Hildon-home plugins will be restored!"
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  cp $confpath/.home.plugins.bkp /home/user/.config/hildon-desktop/home.plugins
  exit 188
elif [ Z"$1" == Z"onlinecheck" ]
then
  touch $confpath/.onlinecheck
  exit 179
elif [ Z"$1" == Z"noonlinecheck" ]
then
  if [ -f $confpath/.onlinecheck ]; then
    rm $confpath/.onlinecheck
  fi
  exit 178
elif [ Z"$1" == Z"break" -o Z"$1" == Z"cancel" -o Z"$1" == Z"stop" ]
then
  output="... current SIM switching activity will be cancelled ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  touch $confpath/.tmp/.break
  exit 249
elif [ Z"$1" == Z"reset" ]
then
  output="... resetting currently running sim-switcher ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  sudo /opt/sim-switcher/phonet-kill-helper
  exit 248
elif [ Z"$1" == Z"csdreset" ]
then
  output="... resetting SIM csd processes ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  sudo /opt/sim-switcher/csd-helper
  sleep 1
  killall sim-switcher; killall switchSIM
  exit 247
elif [ Z"$1" == Z"reactivate" ]
then
  output="... last SIM switching activity will be (re)activated ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$output" 
  fi
  currcfg=$(cat $confpath/.tmp/currentSIM.configFile)
  if [ -n "$currcfg" ]
  then
    #sim-switcher $currcfg
    read_config $currcfg
    rc_ret=$?
    if [ 0 -eq $rc_ret ]
    then
      output="$1: $sim, $refhome, $cellmode, $connpol, $changepol, $iap, $forw_bef, $forw_n_bef, $forw_aft, $forw_n_aft"
      echo "$output"; echo "$output" >> $log_file

      if [ -f $confpath/.tmp/currentSIM ]; then
        rm $confpath/.tmp/currentSIM
      fi
      
      ##sudo /opt/sim-switcher/phonet-helper down
      #sudo /opt/sim-switcher/phonet-kill-helper
      do_switch "$sim" "$refhome" "$cellmode" "$connpol" "$changepol" "$iap" "$forw_bef" "$forw_n_bef" "$forw_aft" "$forw_n_aft" "" "reactivate"
      ds_ret=$?
      output="do_switch returned $ds_ret with msg: $ret_txt"
      echo "$output"; echo "$output" >> $log_file
      exit $ds_ret
    else 
      output="read_config returned $rc_ret with msg: $ret_txt"
      echo "$output"; echo "$output" >> $log_file
      exit $rc_ret
    fi
  else
    sudo /opt/sim-switcher/phonet-helper down
    sleep 10; #was 7
    sudo /opt/sim-switcher/phonet-helper up
  fi
  exit 246
fi

# check running instances
echo "check running instances" >> $log_file
running=0
#echo `ps | grep "sim-switcher "`
#echo `ps | grep "switchSIM "`
runbin=`ps | grep "sim-switcher SIM" | awk '{SUM += 1} END {printf(SUM)}'`
let runbin-=1
if [ $runbin -ge 2 ]; then
  let runbin-=1
fi
runopt=`ps | grep "switchSIM SIM" | awk '{SUM += 1} END {printf(SUM)}'`
let runopt-=1
if [ $runopt -ge 2 ]; then
  let runopt-=1
fi
#
let running+=$runbin
let running+=$runopt
if [ $runbin -ge 2 -o $runopt -ge 2 -o $running -ge 2 ]; then
  output="Trying to run more than one (bin: $runbin, opt: $runopt) instance, 
will die now ..."
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$verbose" ]; then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$output" uint32: string:
  fi
  exit 2
fi

# check,read config and do the switch
echo "check,read config and do the switch" >> $log_file
#if [ -z "$(read_config $1)" ] # will not hand back the 'non-local' vars
read_config $1
rc_ret=$?
if [ 0 -eq $rc_ret ]
then
  output="$1: $sim, $refhome, $cellmode, $connpol, $changepol, $iap, $forw_bef, $forw_n_bef, $forw_aft, $forw_n_aft"
  echo "$output"; echo "$output" >> $log_file
  if [ -n "$oper" ]
  then
    set_operator_name "$1" "$oper"
    cso_ret=$?
    if [ 0 -ne $cso_ret ]
    then
      output="set_operator_name returned $cso_ret with msg: $ret_txt"
      echo "$output"; echo "$output" >> $log_file
      exit $cso_ret
    fi
  fi
  do_switch "$sim" "$refhome" "$cellmode" "$connpol" "$changepol" "$iap" "$forw_bef" "$forw_n_bef" "$forw_aft" "$forw_n_aft"
  ds_ret=$?
  output="do_switch returned $ds_ret with msg: $ret_txt"
  echo "$output"; echo "$output" >> $log_file
  exit $ds_ret
else 
  output="read_config returned $rc_ret with msg: $ret_txt"
  echo "$output"; echo "$output" >> $log_file
  exit $rc_ret
fi

###
###end script
###
