#!/bin/sh
URLSUFFIX=".htm"

function createdir()
{
  if test ! -d "$1"; then
    mkdir -p "$1" && chmod 751 "$1"
  fi
}

function fetch()
{
  URLPREFIX="$1"
  CLASSFILE="$2"
  LISTFILE="$2.tmp"
  PATPREFIX="$3"

  if wget -q -O "$LISTFILE" "$URLPREFIX"; then
    perl -ne "if (/<a href=\"${PATPREFIX}([A-Z]{3}\d\S+)${URLSUFFIX}\">/) { print \"\$1\n\"; }" < "$LISTFILE" > "$CLASSFILE"
    echo -n "* Fetched classfile $CLASSFILE: "
    cat "$CLASSFILE" | wc -l
  fi
}


function parse()
{
  # Create cache directories, if they do not exist
  OLDCACHEDIR="${CACHEDIR}old/"
  createdir "${OLDCACHEDIR}"
  createdir "${CACHEDIR}"

  URLPREFIX="$1"
  CLASSFILE="$2"
  PATPREFIX="$3"

  echo "<? \$origBaseURI = \"${URLPREFIX}${PATPREFIX}\"; \$origBaseExt = \".htm\"; ?>" > "${CACHEDIR}baseuri.data";

  test -e "$CLASSFILE" || return 0
  
  echo "* Processing $CLASSFILE ..."
  cat "$CLASSFILE" | while read i; do
    parse=no
    INFILE="${CACHEDIR}${i}.html"
    ONFILE="${OLDCACHEDIR}${i}.html"
    DATAFILE="${i}.data"
    wget -q -O "${INFILE}.new" "${URLPREFIX}${PATPREFIX}${i}${URLSUFFIX}"

    if test -e "${INFILE}.new"; then
      # New data fetched, does old file exist?
      if test -e "$INFILE"; then
        # Yes, do a diff
        if ! diff -u "$INFILE" "$INFILE.new" > "$INFILE.diff"; then
          # There were differences, do a parse
          parse=yes
          mv "$INFILE" "$ONFILE" && \
          mv "$INFILE.new" "$INFILE"
        else
          # No changes, apparently .. remove the new one
          rm -f "$INFILE.new" "$INFILE.diff"
        fi
      else
        # No old file, parse new data
        mv "$INFILE.new" "$INFILE"
        parse=yes
      fi
    fi

    # No new file fetched, does datafile exist?
    if test ! -e "${CACHEDIR}${DATAFILE}"; then
      # No, try to parse it if old file input exists
      parse=yes
    fi

    # Parsing of old data requested?
    if test "x$parse" = "xyes" -a -e "$INFILE"; then
      if test -e "${CACHEDIR}${DATAFILE}"; then
        mv "${CACHEDIR}${DATAFILE}" "${OLDCACHEDIR}${DATAFILE}"
      fi
      echo "Parsing $i"
      perl parsedata.pl -php "$INFILE" -o "${CACHEDIR}${DATAFILE}"
      #perl parsedata.pl -xml "$INFILE" -o "${CACHEDIR}${i}.xml"
    fi
  done
}

# Fix permissions
chmod 751 . && chmod 644 *.php *.css && chmod 600 coursecache.txt classes.txt classes_next.txt

CACHEDIR="cache/"
fetch "http://www.oamk.fi/tyojarjestykset/otek/luokat/" "classes.txt" "OR_"
fetch "http://www.oamk.fi/~heikkim/Luhti5/Ryhm%84t/" "classes.txt" "Ryh._"
parse "http://www.oamk.fi/tyojarjestykset/otek/luokat/" "classes.txt" "OR_"


CACHEDIR="cache-next/"
fetch "http://www.oamk.fi/~heikkim/Luhti5/Ryhm%84t/" "classes_next.txt" "Ryh._"
parse "http://www.oamk.fi/~heikkim/Luhti5/Ryhm%84t/" "classes_next.txt" "Ryh._"


# http://www.oamk.fi/~heikkim/riihi[1-5]/
# http://www.oamk.fi/~heikkim/Luhti[1-5]/
