#
# Cellular configuration for Scadrial.
#
# vim: et:ts=2:sw=2:
#
{
  pkgs,
  ...
}:
{
  config = {

    #
    # Set up ModemManager to FCC unlock this device.
    #
    environment.etc."ModemManager/fcc-unlock.d/1eac:1001" = {
      text = ''
        #!${pkgs.bash}/bin/sh

        # require program name and at least 2 arguments
        [ $# -lt 2 ] && exit 1

        # first argument is DBus path, not needed here
        shift

        # second and next arguments are control port names
        for PORT in "$@"; do
          # match port type in Linux 5.14 and newer
          grep -q MBIM "/sys/class/wwan/$PORT/type" 2>/dev/null && {
            MBIM_PORT=$PORT
            break
          }
          # match port name in Linux 5.13
          echo "$PORT" | grep -q MBIM && {
            MBIM_PORT=$PORT
            break
          }
        done

        # fail if no MBIM port exposed
        [ -n "$MBIM_PORT" ] || exit 2

        # run mbimcli operation
        ${pkgs.libmbim}/bin/mbimcli --device-open-proxy --device="/dev/$MBIM_PORT" --quectel-set-radio-state=on
        exit $?
      '';

      # Let anyone execute this.
      mode = "555";
    };
  };

}
