#!/bin/bash

function generate_lircmap() {
   ISREMOTE=false
   NAMEFOUND=false
   ISCODE=false
   echo "<lircmap>"
   cat /etc/lirc/lircd.conf |\
   while read line
    do
       if [ "$line" = "" ]
         then
	  continue
       fi
       if echo $line | grep "begin remote" > /dev/null
         then
          ISREMOTE=true
	  NAMEFOUND=false
       elif echo $line | grep "end remote" > /dev/null
         then
	  if $ISREMOTE
	    then
   	     echo "       </remote>"
             ISREMOTE=false
	     NAMEFOUND=false
	     ISCODE=false
	     continue
	  fi
       fi
       if $ISREMOTE
         then
	  if echo $line | grep "begin codes" > /dev/null
	    then
	     ISCODE=true
	     continue
	  elif echo $line | grep "end codes" > /dev/null
	    then
	     ISCODE=false
	     continue
	  fi
	  
	  if ! $NAMEFOUND 
	    then
             if echo $line | grep " *name *.*" > /dev/null
               then
                NAMEFOUND=true
	        device=$(echo $line | awk '{print $2}')
	        if ! echo "$devicelist" | grep -w "$device" > /dev/null
	          then
	           devicelist="$devicelist $device"
                   echo "       <remote device=\"$device\">"
		   continue
                fi
	     fi
	 fi
         if $ISCODE
           then
	    key=$(echo $line | awk '{print $1}')
	    VDR=$(cat /var/lib/vdr/remote.conf | grep "^LIRC\..*$key$")
	    if ! [ "$VDR" =  "" ]
	      then
	       VDR_KEY=$(echo $VDR |awk '{print $1}' | cut -d"." -f2)
	       LIRC_KEY=$(echo $VDR |awk '{print $2}')
   	       case $VDR_KEY
	        in
                           Up)
   	                      echo "           <up>$LIRC_KEY</up>"
			     ;;
                         Down)
			      echo "           <down>$LIRC_KEY</down>"
			     ;;
          	         Menu)
			      echo "           <menu>$LIRC_KEY</menu>"
			     ;;
	                   Ok)
			      echo "           <select>$LIRC_KEY</select>"
			     ;;
		         Back)
			      echo "           <back>$LIRC_KEY</back>"
			     ;;
		         Left)
			      echo "           <left>$LIRC_KEY</left>"
			     ;;
			Right)
		              echo "           <right>$LIRC_KEY</right>"
			     ;;
		          Red)
			      echo "           <red>$LIRC_KEY</red>"
			     ;;
			Green)
			      echo "           <green>$LIRC_KEY</green>"
			     ;;
		       Yellow)
			      echo "           <yellow>$LIRC_KEY</yellow>"
			     ;;
		         Blue)
			      echo "           <blue>$LIRC_KEY</blue>"
			     ;;
			    0)
			      echo "           <zero>$LIRC_KEY</zero>"
			     ;;
			    1)
			      echo "           <one>$LIRC_KEY</one>"
			     ;;
			    2)
			      echo "           <two>$LIRC_KEY</two>"
			     ;;
			    3)
			      echo "           <three>$LIRC_KEY</three>"
			     ;;
			    4)
			      echo "           <four>$LIRC_KEY</four>"
			     ;;
			    5)
			      echo "           <five>$LIRC_KEY</five>"
			     ;;
			    6)
			      echo "           <six>$LIRC_KEY</six>"
			     ;;
			    7)
			      echo "           <seven>$LIRC_KEY</seven>"
			     ;;
			    8)
			      echo "           <eight>$LIRC_KEY</eight>"
			     ;;
			    9)
			      echo "           <nine>$LIRC_KEY</nine>"
			     ;;
			 Info)
			      echo "           <info>$LIRC_KEY</info>"
			     ;;
			 Play)
                              echo "           <play>$LIRC_KEY</play>"
			     ;;
			Pause)
		              echo "           <pause>$LIRC_KEY</pause>"
			     ;;
			 Stop)
			      echo "           <stop>$LIRC_KEY</stop>"
			     ;;
		       Record)
		              echo "           <record>$LIRC_KEY</record>"
			     ;;
		      FastFwd)
                              echo "           <forward>$LIRC_KEY</forward>"
			     ;;
		      FastRew)
			      echo "           <reverse>$LIRC_KEY</reverse>"
			     ;;
		         Next)
			      echo "           <skipplus>$LIRC_KEY</skipplus>"
			     ;;
		         Prev)
			      echo "           <skipminus>$LIRC_KEY</skipminus>"
			     ;;
		        Power)
			      echo "           <power>$LIRC_KEY</power>"
			     ;;
		     Channel+)
		              echo "           <pageplus>$LIRC_KEY</pageplus>"
			     ;;
		     Channel-)
		              echo "           <pageminus>$LIRC_KEY</pageminus>"
			     ;;
		  PrevChannel)
			     ;;
		      Volume+)
		              echo "           <volumeplus>$LIRC_KEY</volumeplus>"
			     ;;
		      Volume-)
		              echo "           <volumeminus>$LIRC_KEY</volumeminus>" 
			     ;;
		         Mute)
			      echo "           <mute>$LIRC_KEY</mute>"
			     ;;
		        Audio)
			     ;;
		     Schedule)
		              echo "           <title>$LIRC_KEY</title>"
			     ;;
		     Channels)
		              echo "           <mytv>$LIRC_KEY</mytv>"
			     ;;
		       Timers)
			     ;;
		   Recordings)
		              echo "           <myvideo>$LIRC_KEY</myvideo>"
			     ;;
		        Setup)
			     ;;
		     Commands)
			     ;;
		esac
	     fi
	  fi
       fi
   done
   echo "</lircmap>"
}

 
	          generate_lircmap
