#!/bin/bash

# Patched version of the original script '/usr/bin/tiitoo-3g-functions.sh'
# Source: 
#
#	http://www.wetab-community.com/index.php?/topic/13213-geloest-3g-unter-kubuntu-1010/
#
# The following steps still need to be performed after downloading:
#	
#	mv ./Tiitoo-3g-functions.sh ./tiitoo-3g-functions.sh
# 	sudo chown root:root ./tiitoo-3g-functions.sh
# 	sudo chmod 755 ./tiitoo-3g-functions.sh
# 	sudo mv ./tiitoo-3g-functions.sh /usr/bin
#

if lsusb | grep '19d2:0031 ONDA Communication' > /dev/null ;
then 
        dev=/dev/ttyUSB2
elif lsusb | grep '19d2:0063 ONDA Communication' > /dev/null ;
then
        dev=/dev/ttyUSB3
elif lsusb | grep '12d1:1003 Huawei Technologies' > /dev/null ;
then
        dev=/dev/ttyUSB1
elif lsusb | grep '12d1:140c Huawei Technologies' > /dev/null ;
then
        dev=/dev/ttyUSB3
else
        dev=/dev/ttyUSB4
fi

is_modem_HuaweiEM770W() {
        lsusb | grep "12d1:1404" &> /dev/null
        return $?
}

exec_at_command() {
        #echo ---
        #echo $1
        chat -e ABORT OK ABORT ERROR '' ATE0 OK  < $dev 2> /dev/null > $dev
        output=$(chat -e ABORT OK ABORT ERROR '' "AT$1" OK < $dev 2>&1 > $dev)
        #echo $output
        #echo "----"
        if echo $output | grep ERROR &> /dev/null ; then 
                echo $output 
                return 1
        else 
                new_output=""
                # ignore unnecessary and unmotivated output from modem
                for word in $output; do
                        word_out=1
                        for ignored_word in OK POSITION; do
                                if echo $word | grep $ignored_word &> /dev/null; then 
                                        word_out=0
                                        break
                                fi
                        done
                        if [ $word_out -eq 1 ]; then
                                new_output="$new_output $word"
                        fi
                done
                echo $new_output  
        fi
}

get_imei() {
        exec_at_command "+CGSN"
}

get_imsi() {
        exec_at_command "+CIMI"
}

get_signal_strength() {
        out=$(exec_at_command "+CSQ")
        if [ $? -eq 0 ]; then 
                echo $out | cut -d " " -f 2 | cut -d "," -f 1
        else 
                echo $out
        fi
}

get_sim_state() {
        out=$(exec_at_command "+CPIN?")
        if [ $? -eq 0 ]; then 
                echo $out | cut -d " " -f 2-
        else 
                echo $out
        fi
}

set_pin_puk() {
        out=$(exec_at_command "+CPIN=$2")
        if [ $? -eq 0 ]; then 
                echo "OK"
        else 
                echo $out
        fi
}

enable_gps() {
        AGPS_SUPL="http://supl.nokia.com"
        if [ -f /etc/sysconfig/agps ]; then
                . /etc/sysconfig/agps
        fi
        out=$(exec_at_command "\^WPURL=$AGPS_SUPL")
        if [ $? -ne 0 ]; then
                echo "WPURL:$out"
                break
        fi
        # configure AGPS to work speed optimal
        out=$(exec_at_command "\^WPDOM=2")
        if [ $? -ne 0 ]; then
                echo "WPDOM:$out"
                break
        fi

        out=$(exec_at_command "\^WPDGP")
        if [ $? -eq 0 ]; then 
                echo "OK"
        else 
                echo "WPDGP:$out"
        fi
}

disable_gps() {
        out=$(exec_at_command "\^WPEND")
        if [ $? -eq 0 ]; then 
                echo "OK"
        else 
                echo $out
        fi
}

get_rf_switch_state() {
        out=$(exec_at_command "\^RFSWITCH?")
        echo $out
}

set_rf_switch_state() {
        out=$(exec_at_command "\^RFSWITCH=$2")
        if [ $? -eq 0 ]; then
                echo "OK"
        else 
                echo $out
        fi
}

reset() {
        exec_at_command "+CFUN=4"
        exec_at_command "+CFUN=6"
        # wait until the modem resets. It will close the device at the end of the reset procedure.
        cat $dev > /dev/null
        sleep 2
}

$1 $@


