#!/bin/sh

confpath=/home/user/.config/sim-switcher

read_config()
{
#$1, config file (without path)
conffile=$confpath/$1
local ret_val

if [ ! -f $conffile ]
then
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Please give a valid config file 
($conffile) ! " uint32:0 string:"OK"
  ret_txt="no conf file"
  ret_val=11
else
  source $conffile
  if [ Z"$sim" == Z"" ]
  then
    /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Please give at least the switching code! 
Use vi, nano, txpad, leafpad to edit the config file 
($conffile) ! " uint32:0 string:"OK"
    ret_text="no sim switching code"
    ret_val=12
  else
    ret_text=""
    ret_val=0
  fi
fi

#echo $ret_txt
return $ret_val
}

do_switch()
{
#$1,sim        SIM switching code, as given by your adapter
#              (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,iap        autoconnect to specific IC, created via fAPN, needs to be read out via 'gconftool -R /system/osso/connectivity/IAP'
#              (name of IC_IAP)
#$6,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))
#$7,forw_n_bef number to forward calls to before switching, intl. format
#              (+49...)
#$8,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))
#$9,forw_n_aft number to forward calls to after switching, intl. format
#              (+49...)

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

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

# setting call forwarding for old SIM (before change)
if [ Z"$6" != Z"" ]; then
  if [ $6 -lt 0 ]; then
    let div=$6
    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:$6 string:"$7" uint32:
  fi
  sleep 5
fi

#set and save new SIM
/usr/bin/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
#echo 'echo '$1' > /tmp/changeSIM.tmp' |root
#echo 'echo '$1' > /opt/sim-switcher/switchSIM.tmp' |root
echo $1 > /home/user/.config/sim-switcher/.tmp/currentSIM
sleep 1

#dis-/enable phonet
#sudo /sbin/ifconfig phonet0 down
sudo /opt/sim-switcher/phonet-helper down
sleep 7
#
#set cell mode
if [ Z"$3" != Z"" -a $3 -ge 0 -a $3 -le 2 ]; then 
  dbus-send --system --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:$3
  sleep 1
fi
#
#set connectivity policy
if [ Z"$4" != Z"" ]; then 
  gconftool -s --type list --list-type string /system/osso/connectivity/network_type/auto_connect [$4]
  sleep 1
fi
#
#sudo /sbin/ifconfig phonet0 up
sudo /opt/sim-switcher/phonet-helper up

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

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

#enable IC for specififc IAP
if [ Z"$5" != Z"" ]; then
  sleep 1
  /usr/bin/dbus-send --system --print-reply --type=method_call --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:"$5" uint32:0
fi

#set call forwarding for new SIM (after change)
if [ Z"$8" != Z"" ]; then
  sleep 5
  if [ $8 -lt 0 ]; then
    let div=$8
    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:$8 string:"$9" uint32:
  fi
fi

return 0
}


#main
if [ ! -d $confpath/.tmp ]; then mkdir $confpath/.tmp; fi

if [ -z "$1" ]
then
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Usage: 
sim-switcher 'name_of_config_file'; #to switch SIM, e.g. 'SIM1'
sim-switcher 'savehome'; #to save plugins/widgets
sim-switcher 'restorehome'; #to restore plugins/widgets" uint32:0 string:"OK"
  exit 1
elif [ Z"$1" == Z"savehome" ]
then
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Hildon-home plugins saved!" 
  cp /home/user/.config/hildon-desktop/home.plugins     /home/user/.config/sim-switcher/home.plugins.bkp
  exit 255
elif [ Z"$1" == Z"restorehome" ]
then
  /usr/bin/dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Hildon-home plugins will be restored!" 
  cp /home/user/.config/sim-switcher/home.plugins.bkp /home/user/.config/hildon-desktop/home.plugins
  exit 254
fi

#if [ -z "$(read_config $1)" ] # will not hand back the 'non-local' vars
read_config $1
rc_ret=$?
if [ 0 -eq $rc_ret ]
then
echo $sim
  do_switch "$sim" "$refhome" "$cellmode" "$connpol" "$iap" "$forw_bef" "$forw_n_bef" "$forw_aft" "$forw_n_aft"
  ds_ret=$?
  echo do_switch returned $ds_ret with msg: $ret_txt
  exit $ds_ret
else 
  echo read_config returned $rc_ret with msg: $ret_txt
  exit $rc_ret
fi
#end script