#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'sos2obst - 02.12.91 - Bernhard Schiefer (modified 10.11.93)'
#
# sos2obst <dir>
#
# - converts OBST3-2 source files to fullfill
#   the naming conventions of OBST3-4
#   *.sos -> *.obst  *_sos.[ch] -> *_obst.[Ch]  *.c -> *.C
# - removes all files generated by OBST3-2
# - touches all schema files *.sos/*.obst
# - sos2obst finds all files reachable from the given root directory
#

tmpsedfile=/tmp/sos2obst

if [ $# -ne 1 ] ; then
   echo >&2 "*** usage: sos2obst <dir>"
   exit 1
else
   dir=$1
fi

# remove generated files
generated=`find . \( -name '*_sos.c' \
		    -o -name '*_sos.h'  \
		    -o -name '*_use.h'  \
          	 \) -print `

if [ -n "$generated" ] ; then
   echo $generated | tr " " "\12"
   echo -n "These files are generated by OBST3-2. They must be removed. OK? "
   read answer
   if [ "$answer" = "y" -o  "$answer" = "Y" ] ; then
      rm $generated
   else
      echo -n "These files will not work correctly with OBST3-3. Abort? "
      read answer
      if [ "$answer" = "y" -o  "$answer" = "Y" ] ; then
         exit 1
      fi
   fi 
   echo
fi

# lookup files to modify
tomodify=`find . \( -name '*.c' \
		    -o -name '*.h'  \
	  	    -o -name '*.sos'  \
	  	    -o -name '[Mm]akefile'  \
	  	 \) -print `

echo $tomodify | tr " " "\12"

echo -n "These files will be updated. OK? "
read answer
if [ "$answer" = 'n' -o "$answer" = 'N' ] ; then
   exit 1
fi

# modify files
for f in $tomodify
do
   cp $f $tmpsedfile
   basename=`basename $f`
   case $basename in 
	[Mm]akefile ) \
           sed -e 's/sos_default/obst_default/g' \
	       -e 's/_sos/_obst/g' \
	       -e 's/\.sos/.obst/g' \
	       -e 's/\.c/.C/g' \
	       -e 's/(SOS\./(OBST./g' \
	       -e 's/(SOS_/(OBST_/g' \
	       -e 's/sos\.a/libOBST/g' \
	       -e 's/OBST.IF_CC/OBST.GEN.CC/g' \
	       -e 's/OBST.LARGEIF_CC/OBST.BIG_GEN.CC/g' \
      	       -e 's/"err.h"/"obst_err.h"/g' \
               -e 's/"sys.h"/"obst_progstd.h"/g' \
               -e 's/"sos.h"/"obst.h"/g' \
	       $tmpsedfile > $f ;;
	*.c ) \
      	   sed -e 's/sos_init/obst_init/g' \
      	       -e 's/"err.h"/"obst_err.h"/g' \
               -e 's/"sys.h"/"obst_progstd.h"/g' \
               -e 's/"sos.h"/"obst.h"/g' \
	       -e 's/_sos\.h"/_obst.h"/g' \
	       $tmpsedfile > `dirname $f`/`basename $f .c`.C
	   rm $f  ;;
	*.h ) \
      	   sed -e 's/"err.h"/"obst_err.h"/g' \
               -e 's/"sys.h"/"obst_progstd.h"/g' \
               -e 's/"sos.h"/"obst.h"/g' \
	       -e 's/_sos\.h/_obst.h/g' \
	       $tmpsedfile > $f ;;
	*.sos ) \
   	   touch $f
   	   mv $f `dirname $f`/`basename $f .sos`.obst ;;
   esac
done


