#!/usr/bin/env bash
#
# Copyright © 2014 Jesse 'Jeaye' Wilkerson
# See licensing in LICENSE file, or at:
#   http://www.opensource.org/licenses/MIT
#
# File: do_generate
# Author: Jesse 'Jeaye' Wilkerson

# XXX: Do not call this script directly: use `make generate`
set -o nounset

function check_existence 
{
  printf "Checking for $1... "
  which $1 > /dev/null 2>&1
  exists=$?
  if [ "$exists" -eq "0" ];
  then
    echo "found" 1>&2
  else
    echo "not found; make sure it's in PATH" 1>&2
    echo "Error: manual generation will not work"
    exit 1
  fi
}

check_existence "elinks"
check_existence "gzip"

echo "Generating from $reference to $tmp_man"
echo "  Removing previous files"
rm -f $tmp_man/*.3
echo "  Parsing documentation on $threads threads"
find $reference -print0 | xargs -0 -n 1 -P $threads -I % \
./bin/stdman -d $tmp_man % > /dev/null 2>&1

echo "  Removing unwanted pages"
for file in $(find $tmp_man -name '*.3' | grep -v "std::") ; do
  rm -f $file;
done

echo "  Creating links for common typedefs"
./do_link $tmp_man
echo "Done generating"
