#!/bin/bash 

if [[ $UID -ne 0 ]]; then
    echo "$0 must be run as root"
    echo " Please use: sudo $0"
    exit 1
fi

IR_KEYTABLE="$(2>&1 ir-keytable 2>&1)"

for rc in "$(echo $IR_KEYTABLE | grep rc/rc | sed 's/Found\ .*rc\///g; s/\/.*//g')";
do
  RC="$(ir-keytable --sysdev $rc 2>&1)"
  echo "### sudo ir-keytable ###"
  echo "$RC"
  echo "########################"
  echo "### Loaded Keytable  ###"
  TYPE=$( echo "$RC" | grep "Enabled protocols" | sed s/Enabled\ protocols:\ //g;)
  TABLE=$( echo "$RC" | grep "Driver" | sed s/.*,\ //g;)
  KEYS=$(ir-keytable -r --sysdev $rc 2>&1 | sed 's/scancode\ //g;s/\ (.*//g;')
  echo -e "# $TABLE, protocol:$TYPE"
  echo -e "$KEYS"
  echo "### End of Keytable ####"
done

