#!/bin/sh

. ./paths

# print headers
echo 'Content-Type: text/xml'
echo 'Cache-control: no-cache'
echo 'Cache-control: no-store'
echo 'Pragma: no-cache'
echo 'Expires: 0'
echo ''


# ARGUMENTS:

param1=`echo $QUERY_STRING |  awk -F '=' '{print $1}' `
param2=`echo $QUERY_STRING |  awk -F '=' '{print $2}' `


# print XML
echo '<?xml version="1.0" encoding="UTF-8"?>'


# root
if [ "$param1" = "root" ] 
then
  echo '<tree>'
  ( echo "All Favourites=infav" ; echo "Favourite Lists=favlists" ; echo "Providers=providers" ; echo "Satellites=satellites" ; echo "All TV Channels=letters_tv" ; echo "All Radio Channels=letters_radio" ) | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" src=\"cgi-bin/xml_chantree?" $2 "\"/>" }'
  echo '</tree>'  
  exit 0
fi






#infav
if [ "$param1" = "infav" ]
then
  echo '<tree>'
  
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT distinct f.name, c.ch_order, c.cas_type FROM channelinfo c, favlistinfo f WHERE f.view_flg=0 AND c.ch_id=f.ch_id AND media_mode = 0 ORDER BY f.name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 !=0) print "icon=\"img/tv_enc.gif\""; else print "icon=\"img/tv_fta.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'

  sqlite3 -separator "=" $PATH_DB_DAT "SELECT distinct f.name, c.ch_order, c.cas_type FROM channelinfo c, favlistinfo f WHERE f.view_flg=0 AND c.ch_id=f.ch_id AND media_mode = 1 ORDER BY f.name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 !=0) print "icon=\"img/radio_enc.gif\""; else print "icon=\"img/radio_fta.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi






# favlists
if [ "$param1" = "favlists" ] 
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT Name, FAVGRP_ID FROM FAVGRPInfo ORDER BY FAVGRP_Seq" | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" src=\"cgi-bin/xml_chantree?favlstchanls=" $2 "\"/>" }'
  echo '</tree>'  
  exit 0
fi

#favlstchanls
if [ "$param1" = "favlstchanls" ]
then
#else favlistid
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT f.name, c.ch_order, c.cas_type, c.media_mode FROM channelinfo c, favlistinfo f WHERE f.view_flg=0 AND f.favgrp_id=$param2 AND c.ch_id=f.ch_id ORDER BY f.favlist_seq ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 == 0 && $4 == 0) print "icon=\"img/tv_fta.gif\""; else if ($3 != 0 && $4 == 0) print "icon=\"img/tv_enc.gif\""; else if ($3 == 0 && $4 == 1) print "icon=\"img/radio_fta.gif\""; else if ($3 != 0 && $4 == 1) print "icon=\"img/radio_enc.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'
fi






#letters_tv
if [ "$param1" = "letters_tv" ]
then
  echo '<tree>'
#  echo '<tree text="All" src="cgi-bin/xml_chantree?allchannels" icon="img/all_tv.gif" openIcon="img/all_tv_open.gif"/>'
  echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nv\nr\nv\ns\nt\nu\nv\nw\nx\ny\nz\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" icon=\"img/all_tv.gif\" openIcon=\"img/all_tv_open.gif\" src=\"cgi-bin/xml_chantree?chanletter_tv=" $1 "\"/>" }'
  echo '</tree>'  
  exit 0
fi

#letters_radio
if [ "$param1" = "letters_radio" ]
then
  echo '<tree>'
#  echo '<tree text="All" src="cgi-bin/xml_chantree?allchannels" icon="img/all_radio.gif" openIcon="img/all_radio_open.gif"/>'
  echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nv\nr\nv\ns\nt\nu\nv\nw\nx\ny\nz\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" icon=\"img/all_radio.gif\" openIcon=\"img/all_radio_open.gif\" src=\"cgi-bin/xml_chantree?chanletter_radio=" $1 "\"/>" }'
  echo '</tree>'  
  exit 0
