#!/bin/sh
#
# rev 1.0 11/04/96
# rev 1.1 20/05/96
#
# Converts an SGD database (.dat) file to a html file with ftp links to all the
# games in the database.
# It should be run only on a Linux system with the DOS drives mounted somewhere
# excessible from anonymous ftp. This point is named in the variables
# FTPxxxxx, where FTPDEFS tells how many are mounted. You must include all the
# drives that have been named in your SGD database (so all drives with spectrum
# software stuffed on it).
# The root directory of the anonymous ftp tree must be named in the variabele
# HOMEFTP. You probably won't have to change this variable.
# It expects that all software is zipped (extension .zip) in a subdirectory
# 'zipped' of the directory named in SGD.INI.
# All software not found (has directory link 255) is given the path name as
# defined in the variable MISSING (plus 'zipped' after it).
#
# The variable definition FTPBASEx is really simple:
# <DOS driveletter><Anonymous FTP link>
# You must also adjust this source where these variables are written to the
# temporary file.
# The game paths are automatically converted to lower case and '\' (DOS)
# directory seperators are converted to '/'.
#
# Anchors are inserted when the first letter for a game name changes.
# For a sample of a xxx.header and xxx.trailer file (the minumum!), look at the
# files sample.header/sample.trailer.
#
# The syntax is really simple: dattohtml <0/1> <0/1> <databasename> <hostname>
# where the first <0/1> is 1 if you want the links to the game
# and the second <0/1> is 1 if you want the .scr and .txt checks
#
# Using the checks makes the process take forever, but the result is worth it.
# Also, (PC) files with a '&' character in it create an errormessage.
#
# Copyright (R) 1996 ThunderWare Research Center
#

MISSING=/pub/dose/z80/progs/games/missing         # No trailing slash!!!
FTPDEFS=3
FTPBASEC=C/pub/dosc/                              # Need trailing slashes!!!
FTPBASED=D/pub/dosd/
FTPBASEE=E/pub/dose/

HOMEFTP=/home/ftp                                 # Root of anonymous ftp tree

ADDLINKS=1
ADDCHECKS=1

if [ ${1} ]
then
  if [ ${1} = 0 ]
  then
    ADDLINKS=0
  else
    if [ ${1} != 1 ]
    then
      echo Forgot to set links parameter!
      exit
    fi
  fi
fi

if [ ${2} ]
then
  if [ ${2} = 0 ]
  then
    ADDCHECKS=0
  else
    if [ ${2} != 1 ]
    then
      echo Forgot to set check parameter!
      exit
    fi
  fi
fi

if [ ${3} ]
then
  BASE=`basename ${3}`
else
  BASE=games
fi

if [ ${4} ]
then
  HOSTNAME=${4}
else
  HOSTNAME=`hostname`.`domainname`
fi

SGDPATH=/dose/z80/sgd

TMPFILE=/tmp/${BASE}.tmp
INIFILE=${SGDPATH}/sgd.ini
DATFILE=${SGDPATH}/${BASE}.dat
RESFILE=${BASE}${ADDLINKS}.html
HEADER=${BASE}.header
TRAILER=${BASE}.trailer

if [ ! -f ${DATFILE} ]
then
  echo "File ${DATFILE} does not exist!"
  exit 1
fi

echo ${ADDLINKS} ${ADDCHECKS} > ${TMPFILE}
echo ${MISSING} >> ${TMPFILE}
echo ${HOMEFTP} >> ${TMPFILE}
echo ${FTPDEFS} >> ${TMPFILE}
echo ${FTPBASEC} >> ${TMPFILE}
echo ${FTPBASED} >> ${TMPFILE}
echo ${FTPBASEE} >> ${TMPFILE}
echo ${HOSTNAME} >> ${TMPFILE}
grep GameDir ${INIFILE} | wc -l >> ${TMPFILE}
grep GameDir ${INIFILE} >> ${TMPFILE}
cat ${DATFILE} | wc -l >> ${TMPFILE}
cat ${DATFILE} >> ${TMPFILE}
cp ${HEADER} ${RESFILE}

