#!/bin/sh

# Brief
# This file is being cleaned up. It will be sourced in by
# another file and the client accesses the functions.
# However it currently uses a very primitive method.
# <TODO> URGENT FIX.

source chel.sh

source docmakefile.sh


# DEPRECATED
# Archiving cpp source,
# Delete non-exported files eg binaries and crap.
function cleannonexportdoc
{
  local i
  for i in `ls | grep -v '.*.txt\|.*.cpp\|Makefile\|.*.html\|.*.xml\|.*.h\|.*.png\|.*.jpg\|.*.gif\|.*.wrl\|.*.sh'` 
  do
    if [ -f $i ] 
    then
      #echo $i
      rm $i
    fi
  done

  # main.cpp~ survives.
  for i in `ls *~` 
  do
    if [ -f $i ] 
    then
      rm ${i}
    fi
  done

}


# Write the code with only the source files.
# <TODO> Rewrite to include everything and exclude
#   somethings. Motivation: test and text files need
#   to be included for the tests.
function projupdatewritesrc 
{
  echo ""
  echo "Write the proj source code."
  echo ""

  # Only .cpp, .h and Makefile files are copied.

  local dest=/tmp/proj

  rm -R ${dest} 
  mkdir ${dest}

  proj

  local i
  for i in `find . -name \*.h`; do cpm ${dest} ${i}; done;
  for i in `find . -name \*.cpp`; do cpm ${dest} ${i}; done;
  for i in `find . -name Makefile`; do cpm ${dest} ${i}; done;
  cp MakefileUpdate.sh ${dest}

  echo "Creating archive"
  pushd /tmp
  tar -cf cppcode.tar proj
  gzip cppcode.tar
  rm -f ${base}/p1/misc/cppcode.tar.gz
  mv cppcode.tar.gz ${base}/p1/misc
  popd

  popd
}



function classesextract
{
  # Find all the lines with class in the header files.
  grep -h class *.h > /tmp/temp01.txt
  # Keep only the ones where class is the first word. 
  awk ' $1 == "class" { print $2 }' /tmp/temp01.txt > /tmp/temp02.txt
  # Remove declarations which have a colen at the end.
  awk ' $0 !~ /;/ ' /tmp/temp02.txt > /tmp/temp03.txt
  # Uniquely sort the list.
  sort -u /tmp/temp03.txt > /tmp/temp04.txt
}

function classtable
{
  local i
  for i in `cat /tmp/temp04.txt`; do
    if [ -e ~/base/p1/misc/proj/html/class${i}.html ]; 
    then 
      echo \<a class=\"reflocal\" href=\"../html/class${i}.html\"\>${i}\<\/a\> \<br\/\>
    fi
  done;
}


function sourcetablemake5
{
  echo \<!--UPDATE_SOURCE_TABLE_BEGIN--\>
  echo \<div class=\"float25\"\>

  echo \<a id=\"Source\"\> \<\/a\>
  echo \<h3\> \<a href=\"..\/..\/download.html\"\>Source\<\/a\> \<\/h3\>

  echo \<div class=\"float10\"\>
  echo \<a href=\"Makefile.html\"\> Makefile \<\/a\> \<br\/\>

  classesextract

  classtable

  echo \<\/div\>

  echo \<\/div\>

  echo \<!--UPDATE_SOURCE_TABLE_END--\>
}










#function projupdateMakefilehtml
#{
#  proj
#  local i
#  for i in `ls */Makefile`;
#  do
#    pushd ${i%%/*}

#    Makefilehtml

#    popd
#  done
#  popd
#}


function sourcetableupdate2 
{

  projupdateMakefilehtml
 
  declare marker=--UPDATE_SOURCE_TABLE--

  proj
  local sourcetableupdate2_i
  for sourcetableupdate2_i in `ls */doc.xml */doc.html`;
  do
    pushd ${sourcetableupdate2_i%%/*}

#    echo crap
#    echo ${sourcetableupdate2_i##*/}

    sourcetablemake5 ${sourcetableupdate2_i%%/*} > sourcetable.txt

    local filename=${sourcetableupdate2_i##*/}

#    echo moving
#    echo ${filename}

    if grep -s -e ${marker} ${filename};
    then
      sed -e '/--UPDATE_SOURCE_TABLE_BEGIN--/,/--UPDATE_SOURCE_TABLE_END--/d' -e '/--UPDATE_SOURCE_TABLE--/r sourcetable.txt' ${filename} > ${filename}2
      mv -f ${filename} /tmp/${filename}
      mv -f ${filename}2 ${filename}
    fi
    rm sourcetable.txt    

    #cp Makefile Makefile.txt

    popd
  done
  popd
}



# Dependency for updates.
#  sourcetableupdate2  ->  sourcetablemake5: write class table, Makefile














#
#  PROJECT COMPILATION STATE
#


#----------------------------
#  Writes and archives src to gz file.
#$ bash update.sh writesrc
commandlinetoken=writesrc
commandlinecommand="projupdatewritesrc"
commandline $@

#----------------------------
#  Creates the source table which is inserted into doc.xml or doc.html
#$ bash update.sh sourcetable 
commandlinetoken=sourcetable
commandlinecommand="sourcetableupdate2"
commandline $@




