#!/bin/sh
#
# Network Stream Setting
#
# runlevels: geexbox, debug, configure

CONF=/etc/netstream
TMP=/tmp/streamtmp

[ -f /etc/network -a -f $CONF ] || exit 1
. /etc/network

# Disable streams if not utilized.
test "$SHOUTCAST" = "no" -a "$SHOUTCASTTV" = "no" -a "$ICECAST" = "no" -a `grep -c ^STREAM $CONF` -eq 0 -a `grep -c ^EXTM3U $CONF` -eq 0 -a `grep -c ^EXTPLS $CONF` -eq 0 && exit 1

echo "### Network Stream Configuration ###"

DAY=`date '+%d'`
MONTH=`date '+%m'`
YEAR=`date '+%Y'`
year=`date '+%y'`
BASEDIR="/mnt/Network Streams"

# network stream
DIR="$BASEDIR"
for I in `grep "^STREAM" $CONF | sed "s# #%20#g"`; do
  [ ! -d "$DIR" ] && mkdir -p "$DIR"
  URL="$(echo $I |sed -e "s#^STREAM=\"\(.*\)\":.*#\1#" -e "s#%DD#$DAY#g" -e "s#%MM#$MONTH#g" -e "s#%YY#$YEAR#g" -e "s#%yy#$year#g")"
  FILE="$(echo $I |sed -e "s#^STREAM=\"\(.*\)\":\(.*\)#\2#" -e "s#%20# #g" -e "s#/# #g")"
  echo "$URL" > "$DIR/$FILE.pls"
done

