#! /bin/sh

progname=irman2lirc

if [ "$1" = "--help" ]; then
  echo "${progname}: converts irman.conf to lirc config file"
  echo
  echo "usage: ${progname} [file]"
  echo "       where \`file' is the full file name of your irman.conf"
  echo "       (default /usr/local/etc/irman.conf)"
  exit 0
fi

irmanconf=${1:-/usr/local/etc/irman.conf}
if [ ! -f ${irmanconf} ]; then
  echo "cannot find ${irmanconf}.  try ${progname} --help"
  exit 1;
fi

echo "# lircd.conf automatically generated by ${progname}"
cat <<EOF
#
# brand:             Irman
# model:             All models
EOF

echo "# devices:           remotes from ${irmanconf}"
grep bind ${irmanconf} | sed 's/^[ \t]*bind[ \t]\+\([^- \t]\+-[^-]\+\)-.*$/\1/' |
  sort | uniq | sed 's/^/#                        /'

cat <<EOF
#

begin remote

  name            irman
  bits            48
  flags           SPACE_ENC
  eps             30
  aeps            100
  one             1024  3072
  zero            1024  2048
  ptrail          1024
  pre_data_bits   16
  pre_data        0xffff
  post_data_bits  0
  post_data       0x0
  gap             65536
  repeat_bit      0

     begin codes

EOF

IFS="    "
grep '^[        ]*bind' ${irmanconf} |
while
  read bind name code
do
  if [ "x$bind" = "xbind" -a -n "$code" -a -n "$name" ]; then
    code=`echo "${code}000000000000" | cut -c1-12`
    echo "          ${name}     0x0000${code}"
  fi
done

cat <<EOF

      end codes

end remote
EOF

