# Generate an entry for media.html from its source in rec format.

# Called from avgen and add_entry.

# Public domain.
# Last updated 2024-01-27.
# Please report bugs to thg@gnu.org or ineiev@gnu.org.


function convert_entry () {

  tmp=$(make_temp 'convert_entry')

  # Remove any linefeed between field name and data;
  # remove empty fields (except By and Description),
  # and blank lines that would interfere with sorting.
  sed '/^\(License\|Note\|Files\|Description\):$/ { N; s,\n+,, }' $1 > $tmp
  sed -n '/^By:$/p; /^Description:$/p; /^[A-Za-z]\+:$/d; /^$/d; p' $tmp > $1

  n=1
  for f in Id Year Lang Type Length Media; do
    data[n]=$(grep "^${f}:" $1 | sed "s,^${f}: ,,")
    n=$(( $n + 1 ))
  done
  language=$(grep "^Language: " $1 |
    sed 's,^.\+: \([-a-zA-Z]\+\)\( .*\)\?$,\1,')

  id=${data[1]}
  year=${data[2]}
  lang=${data[3]}
  type=${data[4]}
  tlength=${data[5]}
  media=${data[6]}; media=${media// /|}

  case "$tlength" in
      long ) Length="&gt;45min";;
    medium ) Length="10-45min";;
     short ) Length="&lt;10min";;
         * ) Length="unknown";;
  esac

  details='<details>'
  grep -q '^Open: [y|Y]' $1 &&
    details='<details open="open">'

  intro=''
  grep -q '^Introductory: [y|Y]' $1 &&
    intro='<span class="introductory">*</span>\n  '

  moreinfo_ssi=''
  grep -q '^MoreInfo:' $1 &&
    moreinfo_ssi='<!--#echo encoding=\"none\" var=\"MOREINFO\" -->'

  note_ssi=''
  grep -q '^Note:' $1 &&
    note_ssi='<!--#echo encoding=\"none\" var=\"NOTE\" -->'

  description_ssi=''
  grep -q '^Description: ' $1 &&
    description_ssi='
<div class=\"emph-box\">\n<!--#echo encoding=\"none\" var=\"DESCRIPTION\" --></div>\n'

  echo "${0##*\/}: Converting $id to html..."

  if play_link=$(grep "<a class='play'" $1); then
     play_link="${play_link/*</<}Play</a>"
  else
     echo '*** No play link'
  fi

  sed '/^Id:/,/^Media:/d
       s,^Title: ,     <!--#set var="TITLE" value=",
       s,^MoreInfo: ," --><!--#set var="MOREINFO" value=",
       /^By:/i\
" --><!--#set var="METADATA" value="
       /^Duration: /a\
" --><!--#set var="LICENSING" value="
       s,^Note: ," --><!--#set var="NOTE" value=",
       s,^Files: ," --><!--#set var="FILES" value=",
       s,^Description:," --><!--#set var="DESCRIPTION" value=",
      ' $1 > $2

  printf "\" --><!--#if expr=\"!\$YEAR || \$YEAR = $year\"\n\
  --><!--#if expr=\"!\$LANG || \$LANG = $lang\"\n\
  --><!--#if expr=\"!\$TYPE || \$TYPE = $type\"\n\
  --><!--#if expr=\"!\$LENGTH || \$LENGTH = $tlength\"\n\
  --><!--#if expr=\"!\$MEDIA || \$MEDIA = /($media)/\"\n\
  --><!--#if expr=\"!\$SEARCH || '\$TITLE \$MOREINFO \$METADATA \$DESCRIPTION' = /(?i)\$SEARCH/\" -->
<div class=\"entry\" id=\"$id\">\n\
${details}\n\
<summary>\n  ${play_link}\n\
  <span>${intro}<b><!--#echo encoding=\"none\" var=\"TITLE\" -->  </b>${moreinfo_ssi}\
</span></summary>\n\
<p class=\"metadata\"><!--#echo encoding=\"none\" var=\"METADATA\" --></p>\n\
<p class=\"licensing\"><!--#echo encoding=\"none\" var=\"LICENSING\" --></p>\n\
${note_ssi}\
<!--#echo encoding=\"none\" var=\"FILES\" -->\
${description_ssi}\
<hr class=\"thin\" />\n\
</details>\n\
</div>\n\
<!--#endif --><!--#endif --><!--#endif --><!--#endif --><!--#endif --><!--#endif -->\n\n" >> $2

  sed ':a { N; s,\n+ ,+ ,g; ba }' $2 |
  sed '
    /^By:$/d
    /set var="LICENSING"/,/License:/ {
      s,^Copyright: ,  <i>Copyright</i> \&copy; ,
      /<i>Copyright/ s,$,<br />,
    }
    /set var="METADATA"/,/^License/ { /Copyright/! s,\([^ :]\+:\)\(.*\),  <i>\1</i>\&nbsp;\2, }
    /set var="METADATA"/,/<i>Language:<\/i>/ { /METADATA/! s,$,<br />, }
    s,+ ,\n,g
    s,<i>Hosts:</i>,<i>Host(s):</i>,
    s,<i>ThanksTo:</i>,<i>Thanks to:</i>,
    s,<i>RecordedBy:</i>,<i>Recorded by:</i>,
    s,>round-table</a>,>round table</a>,
    ' |
  sed "
    /<i>Language:/ {
      /nbsp; English/! s,\(<i>Language:[^ ]\+ [^ <]\+\),<span class='lang'>\1</span>,
    }
    /<i>Duration:/ {
      s, *\(.\+\),  <span class='duration'>\1</span>,
    }" > $tmp && mv $tmp $2
}
