#!/bin/sh
# This file is part of GNU OrgaDoc.
#
# GNU OrgaDoc 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.
#
# GNU OrgaDoc 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. If not, see http://www.gnu.org/licenses/.

##
## add_docs.sh
## Login : <speedblue@morpheus>
## Started on  Fri Dec 12 15:18:49 2003 Julien Lemoine
## $Id: orgadoc_init_readmes,v 2.0 2017/04/27 20:32:56 atsb Exp $
## 
## Copyright (C) 2003 Julien Lemoine
## This program 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 2 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; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

ORGADOC_DEBUG=0
ORGADOC_SLEEP=0

help()
{
  echo "OrgaDoc Init Readme (Try  to generate readme.xml files for orgadoc"
  echo "                     automaticaly from documentation files (.pdf, .ps, ...)"
  echo
  echo "Usage: $0 [OPTION] directory"
  echo
  echo "Options:"
  echo -e "  -d, --debug\t\tTurn Debug on"
  echo -e "  -v, --version\t\tOutput version information and exit"
  echo -e "  -h, --help\t\tDisplay this help and exit"
}

version()
{
  echo "OrgaDoc Init Readmes"
  echo "Version : 0.9"
  echo
  echo "Copyright 2002-2004 Julien Lemoine <speedblue@happycoders.org>"
  echo "Copyright (C) 2017 Adam Bilbrough <abilbrou@gmail.com>"
  echo
  echo "Report bugs on the OrgaDoc Savannah project page."
}

options()
{
  case "$1" in
      -s)
	  ORGADOC_SLEEP=1
	  ;;
      -d|--debug)
	  ORGADOC_DEBUG=1
	  ;;
      -h|--help)
	  help
	  exit 0
	  ;;
      -v|--version)
	  version
	  exit 0
	  ;;
      *)
	  if [ "$1" != "" ]; then
            if [ "$ORGADOC_DIR" = "" ]; then
	      ORGADOC_DIR=$1;
	    else
	      echo "Warning: unused option [$1]"
	    fi
          fi
	  ;;
  esac
}

options $1 $2
while shift; do 
  options $1 $2;
done

if [ "$ORGADOC_DIR" = "" ]; then
  echo "Error: You must give a directory"
  echo
  help
  exit 1
fi

PATTERNS='-i -e .pdf$ -e .doc$ -e .ps$ -e .ps.gz$
          -e .html$ -e .htm$ -e .ps.Z$'
SUBDIRS=`find $ORGADOC_DIR -maxdepth 1 -type d`
FILES=`find $ORGADOC_DIR -maxdepth 1 -type f`

echo "Entering directory [$ORGADOC_DIR]"
if test -f $ORGADOC_DIR/readme.xml; then
  tempfile=`tempfile`
  cat $ORGADOC_DIR/readme.xml | (
    read lineb
    while read line; do
      echo $lineb >> $tempfile
      lineb=$line
    done
  )
  cp $tempfile $ORGADOC_DIR/readme.xml
  rm -f $tempfile
fi
for file in $FILES; do
  if echo $file | grep $PATTERNS > /dev/null; then
    if ! test -f $ORGADOC_DIR/readme.xml; then
      echo '<?xml version="1.0" encoding="ISO-8859-1"?>' >> $ORGADOC_DIR/readme.xml;
      echo "<readme>" >> $ORGADOC_DIR/readme.xml;
    fi
    echo "<document>" >> $ORGADOC_DIR/readme.xml;
    FILE=`echo $file | sed "s:[^/]*/::g"`
    case $file in
      *.pdf)
	    if [ "$ORGADOC_DEBUG" = "1" ]; then
	      echo "Found $file, extract title, author, number of pages and date"
	    fi
	    TITLE=`pdfinfo $file 2> /dev/null | grep Title: | sed 's/Title:[ \t]\+//'`
	    AUTHOR=`pdfinfo $file 2> /dev/null | grep Author: | sed 's/Author:[ \t]\+//'`
	    PAGES=`pdfinfo $file 2> /dev/null | grep Pages: | sed 's/Pages:[ \t]\+//'`
	    DATE=`pdfinfo $file 2> /dev/null | grep CreationDate: | sed 's/CreationDate:[ \t]\+//'`
	    echo "<title>$TITLE</title>" >> $ORGADOC_DIR/readme.xml
	    echo "<file>$FILE</file>" >> $ORGADOC_DIR/readme.xml
	    echo "<date>$DATE</date>" >> $ORGADOC_DIR/readme.xml
	    echo "<type>public</public>" >> $ORGADOC_DIR/readme.xml
	    echo "<author>$AUTHOR</author>" >> $ORGADOC_DIR/readme.xml
	    echo "<nbpages>$PAGES</nbpages>" >> $ORGADOC_DIR/readme.xml
	    echo "<language>FIXME: LANGUAGE</language>" >> $ORGADOC_DIR/readme.xml
	    echo "<summary>FIXME: SUMMARY</summary>" >> $ORGADOC_DIR/readme.xml
	    if pdfinfo $file 2> /dev/null | grep Tagged | grep yes > /dev/null; then
	      SECTIONS=`strings $file | grep '/Title ' | sed 's/\/Title[ \t]\+//'`
	      IFS='