fi

#chanletter_tv
if [ "$param1" = "chanletter_tv" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order, cas_type FROM channelinfo WHERE hide=0 AND media_mode=0 AND name LIKE \"$param2%\" ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 ==0) print "icon=\"img/tv_fta.gif\""; else print "icon=\"img/tv_enc.gif\"";  print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi

#chanletter_radio
if [ "$param1" = "chanletter_radio" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order, cas_type FROM channelinfo WHERE hide=0 AND media_mode=1  AND name LIKE \"$param2%\" ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 ==0) print "icon=\"img/radio_fta.gif\""; else print "icon=\"img/radio_enc.gif\"";  print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi

#allchannels: and now we wait ..  or not: disabled because IE crashed with so many nodes ... hihihi
#if [ "$param1" = "allchannels" ]
#then
#  echo '<tree>'
#  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order FROM channelinfo WHERE hide=0 ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" action=\"javascript:remote_change_channel("$2")\"/>" }'
#  echo '</tree>'  
#  exit 0
#fi






#providers
if [ "$param1" = "providers" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT lower(provider_name), sdt_id FROM sdtinfo ORDER BY lower(provider_name); " | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" src=\"cgi-bin/xml_chantree?providerid=" $2 "\"/>" }'
  echo '</tree>'  
  exit 0
fi

#providerid
if [ "$param1" = "providerid" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order, cas_type, media_mode FROM channelinfo WHERE sdt_id = $param2 ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 == 0 && $4 == 0) print "icon=\"img/tv_fta.gif\""; else if ($3 != 0 && $4 == 0) print "icon=\"img/tv_enc.gif\""; else if ($3 == 0 && $4 == 1) print "icon=\"img/radio_fta.gif\""; else if ($3 != 0 && $4 == 1) print "icon=\"img/radio_enc.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi






#satalites
if [ "$param1" = "satellites" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT distinct b.name, b.sat_id FROM ChannelInfo a, SATInfo b WHERE a.SAT_ID = b.SAT_ID; " | awk -F '=' '{gsub(/&/,"\\&amp;"); print "<tree text=\"" $1 "\" src=\"cgi-bin/xml_chantree?satelliteid=" $2 "\"/>" }'
  echo '</tree>'  
  exit 0
fi

#sataliteid
if [ "$param1" = "satelliteid" ]
then
  echo '<tree>'
  echo '<tree text="All TV Channels" icon="img/all_tv.gif" openIcon="img/all_tv_open.gif" src="cgi-bin/xml_chantree?satelliteid_tv='$param2'"/>'
  echo '<tree text="All Radio Channels" icon="img/all_radio.gif" openIcon="img/all_radio_open.gif" src="cgi-bin/xml_chantree?satelliteid_radio='$param2'"/>'
  echo '</tree>'  
  exit 0
fi


#sataliteid
if [ "$param1" = "satelliteid_tv" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order, cas_type FROM channelinfo WHERE sat_id = $param2 AND media_mode = 0 ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 == 0) print "icon=\"img/tv_fta.gif\""; else print "icon=\"img/tv_enc.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi

#sataliteid
if [ "$param1" = "satelliteid_radio" ]
then
  echo '<tree>'
  sqlite3 -separator "=" $PATH_DB_DAT "SELECT name, ch_order, cas_type FROM channelinfo WHERE sat_id = $param2 AND media_mode = 1 ORDER BY name ASC; " | awk -F '=' '{gsub(/&/,"\\&amp;"); ORS=""; print "<tree text=\"" $1 "\" "; if ($3 == 0) print "icon=\"img/radio_fta.gif\""; else print "icon=\"img/radio_enc.gif\""; print " action=\"javascript:remote_change_channel("$2")""\"/>" }'
  echo '</tree>'  
  exit 0
fi

