#! /bin/sh
# Utility script to generate html docs for all ports.
#
#   Copyright 2012 Free Software Foundation
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.
#

# Run this script in the src/cgen directory.

# Exit on any error.
set -e

# For debugging.
set -x

# ??? Some are missing, need to add them after some research.
#../cpu/m32c.cpu
#../cpu/xc16x.cpu
archs="
cpu/fr30.cpu
cpu/arm.cpu
../cpu/frv.cpu
cpu/i960.cpu
cpu/ip2k.cpu
../cpu/iq2000.cpu
../cpu/lm32.cpu
../cpu/m32r.cpu
cpu/mep.cpu
../cpu/mt.cpu
cpu/openrisc.cpu
cpu/sh.cpu
cpu/xstormy16.cpu
"

if [ ! -f sim.scm ]
then
    echo "Not in the src/cgen directory." >& 2
    exit 1
fi

export builddir=tmp-doc

rm -rf $builddir
mkdir $builddir

export cgendir=`pwd`

(
  set -e
  set -x
  cd $builddir
  $cgendir/configure --prefix /tmp/junk --target m32r-elf

  for a in $archs
  do
    archfile=../$a
    arch=$(basename $archfile .cpu)

    case $arch in
    arm)
      make html ARCH=$arch ARCHFILE=$archfile ISAS=arm INSN_FILE_NAME=arm-arm-insn.html
      mv arm.html arm-arm.html
      mv arm-insn.html arm-arm-insn.html
      make html ARCH=$arch ARCHFILE=$archfile ISAS=thumb INSN_FILE_NAME=arm-thumb-insn.html
      mv arm.html arm-thumb.html
      mv arm-insn.html arm-thumb-insn.html
      ;;
    frv)
      make html ARCH=$arch ARCHFILE=$archfile MACHS="frv,simple,tomcat,fr400" INSN_FILE_NAME=frv-1-insn.html
      mv frv.html frv-1.html
      mv frv-insn.html frv-1-insn.html
      make html ARCH=$arch ARCHFILE=$archfile MACHS="fr500" INSN_FILE_NAME=frv-2-insn.html
      mv frv.html frv-2.html
      mv frv-insn.html frv-2-insn.html
      make html ARCH=$arch ARCHFILE=$archfile MACHS="fr550" INSN_FILE_NAME=frv-3-insn.html
      mv frv.html frv-3.html
      mv frv-insn.html frv-3-insn.html
      ;;
    *)
      make html ARCH=$arch ARCHFILE=$archfile
      ;;
    esac
  done
)

(
  set -e
  set -x
  cd $builddir

  rm -f index.html

  cat > index.html <<EOF
<html>
<head>
<title>CGEN Machine Generated Documentation</title>
</head>
<body>

<h1>Machine Generated Documentation</h1>

<ul>
EOF

  lower="abcdefghijklmnopqrstuvwxyz"
  upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

  for html in *.html
  do
    name=""

    case $html in
    *-insn.html) ;;
    index.html) ;;
    arm-arm.*) name="ARM" ;;
    arm-thumb.*) name="ARM Thumb" ;;
    frv-1.*) name="FRV FR400" ;;
    frv-2.*) name="FRV FR500" ;;
    openrisc.*) name="Openrisc" ;;
    xstormy16.*) name="Xstormy16" ;;
    *) name=`echo ${html/.html} | tr $lower $upper` ;;
    esac

    if [ "$name" != "" ]
    then
      echo "<li><a href=\"$html\">$name</a>" >> index.html
    fi
  done

  cat >> index.html <<EOF
</ul>

</body>
</html>
EOF
)