'
	      for section in $SECTIONS; do
                echo "<part>$section</part>" >> $ORGADOC_DIR/readme.xml;
              done
	      IFS='	 
'
	    else
		echo "<part>FIXME: PART</part>" >> $ORGADOC_DIR/readme.xml
	    fi
	    ;;
	*.ps.gz | *.ps.Z)
	    if [ "$ORGADOC_DEBUG" = "1" ]; then
	      echo "Found $file, extract number of pages"
	    fi
	    PAGES=`zcat $file | grep '^%%Pages:' | head -1 | sed 's/%%Pages:[ \t]\+//'`
	    echo "<title>FIXME: TITLE</title>" >> $ORGADOC_DIR/readme.xml
	    echo "<file>$FILE</file>" >> $ORGADOC_DIR/readme.xml
	    echo "<date>FIXME: DATE</date>" >> $ORGADOC_DIR/readme.xml
	    echo "<type>public</public>" >> $ORGADOC_DIR/readme.xml
	    echo "<author>FIXME: AUTHOR</author>" >> $ORGADOC_DIR/readme.xml
	    echo "<nbpages>$PAGES</nbpages>" >> $ORGADOC_DIR/readme.xml
	    echo "<language>FIXME: LANGUAGE</language>" >> $ORGADOC_DIR/readme.xml
	    echo "<summary>FIXME: SUMMARY</summary>" >> $ORGADOC_DIR/readme.xml
	    echo "<part>FIXME: PART</part>" >> $ORGADOC_DIR/readme.xml
	    ;;
	*.ps)
	    if [ "$ORGADOC_DEBUG" = "1" ]; then
	      echo "Found $file, extract number of pages"
	    fi
	    PAGES=`cat $file | grep '^%%Pages:' | head -1 | sed 's/%%Pages:[ \t]\+//'`
	    echo "<title>FIXME: TITLE</title>" >> $ORGADOC_DIR/readme.xml
	    echo "<file>$FILE</file>" >> $ORGADOC_DIR/readme.xml
	    echo "<date>FIXME: DATE</date>" >> $ORGADOC_DIR/readme.xml
	    echo "<type>public</public>" >> $ORGADOC_DIR/readme.xml
	    echo "<author>FIXME: AUTHOR</author>" >> $ORGADOC_DIR/readme.xml
	    echo "<nbpages>$PAGES</nbpages>" >> $ORGADOC_DIR/readme.xml
	    echo "<language>FIXME: LANGUAGE</language>" >> $ORGADOC_DIR/readme.xml
	    echo "<summary>FIXME: SUMMARY</summary>" >> $ORGADOC_DIR/readme.xml
	    echo "<part>FIXME: PART</part>" >> $ORGADOC_DIR/readme.xml
	    ;;
	*)
	    if [ "$ORGADOC_DEBUG" = "1" ]; then
	      echo "Found $file, could not extract any informations from file"
	    fi
	    echo "<title>FIXME: TITLE</title>" >> $ORGADOC_DIR/readme.xml
	    echo "<file>$FILE</file>" >> $ORGADOC_DIR/readme.xml
	    echo "<date>FIXME: DATE</date>" >> $ORGADOC_DIR/readme.xml
	    echo "<type>public</public>" >> $ORGADOC_DIR/readme.xml
	    echo "<author>FIXME: AUTHOR</author>" >> $ORGADOC_DIR/readme.xml
	    echo "<language>FIXME: LANGUAGE</language>" >> $ORGADOC_DIR/readme.xml
	    echo "<summary>FIXME: SUMMARY</summary>" >> $ORGADOC_DIR/readme.xml
	    echo "<part>FIXME: PART</part>" >> $ORGADOC_DIR/readme.xml
	    ;;
    esac
    echo "</document>" >> $ORGADOC_DIR/readme.xml;
  fi
done
if test -f $ORGADOC_DIR/readme.xml; then
  echo "</readme>" >> $ORGADOC_DIR/readme.xml;
fi

echo "Leaving directory [$ORGADOC_DIR]"

for dir in $SUBDIRS; do
  if [ "$dir" != "." ]; then
    if [ "$dir" != "$ORGADOC_DIR" ]; then
      if [ "$ORGADOC_DEBUG" = "1" ]; then
        $0 $dir -s -d
      else
        $0 $dir -s
      fi
    fi
  fi
done

if [ "$ORGADOC_SLEEP" = "0" ]; then
  echo
  echo "readme.xml files are now created, please edit the FIXME lines"
fi
