#!/bin/sh

# Usage: install_manual_files <version_num>
#   where <version_num> is something like 2.1-I-17

if [ $# -eq 0 ] ; then
   echo "Usage $0 <version_num>"
   echo "   <version_num> should be something like 2.1-I-17"
   exit 1
else
   VERSION_NUM=$1
   VERSION=CGAL-$1
fi

SOURCE_DIR="/KM/projects/CGAL/Work_in_Progress/CGAL_autotest/$VERSION"

if [ ! -d $SOURCE_DIR/doc_tex ] ; then
   echo "source directory $SOURCE_DIR/doc_tex does not exist"
   exit 1
fi

if [ ! -d doc_tex_skel ] ; then
   echo "skeleton directory doc_tex_skel does not exist in this directory"
   exit 1
fi


TARGET_DIR="${VERSION}_doc_tex"
if [ -d $TARGET_DIR ]; then
    echo "directory $TARGET_DIR already exists"
    exit 1
fi

mkdir $TARGET_DIR


# copy the "skeleton" directory
cp -r doc_tex_skel/* $TARGET_DIR

# put the right version number in the makefile
sed 's/VERSION_NUM=.*/VERSION_NUM='$VERSION_NUM'/' $TARGET_DIR/makefile > $TARGET_DIR/new_makefile
\mv $TARGET_DIR/new_makefile $TARGET_DIR/makefile

SRC_SUBDIRS=`ls $SOURCE_DIR/doc_tex`

if [ ! -f $TARGET_DIR/docdirs ]; then
  echo "*** 'docdirs' file does not exist in $TARGET_DIR ***"
  exit 1
fi


find_in_target_docdirs()
{
   for TGT_SUBSUBDIR in $TGT_DOCDIRS; do
      if [ $TGT_SUBSUBDIR = $SRC_SUBSUBDIR \
          -o ${TGT_SUBSUBDIR}_ref = $SRC_SUBSUBDIR ]; then
         echo "y"
         return
      fi
   done
   echo "n"
}

# for each subdirectory in the source
#   see if there is a corresponding subdirectory in the target
#   if so, see if there is a docdirs file in that target subdirectory
#      if so, see if all the directories listed in this docdirs are
#      also subdirectories of the source AND see if all the subdirectories
#      of the source are listed in the docdirs file
for SUBDIR in $SRC_SUBDIRS; do
   if [ ! -d $TARGET_DIR/$SUBDIR ]; then
      echo
      echo "+++ subdirectory $SUBDIR is new in source directory +++
      echo "+++ update 'docdirs' file in $TARGET_DIR +++
      echo
   elif [ -f $TARGET_DIR/$SUBDIR/docdirs ]; then
      TGT_DOCDIRS=`cat $TARGET_DIR/$SUBDIR/docdirs`
      for TGT_SUBSUBDIR in $TGT_DOCDIRS; do
         if [    $TGT_SUBSUBDIR != "Title" \
              -a $TGT_SUBSUBDIR != "Preface" \
              -a $TGT_SUBSUBDIR != "Introduction" \
              -a ! -d $SOURCE_DIR/doc_tex/$SUBDIR/$TGT_SUBSUBDIR ]; then
            echo "--- directory $TGT_SUBSUBDIR does not exist in $SOURCE_DIR/doc_tex/$SUBDIR"
         fi
      done
      make_AllMains $TARGET_DIR/$SUBDIR
      SRC_SUBSUBDIRS=`ls $SOURCE_DIR/doc_tex/$SUBDIR`
      for SRC_SUBSUBDIR in $SRC_SUBSUBDIRS; do
         RESULT=`find_in_target_docdirs`
         if [ $RESULT = "n"  ]; then
            echo "+++ directory $SRC_SUBSUBDIR is not in $TARGET_DIR/$SUBDIR/docdirs"
         fi
      done
   fi
done

cp -r $SOURCE_DIR/doc_tex/* $TARGET_DIR

# edit the cgalrelease command to correspond to this version
# 
DOCDIRS=`cat $TARGET_DIR/docdirs`
for DIR in $DOCDIRS; do
   if [ -f $TARGET_DIR/$DIR/wrapper.tex ]; then
      sed 's/\\newcommand{\\cgalrelease}.*/\\newcommand{\\cgalrelease}{'$VERSION_NUM'}/' $TARGET_DIR/$DIR/wrapper.tex > temp_wrapper.tex
      mv temp_wrapper.tex $TARGET_DIR/$DIR/wrapper.tex
   else
      echo "!!! directory $TARGET_DIR/$DIR has no wrapper.tex !!!"
   fi
done


echo "***copying common_intro.tex to kernel and support ***"
\cp $TARGET_DIR/basic/Introduction/common_intro.tex $TARGET_DIR/support/Introduction
\cp $TARGET_DIR/basic/Introduction/common_intro.tex $TARGET_DIR/kernel/

echo "***copying Timer to Miscellany***"
\cp $TARGET_DIR/support/Timer/* $TARGET_DIR/support/Miscellany
\cp $TARGET_DIR/support/Timer_ref/* $TARGET_DIR/support/Miscellany_ref
echo "***copying Hash_map to Miscellaney***"
\cp $TARGET_DIR/support/Hash_map/* $TARGET_DIR/support/Miscellany
\cp $TARGET_DIR/support/Hash_map_ref/* $TARGET_DIR/support/Miscellany_ref
echo "***copying Union_find to Miscellaney***"
\cp $TARGET_DIR/support/Union_find/* $TARGET_DIR/support/Miscellany
\cp $TARGET_DIR/support/Union_find_ref/* $TARGET_DIR/support/Miscellany_ref
echo "***copying Modifier to Miscellaney***"
\cp -r $TARGET_DIR/support/Modifier/* $TARGET_DIR/support/Miscellany
\cp -r $TARGET_DIR/support/Modifier_ref/*.tex $TARGET_DIR/support/Miscellany_ref
echo "***recopying Miscellany main.tex***"
\cp doc_tex_skel/support/Miscellany/main.tex $TARGET_DIR/support/Miscellany
echo "***recopying Miscellany_ref intro.tex***"
\cp doc_tex_skel/support/Miscellany_ref/intro.tex $TARGET_DIR/support/Miscellany_ref

cd $TARGET_DIR/support
echo "***creating Miscellany_ref main.tex***"
cc_make_ref_pages Miscellany_ref

cd .. 
link_html_wrapper
cd ..

echo "making link to examples and demo directories"
\rm -f examples
\rm -f demo
ln -s $SOURCE_DIR/examples .
ln -s $SOURCE_DIR/demo .



syntax highlighted by Code2HTML, v. 0.9.1