#!/bin/sh

# do not wait for a network connection to launch the connection managers
dconf write /org/gnome/empathy/use-conn false 2>&1 > /dev/null

# check if there is a tp-ofono account already
# for now assume that if there is at least one account, the system is properly configured.
mc-tool show ofono/ofono/account0 2>&1 > /dev/null

# if there is not, create accounts and enable them
if [ $? -eq 1 ]; then
        # check if there is at least one modem
        if [ "$(getprop rild.libpath '')" == "" ]; then
            exit 0
        fi
        LAST_MODEM="`expr $(getprop ril.num_slots 1) - 1`"
        # get the translated default sim card name from the telephony-service po files.
        SIM_NAME=$(gettext -d telephony-service "SIM %1")
        GSETTINGS_ARRAY="{"

        # and for each modem found create a telepathy account
        for INDEX in $(seq 0 $LAST_MODEM); do
            ACCOUNT="account$INDEX"
            echo "creating ofono/ofono/$ACCOUNT"
            mc-tool add ofono/ofono $ACCOUNT string:modem-objpath=/ril_$INDEX
            echo "enabling ofono/ofono/$ACCOUNT"
            mc-tool enable ofono/ofono/$ACCOUNT
            mc-tool auto-connect ofono/ofono/$ACCOUNT
            # replace the %1 with the actual index
            NAME=$(echo $SIM_NAME | sed "s/%1$/$(($INDEX+1))/")
            mc-tool display ofono/ofono/$ACCOUNT "$NAME"
            # append this entry to the gsettings array
            GSETTINGS_ARRAY="$GSETTINGS_ARRAY '/ril_$INDEX': '$NAME',"
        done
        # remove the last "," if present
        GSETTINGS_ARRAY="$(echo $GSETTINGS_ARRAY | sed 's/,$//g')}"
        # set names in gsettings
        gsettings set com.ubuntu.phone sim-names "$GSETTINGS_ARRAY"
fi

echo 'ofono accounts initialized'
