#!/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  Sat Jan  3 15:28:44 2004 Julien Lemoine
## $Id: orgadoc_add_docs,v 2.0 2017/04/27 20:32:57 atsb Exp $
## 
## Copyright (C) 2004 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_HOST=localhost
ORGADOC_CVS_MODULE=orgadoc
ORGADOC_CVS_ROOT=$HOME/cvs
ORGADOC_DEBUG=0
ORGADOC_HOST_DIR=/var/www/docs/orgadoc

help()
{
  echo "OrgaDoc Add Docs (Add documents on server when xml files are stored on CVS)"
  echo
  echo "Usage: $0 [OPTION] xml-file section"
  echo
  echo "Options:"
  echo -e "  -H, --host <str>\tHost to upload xml file and datas (default: localhost)"
  echo -e "  -D, --host-dir <str>\tHost directory where doc are stored"
  echo -e "\t\t\t(default: /var/www/docs/orgadoc)"
  echo -e "  -c, --cvs <str>\tCVS Module where"
  echo -e "\t\t\txml files are stored (default: orgadoc)"
  echo -e "  -s, --cvs-root <str>\tRoot CVS directory (for cvs merge)"
  echo -e "\t\t\t(default: $HOME/cvs)"
  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 Add Docs"
  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
      -d|--debug)
	  ORGADOC_DEBUG=1
	  ;;
      -s|--cvs-root)
	  if [ "$2" = "" ]; then
	    echo "Error -s, --cvs-root option take an argument"
	    exit 1;
	  else
	    ORGADOC_CVS_ROOT=$2
	  fi
	  ;;
      -D|--host-dir)
	  if [ "$2" = "" ]; then
	    echo "Error -D, --host-dir option take an argument"
	    exit 1;
	  else
	    ORGADOC_HOST_DIR=$2
	  fi
	  ;;
      -c|--cvs)
	  if [ "$2" = "" ]; then
	    echo "Error -c, --cvs option take an argument"
	    exit 1;
	  else
	    ORGADOC_CVS_MODULE=$2
	  fi
	  ;;
      -H|--host)
	  if [ "$2" = "" ]; then
	    echo "Error -H, --host option take an argument"
	    exit 1;
	  else
	    ORGADOC_HOST=$2
	  fi
	  ;;
      -h|--help)
	  help
	  exit 0
	  ;;
      -v|--version)
	  version
	  exit 0
	  ;;
      *)
	  if [ "$1" != "" ]; then
            if [ "$ORGADOC_XML_FILE" = "" ]; then
	      ORGADOC_XML_FILE=$1;
	    else
	      if [ "$ORGADOC_SECTION" = "" ]; then
	        ORGADOC_SECTION=$1
	      else
		echo "Warning: unused option [$1]"
	      fi
	    fi
          fi
	  ;;
  esac
}

options $1 $2
while shift; do 
  options $1 $2;
  case "$1" in
      -c|--cvs|-H|--host|-D|--host-dir|-s|--cvs-root)
	  shift
	  ;;
  esac
done

ORGADOC_CVS_DIR=$ORGADOC_CVS_ROOT/$ORGADOC_CVS_MODULE
if [ "$ORGADOC_XML_FILE" = "" ]; then
  echo "Error: You must give a xml file"
  echo
  help
  exit 1
fi
if [ "$ORGADOC_SECTION" = "" ]; then
  echo "Error: You must give a section"
  echo
  help
  exit 1
fi

if [ "$ORGADOC_DEBUG" = "1" ]; then
  echo "--- Server Information ---"
  echo "  Hostname : $ORGADOC_HOST"
  echo "  Directory : $ORGADOC_HOST_DIR"
  echo
  echo "--- Local Information ---"
  echo " CVS Module : $ORGADOC_CVS_MODULE"
  echo " CVS Root Directory : $ORGADOC_CVS_ROOT"
  echo " XML File to upload : $ORGADOC_XML_FILE"
  echo " Section : $ORGADOC_SECTION"
  echo 
fi

FILES=`cat $ORGADOC_XML_FILE | tr '\n' ' ' |		\
tr '\r' ' ' | sed 's/[ \t]//g' |			\
sed 's/<[^f][^<]*<\/[^>]*>//g' |			\
sed 's/<[^f/][^>]*>//g' |				\
sed 's/<\/[^f][^>]*>//g' |				\
sed 's/<file>\([^<]*\)<\/file>/\1/g' | tr '' '\n'`

if [ "$FILES" = "" ]; then
  echo "Nothing to upload..."
  exit 0
fi

echo "Step 1: Upload documents"
ORGADOC_XML_DIR=`echo $ORGADOC_XML_FILE | sed -e 's/\/[^/]*$//' -e 's/^[^/]*$/./'`
for file in $FILES; do 
  echo "  Uploading : $ORGADOC_XML_DIR/$file"
  echo "  Destination : $ORGADOC_HOST:$ORGADOC_HOST_DIR/$ORGADOC_SECTION"
  ssh $ORGADOC_HOST mkdir -p $ORGADOC_HOST_DIR/$ORGADOC_SECTION > /dev/null 2>&1
  scp $ORGADOC_XML_DIR/$file $ORGADOC_HOST:$ORGADOC_HOST_DIR/$ORGADOC_SECTION
done
echo
echo "Step 2: Update cvs readme.xml file"
cd $ORGADOC_CVS_DIR && cvs update -d
tempfile=`tempfile`
if test -d $ORGADOC_CVS_DIR/$ORGADOC_SECTION; then
  echo
  echo "Step 3: Merge readme.xml files"
  cat $ORGADOC_CVS_DIR/$ORGADOC_SECTION/readme.xml | (
    read lineb
    while read line; do
      echo $lineb >> $tempfile
      lineb=$line
    done
  )
  cat $ORGADOC_XML_FILE | sed 's/<readme>//g>' | sed 's/<\/readme>//g' >> $tempfile
  echo "</readme>" >> $tempfile
else
  cp $ORGADOC_XML_FILE >> $tempfile
fi
cp $tempfile $ORGADOC_CVS_DIR/$ORGADOC_SECTION/readme.xml
echo 
echo "Step 4: Commit changes"
if test -d $ORGADOC_CVS_DIR/$ORGADOC_SECTION; then
  cd $ORGADOC_CVS_DIR && cvs commit
else
  mkdir -p $ORGADOC_CVS_DIR/$ORGADOC_SECTION
  cp $tempfile $ORGADOC_CVS_DIR/$ORGADOC_SECTION/readme.xml
  ORGADOC_NEW=`echo $ORGADOC_CVS_DIR/$ORGADOC_SECTION | sed "s:$ORGADOC_CVS_ROOT::g"`
  cd $ORGADOC_CVS_DIR/$ORGADOC_SECTION && cvs import $ORGADOC_NEW R1_0 V1_0
fi
rm -f $tempfile

exit 0
