#!/bin/sh

# do.sh
# (c) 2003-2009 Jan ONDREJ (SAL) <ondrejj@salstar.sk>
# version 1.1

export DISPLAY=:0

cat `dirname $0`/lircrc | sed 's/#.*$//g' | awk '
  BEGIN {
    anychar="[a-zA-Z0-9]"
    #printf("begin\n")
    #printf("  flags = startup_mode\n")
    #printf("  mode = tvtime\n")
    #printf("end\n")
  }
  function id(idn) {
    while (idn>0) {
      idn--
      printf(" ")
    }
  }
  function echo(txt, arg, plus) {
    if (plus) {
      id(idx+2+plus)
    } else {
      id(idx+2)
    }
    printf(txt"\n", arg)
  }
  /^ *#/ { next }
  /^ *$/ { next }
  /^begin/ { print $0; idx+=2; next }
  /^end/ { print $0; idx-=2; next }
  {
    butn=split($1,but,";")
    comment=$2
    ps=""
    n=3
    while ($n != "") {
      ps=ps " " $n
      n++
    }
    pn=split(ps,par,";")
    while (butn>0) {
      echo("begin", "", -2)
      abutn=split(but[butn],abut,"@")
      butn--
      if (abutn>1) {
        echo("remote = %s", abut[2])
      #} else {
      #  echo("remote = *")
      }
      echo("button = %s", abut[1])
      if (par[1]~anychar) {
        echo("prog =%s", par[1])
        if (abut[2]=="TV" || abut[2]=="VCR") {
          echo("repeat = 2");
          #echo("delay = 2")
        }
      }
      if (par[2]~anychar) {
        echo("config = %s", par[2])
      }
      n=3
      while (par[n]~anychar) {
        echo("%s", par[n])
        n++
      }
      echo("end", "", -2)
    }
  }
' > ~/.lircrc

cycle() {
  while sleep 1s; do
    $1 > /dev/null 2>&1
    [ $? = 143 ] && break
  done
  echo "KILLED!"
}

killall python
killall irexec
killall irxevent
killall -9 irexec
killall -9 irxevent
cycle irexec &
cycle irxevent &

