#!/bin/sh

#### 3DLDF-<Version>/CWEB/tsthdweb
#### Created by Laurence D. Finston (LDF).

# This file is part of 3DLDF, a package for three-dimensional drawing. 
# Copyright (C) 2003 Laurence D. Finston. 

# 3DLDF 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. 

# 3DLDF 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 3DLDF; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

# The author can be contacted at: 

# Laurence D. Finston 
# Kreuzbergring 41 
# D-37075 Goettingen 
# Germany 

# lfinsto1@gwdg.de 
# s246794@stud.uni-goettingen.de 

# $Id: tsthdweb,v 1.4 2003/12/01 18:38:18 lfinsto1 Exp $

## tsthdweb is a shell script that compares to files with the same
## name and different extensions using diff.  It is used for comparing
## old and new versions of the header files and the C++ code files
## generated by ctangle.  It is called in ./Makefile.am.  It's used,
## because I was not able to get the conditional construction into
## Makefile.am without causing errors.  LDF 2003.11.17.


## First argument:  Filename without extension.

## Second Argument: 0 --- C++ and header files.
#                   1 --- C++ file only (main.web).
#                   2 --- Header file only (loader.web).



if test $2 -lt 0 || test $2 -gt 2
then
   echo "ERROR! In tsthdweb: Invalid second argument: $2"
   echo "Returning 1."
   exit 1
fi


rm -f -r diff_res

if test $2 -eq 0  || test $2 -eq 1 # Test C++ file.
then
   if test ! -e $1.tmw
     then
        echo "$1.tmw doesn't exist.  Touching $1.tmw."
        touch $1.tmw
   fi
   file_a=$1.cxx
   file_b=$1.c
   if test ! -e $file_a
     then
       echo "$file_a doesn't exist. Renaming $file_b to $file_a, "
       echo "touching $1.tmw and exiting tsthdweb."
       mv $file_b $file_a
       touch $1.tmw
       exit 0
   fi
fi

if test $2 -eq 0 || test $2 -eq 2 # Test header file.
   then
      file_c=$1.h
      file_d=$1.hbk
      if test ! -e $file_d
        then
          echo "$file_d doesn't exist. Touching $1.tim and exiting tsthdweb."
          touch $1.tim
          exit 0
      fi
fi


cxx_changed=0
header_changed=0

if test $2 -eq 0 || test $2 -eq 1  # Test C++ file.
   then
      diff -q -w -B -I '^[[:space:]]*\(#\|/\*\)' $file_a $file_b > diff_res

      # cat diff_res
      if test -s diff_res 
         then
             echo "$file_b has changed."
             echo "Renaming $1.c to $1.cxx."
             rm -f -r $1.cxx
             mv $1.c $1.cxx
             echo "Touching $1.tmw."
             touch $1.tmw
             cxx_changed=1
         else
             echo "$1.c hasn't changed. Deleting $1.c."
             rm -f -r $1.c
             touch $1.tmw --reference=$1.cxx
      fi
fi


if test $2 -eq 0 || test $2 -eq 2 # Test header file.
   then
       diff -q -w -B -I '^[[:space:]]*\(#\|/\*\)' $file_c $file_d > diff_res
       if test -s diff_res 
          then 
             #cat diff_res
             echo "$file_c has changed."
             echo "Touching $1.tim."
             touch $1.tim
             header_changed=1
          else
             echo "$file_c hasn't changed."
             rm -f -r $1.h
             cp -p $1.hbk $1.h
             touch $1.tim --reference=$1.hbk
        fi
fi

if test $cxx_changed -eq 0 && test $header_changed -eq 0
   then
     if test $1.tim -ot $1.tmw
        then
            echo "Setting timestamp of $1.web to that of $1.tim."
            touch $1.web --reference=$1.tim
        else
            echo "Setting timestamp of $1.web to that of $1.tmw."
            touch $1.web --reference=$1.tmw
     fi
     echo "You will have to revert the buffer of $1.web"
     echo "if you're currently, working on it, because this changes it on disk."
elif test $cxx_changed -eq 0 && test $header_changed -eq 1
    then
            echo "Setting timestamp of $1.tmw to that of $1.tim."
            touch $1.tmw --reference=$1.tim
elif test $cxx_changed -eq 1 && test $header_changed -eq 0
    then
            echo "Setting timestamp of $1.tim to that of $1.tmw."
fi            




rm -fr $1.hbk
rm -fr diff_res



