#!/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
        # get the modems from ofono and sort them alphabetically
        MODEMS=`dbus-send --print-reply --system --dest=org.ofono / org.ofono.Manager.GetModems | grep object | cut -f2 -d\" | sort`
        INDEX=0
        # 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 modem in $MODEMS; do
            ACCOUNT="account$INDEX"
            echo "creating ofono/ofono/$ACCOUNT"
            mc-tool add ofono/ofono $ACCOUNT string:modem-objpath=$modem
            echo "enabling ofono/ofono/$ACCOUNT"
            mc-tool enable ofono/ofono/$ACCOUNT
            mc-tool auto-connect ofono/ofono/$ACCOUNT
            INDEX=$((INDEX+1))
            # replace the %1 with the actual index
            NAME=$(echo $SIM_NAME | sed "s/%1$/$INDEX/")
            mc-tool display ofono/ofono/$ACCOUNT "$NAME"
            # append this entry to the gsettings array
            GSETTINGS_ARRAY="$GSETTINGS_ARRAY '$modem': '$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'
