#!/bin/bash

cat > /usr/sbin/fix-astra-mobile-inputs <<EOF
#!/bin/bash

PS_PID=""

export LANG=C

if [ \$(astra-mic-control is-enabled) == "DISABLED" ]
then
  exit 0
fi

while [ -z \$(pidof plasmashell) ]
do
    echo "Wait..."
    sleep 1
done

while :
do
  if [[ \${PS_PID} != \$(pidof plasmashell) ]]
  then
      PS_PID=\$(pidof plasmashell)
      find /sys/class/input/event* -exec udevadm trigger --action=add {} \;
  fi

  sleep 5
done
EOF

chmod +x /usr/sbin/fix-astra-mobile-inputs

cat > /etc/systemd/system/fix-astra-mobile-inputs.service  <<EOF
[Unit]
Description=Fix Astra Mobile inputs

[Service]
Type=simple
ExecStart=/bin/bash -c /usr/sbin/fix-astra-mobile-inputs

[Install]
WantedBy=multi-user.target
EOF

systemctl enable fix-astra-mobile-inputs