#!/bin/sh

echo "ragnarok output file splitter -- written by negative, fixed by lcamtuf"

if [ "$2" = "" ]; then
  echo "Usage: $0 ragnarok_outfile output_dir"
  echo
  exit 1
fi

WHEREIS=./spliter.pl

if [ ! -x spliter.pl ]; then
  WHEREIS=`which spliter.pl 2>/dev/null`
  if [ "$WHEREIS" = "" ]; then
    echo "Error: 'spliter.pl' must be in current directory or in path."
    echo
    exit 1
  fi
fi

$WHEREIS $1 $2

if [ ! -d $2 ]; then
  echo "Error: no output generated."
  echo
  exit 1
fi

cd $2

RULE='s/#A1/index.html/g;s/#A2/calls.html/g;s/#A3/params.html/g;s/#A4/buffers.html/g;s/#A5/io.html/g;s/#A6/raw.html/g;s/#f/calls.html#f/g;s/#d/io.html#d/g;s/#l/raw.html#l/g;s/#S/params.html#S/g;'

# index.html
cat header.txt > index.html
cat index.txt | sed $RULE >> index.html
cat footer.txt >> index.html

# calls.html
cat header.txt > calls.html
cat calls.txt | sed $RULE >> calls.html
cat footer.txt >> calls.html

# params.html
cat header.txt > params.html
cat params.txt | sed $RULE >> params.html
cat footer.txt >> params.html

# buffers.html
cat header.txt > buffers.html
cat buffers.txt | sed $RULE >> buffers.html
cat footer.txt >> buffers.html

# io.html
cat header.txt > io.html
cat io.txt | sed $RULE >> io.html
cat footer.txt >> io.html

# raw.html
cat header.txt > raw.html
cat raw.txt | sed $RULE >> raw.html
cat footer.txt >> raw.html

# delete the unused
rm -f raw.txt footer.txt header.txt io.txt buffers.txt params.txt calls.txt index.txt


