#!/bin/sh
# harbour-carhotspot-helper
# Helper script for Car Hotspot on Sailfish OS
# Provides privileged ConnMan technology management

case "$1" in
    wifi-off)
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false
        ;;
    wifi-on)
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true
        ;;
    tethering-off)
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl tether wifi off 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Tethering variant:boolean:false 2>/dev/null || true
        ;;
    tethering-on)
        # Ensure wifi technology is powered on first
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl enable wifi 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true 2>/dev/null || true
        # Brief pause for interface initialization
        sleep 0.5
        # Now enable tethering (Access Point mode)
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl tether wifi on 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Tethering variant:boolean:true 2>/dev/null || true
        ;;
    cellular-on)
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/cellular net.connman.Technology.SetProperty string:Powered variant:boolean:true
        ;;
    cellular-off)
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/cellular net.connman.Technology.SetProperty string:Powered variant:boolean:false
        ;;
    bluetooth-on)
        for rf in /usr/sbin/rfkill /usr/bin/rfkill /sbin/rfkill /bin/rfkill; do
            if [ -x "$rf" ]; then
                "$rf" unblock bluetooth 2>/dev/null || true
                break
            fi
        done
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl enable bluetooth 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/bluetooth net.connman.Technology.SetProperty string:Powered variant:boolean:true 2>/dev/null || true
        ;;
    bluetooth-off)
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl disable bluetooth 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/bluetooth net.connman.Technology.SetProperty string:Powered variant:boolean:false 2>/dev/null || true
        ;;
    bluetooth-restart)
        # Deep recovery of the Sailfish OS Bluetooth subsystem
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl disable bluetooth 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/bluetooth net.connman.Technology.SetProperty string:Powered variant:boolean:false 2>/dev/null || true
        if [ -x /bin/systemctl ] || [ -x /usr/bin/systemctl ]; then
            systemctl restart bluetooth-rfkill-event.service 2>/dev/null || true
        fi
        sleep 1
        for rf in /usr/sbin/rfkill /usr/bin/rfkill /sbin/rfkill /bin/rfkill; do
            if [ -x "$rf" ]; then
                "$rf" unblock bluetooth 2>/dev/null || true
                break
            fi
        done
        if [ -x /usr/bin/connmanctl ]; then
            /usr/bin/connmanctl enable bluetooth 2>/dev/null || true
        fi
        /usr/bin/dbus-send --system --dest=net.connman /net/connman/technology/bluetooth net.connman.Technology.SetProperty string:Powered variant:boolean:true 2>/dev/null || true
        ;;
    *)
        echo "Usage: $0 {wifi-off|wifi-on|tethering-off|tethering-on|cellular-on|cellular-off|bluetooth-on|bluetooth-off|bluetooth-restart}"
        exit 1
        ;;
esac