cat ${TMPFILE} | awk '
BEGIN { Cnt  = 0
        Anchor="0"
	      print ("<A NAME=\"GMNUM\">") }

      function ExpandEsc(OldName,     NewName, Counter)
      {
        NewName = ""
        Counter = 0
        while (++ Counter <= length (OldName))
        {
	        if (substr (OldName, Counter, 1) == "(")
	          NewName = NewName "\\("
	        else if (substr (OldName, Counter, 1) == ")")
	          NewName = NewName "\\)"
	        else if (substr (OldName, Counter, 1) == "{")
	          NewName = NewName "\\{"
	        else if (substr (OldName, Counter, 1) == "}")
	          NewName = NewName "\\}"
	        else if (substr (OldName, Counter, 1) == "&")
	          NewName = NewName "\\&"
	        else if (substr (OldName, Counter, 1) == "!")
	          NewName = NewName "\\!"
	        else
	          NewName = NewName substr (OldName, Counter, 1)
	      }
	      return (NewName)
	    }

      { if (NR == 1) { AddLinks = $1 ; AddChecks = $2 }
        else if (NR == 2) { GamePaths[255] = $0 }
        else if (NR == 3) { FtpRoot = $0 }
        else if (NR == 4) { FtpBases = $0 }
	      else if (NR <= FtpBases + 4)
        { FtpBase[substr ($0, 1, 1)] = substr ($0, 2, length ($0) - 2) }
        else if (NR == FtpBases + 5) { Host = $0 }
        else if (NR == FtpBases + 6) { End = $1 }
	      else if (NR <= FtpBases + End + 6)
	      { if (substr ($0, 1, 1) != "#")
            if (substr ($3, 2, 1) == ":")                       # PC directory ?
            { UnixPath = tolower (substr ($3, 3, length ($3) - 3))
              gsub ( /\\/, "/", UnixPath)
	            GamePaths[Cnt ++] = FtpBase[substr ($3, 1, 1)] UnixPath }}
	      else if (NR == FtpBases + End + 7) { TotalEntries = $1 }
	      else
        { GameName  = substr ($0,   1, 36)
	        Year      = substr ($0,  38,  4)
	        Publisher = substr ($0,  43, 36)
	        Memory    = substr ($0,  80,  3)
	        Players   = substr ($0,  84,  1)
	        Together  = substr ($0,  86,  1)
	        Sticks    = substr ($0,  88,  5)
	        PCName    = substr ($0,  94,  8)
	        PCExt     = substr ($0, 103,  3)
	        Type      = substr ($0, 107,  7)
	        PathIndex = substr ($0, 115,  3)
	        Size      = substr ($0, 119,  7)
	        HasScreen = substr ($0, 127,  1)
	        Improve   = substr ($0, 129,  1)
	        Spaces = "                                        "
	        Cnt = index (PCName, " ")                     # Remove trailing spaces
	        if (Cnt == 0)
	          Cnt = length (PCName)
          else
	          Cnt --
          ShrPCName = substr (PCName, 1, Cnt)
	        Cnt = length (GameName)
	        EndOfName = Cnt + 1
	        if (substr (GameName, Cnt, 1) == ")" && substr (GameName, Cnt - 1, 1) != ".")
	        { while (substr (GameName, -- Cnt, 1) != "(") ;
	          EndOfName = Cnt ; Cnt -- }
	        while (substr (GameName, Cnt, 1) == " ")
	          Cnt --
          ShrGameName = substr (GameName, 1, Cnt)
	        FillUp = substr (Spaces, 1, EndOfName - Cnt)
          NewAnchor = substr (GameName, 1, 1)
          if (NewAnchor != "0" && NewAnchor != "1" && NewAnchor != "2" && \
              NewAnchor != "3" && NewAnchor != "4" && NewAnchor != "5" && \
              NewAnchor != "6" && NewAnchor != "7" && NewAnchor != "8" && \
              NewAnchor != "9")
            if (NewAnchor != Anchor)
            {
              Anchor = NewAnchor
              print ("")
              print ("<HR><A NAME=\"GM" Anchor "\">")
            }
          GPath = GamePaths[int (PathIndex)]
          LName = tolower (ShrPCName)
          if (AddChecks == 1)
          {
            HasScrShot = " "
            HasInfo = " "
            CmdLine = ExpandEsc(FtpRoot GPath "/scrshot/" LName ".scr")
            if (system ("./texists " CmdLine))
              HasScrShot = "Y"
            CmdLine = ExpandEsc(FtpRoot GPath "/info/" LName ".txt")
            if (system ("./texists " CmdLine))
              HasInfo = "Y"
	          if (AddLinks == 1)
	            print ("<A HREF=\"ftp://" Host GPath "/zipped/" LName ".zip\">" \
	                   ShrGameName "</A>" FillUp, \
	                   substr (GameName, EndOfName, 37 - EndOfName), Year, \
		                 Publisher, Memory, Type, HasScreen, HasScrShot, HasInfo, \
		                 PCExt, Size, Improve)
	          else
	            print (ShrGameName FillUp, substr (GameName, EndOfName, \
	                   37 - EndOfName), Year, Publisher, Memory, Type, HasScreen, \
	                   HasScrShot, HasInfo, PCExt, Size, Improve)
	        }
	        else if (AddLinks == 1)
	          print ("<A HREF=\"ftp://" Host GPath "/zipped/" LName ".zip\">" \
	                 ShrGameName "</A>" FillUp, \
	                 substr (GameName, EndOfName, 37 - EndOfName), Year, \
		               Publisher, Memory, Type, HasScreen, PCExt, Size, Improve)
	        else
	          print (ShrGameName FillUp, substr (GameName, EndOfName, \
	                 37 - EndOfName), Year, Publisher, Memory, Type, HasScreen, \
	                 PCExt, Size, Improve)
	      }}

      END { print ("")
            print ("<HR>")
            print ("<CENTER>Total number of entries: " TotalEntries "</CENTER>")
          }' >> ${RESFILE}
cat ${TRAILER} >> ${RESFILE}

rm -f ${TMPFILE}