SC_TUNE=$(sed -n -e "s#^SHOUTCASTTUNE_URI=\"\(.*\)\"#\1#p" $CONF)
(
  while [ ! -f /var/ifup ]; do
    sleep 1
  done 
  if [ "$ICECAST" = "yes" ]; then
    # icecast radio
    unset FILE URL
    rm -f $TMP
    wget -q -T $TIMEOUT -t $TRIES -O $TMP `sed -n -e "s#^ICECAST_URI=\"\(.*\)\"#\1#p" $CONF` 2>/dev/null || echo "" > $TMP
    DIR="$BASEDIR/Icecast Radio"
    for I in `sed -n -e "s#.*<server_name>\(.*\)</server_name>.*#\1#p" -e "s#.*<listen_url>\(.*\)</listen_url>.*#\1#p" $TMP | sed "s# #%20#g"`; do
      [ ! -d "$DIR" ] && mkdir -p "$DIR"
      [ `echo $I | grep -c -e "://"` -gt 0 ] && URL="$I"
      [ `echo $I | grep -c -e "://"` -eq 0 ] && FILE="$(echo $I | sed -e "s#%20# #g" -e "s#/# #g")"
      if [ -n "$FILE" -a -n "$URL" ]; then
        echo "$URL" > "$DIR/$FILE.pls"
        unset FILE URL
      fi
    done
  fi

  if [ "$SHOUTCAST" = "yes" ]; then
    # shoutcast radio
    for GENRE in `sed -n -e "s#^SHOUTCAST_GENRE=\"\(.*\)\"#\1#p" $CONF`; do
      rm -f $TMP
      wget -q -T $TIMEOUT -t $TRIES -O $TMP `sed -n -e "s#^SHOUTCAST_URI=\"\(.*\)\"#\1$GENRE#p" $CONF` 2>/dev/null || echo "" > $TMP
      DIR="$BASEDIR/SHOUTcast Radio/`echo $GENRE | sed -e "s#/#-#g" -e "s#%20# #g" -e "s#%26#&#g"`"
      for I in `sed "s#\&amp;#\&#g" $TMP |sed -n "s#.*name=\"\([^\"]*\)\".*id=\"\([0-9]*\)\".*#\1_TAG_\2#p"| sed -e "s#\ #_#g"`; do
        [ ! -d "$DIR" ] && mkdir -p "$DIR"
        FILE="$(echo $I | sed -e "s#\(.*\)_TAG_[0-9]*#\1#" | sed -e "s#[_/]# #g" -e "s#://##g")"
        URL="$SC_TUNE?id=$(echo $I | sed -e "s#.*_TAG_\([0-9]*\)#\1#")"
        echo "$URL" > "$DIR/$FILE.pls"
      done
    done
  fi

  if [ "$SHOUTCASTTV" = "yes" ]; then
    # SHOUTcast TV content filter
    if [ -n "$BLACKLIST" ]; then
      BL="-v"
      for I in $BLACKLIST; do
        BL="$BL -e genre=\"[^\"]*$I[^\"]*\""
      done
    else # Accept all Genre
      BL="-e genre=\"[^\"]*\""
    fi

    if [ -n "$WHITELIST" ]; then
      for I in $WHITELIST; do
        WL="$WL -e genre=\"[^\"]*$I[^\"]*\""
      done
    else # Accept all Genre
      WL="-e genre=\"[^\"]*\""
    fi

    # SHOUTcast TV
    rm -f $TMP
    wget -q -T $TIMEOUT -t $TRIES -O $TMP `sed -n -e "s#^SHOUTCASTTV_URI=\"\(.*\)\"#\1#p" $CONF` 2>/dev/null || echo "" > $TMP
    DIR="$BASEDIR/SHOUTcast TV"
    for I in `grep -i $BL $TMP | grep -i $WL | sed "s#\&amp;#\&#g" | sed -n "s#.*name=\"\([^\"]*\)\".*id=\"\([0-9]*\)\".*#\1_TAG_\2#p"|sed "s#\ #_#g"`; do
      [ ! -d "$DIR" ] && mkdir -p "$DIR"
      FILE="$(echo $I | sed -e "s#\(.*\)_TAG_[0-9]*#\1#" | sed -e "s#[_/]# #g" -e "s#://##g")"
      URL="$SC_TUNE?id=$(echo $I | sed -e "s#.*_TAG_\(.*\)#\1#")"
      echo "$URL" > "$DIR/$FILE.pls"
    done
  fi

  # extended m3u playlists
  unset FILE URL
  for J in `grep "^EXTM3U" $CONF | sed "s# #%20#g"`; do
    DIR="$BASEDIR/$(echo $J | sed -e "s#^EXTM3U=\".*\":\(.*\)#\1#" -e "s#%20# #g")"
    M3UURL="$(echo $J | sed "s#^EXTM3U=\"\(.*\)\":.*#\1#")"
    rm -f $TMP
    wget -q -T $TIMEOUT -t $TRIES -O $TMP "$M3UURL" 2>/dev/null || continue
    for I in `sed -n -e "s/#EXTINF:[-]*[0-9]*,\(.*\)/\1/p" -e "s#\(.*\)://\(.*\)#\1://\2#p" $TMP | sed "s# #%20#g"`; do
      [ -n "$FILE" ] || FILE="$(echo $I | sed -e "s#%20# #g" -e "s#/# #g")"
      [ -n "$URL" ] || URL="$(echo $I | sed -n -e "s#\(.*\)://\(.*\)#\1://\2#p" | sed -e "s#%DD#$DAY#g" -e "s#%MM#$MONTH#g" -e "s#%YY#$YEAR#g" -e "s#%yy#$year#g")"
      if [ -n "$FILE" -a -n "$URL" ]; then
        [ ! -d "$DIR" ] && mkdir -p "$DIR"
        echo "$URL" > "$DIR/$FILE.pls"
        unset FILE URL
      fi
    done
  done

  # extended pls playlists
  for J in `grep "^EXTPLS" $CONF | sed "s# #%20#g"`; do
    DIR="$BASEDIR/$(echo $J | sed -e "s#^EXTPLS=\".*\":\(.*\)#\1#" -e "s#%20# #g")"
    [ ! -d "$DIR" ] && mkdir -p "$DIR"
    EXTPLSURL="$(echo $J | sed "s#^EXTPLS=\"\(.*\)\":.*#\1#")"
    FNUM=0; TNUM=0
    rm -f $TMP
    wget -q -T $TIMEOUT -t $TRIES -O $TMP "$EXTPLSURL" 2>/dev/null || continue
    cat $TMP | while read LINE; do
      # strip unwanted chars
      LINE=`echo $LINE | sed -e "s%\]\[%_%g" -e "s%^M%%g" | sed -e "s%\]%%g" -e "s%\[%%g"`
      [ `echo $LINE | grep -c -i "title[0-9]*="` -eq 1 ] && \
        TITLE=`echo $LINE | sed "s%[Tt]itle[0-9]*=\(.*\)%\1%"` && \
        TNUM=`echo $LINE |sed "s%[Tt]itle\([0-9]*\)=.*%\1%"`
      [ `echo $LINE | grep -c -i "file[0-9]*="` -eq 1 ] && \
        FILE=`echo $LINE | sed "s%[Ff]ile[0-9]*=\(.*\)%\1%"` && \
        FNUM=`echo $LINE |sed "s%[Ff]ile\([0-9]*\)=.*%\1%"`
     [ $FNUM -eq $TNUM -a $FNUM -gt 0 ] && echo $FILE > "$DIR/$TITLE.pls"
    done
  done
)&

rm -f $TMP

exit 0
