#!/bin/bash

# bdfans.sh
# Parsing www.bd-fans.com

function bdfans_list_download () {
  download "http://www.bd-fans.com/RnD.html"
}

function bdfans_list_sectionname () { # args: line
  
}

function bdfans_list_parse () {
  local path begin end
  path="$tmp_dir/RnD.html"
  
  # first, find the first occurence of <div class="tabletitle">
  begin=`grep -n -m 1 '<div class="tabletitle">' "$path"`
  begin=${begin%%:*}
  end=`grep -n -m 1 '<a name="Graphics">' "$path"`
  end=${end%%:*}
  : $((end--))
  
  # prepare sed script
  get_tmp_filename
  cat > $file <<HEREDOC
# don't use lines out of range $begin-$end
$begin,$end! d

# remove beginning spaces, <br> tags and empty lines
s/<br>//
s/^ *//
/^$/d

# remove attributes from <td> and <th> (they aren't needed)
s/<td [^>]*>/<td>/
s/<th [^>]*>/<th>/

# remove line wraps
/\w$/ {
  \$b
  :b
  N
  /\n\w/ {
    s/\n/ /
    bb
  }
  P; D
}
HEREDOC
  
  # remove blank lines and lines out of range $begin-$end
#  content=`sed -nf "$file" "$path"`
  sed -f "$file" "$path"
#  cat $file
}

