#! /bin/csh -f
#
# builder.sh
# ==========
# John Holdsworth
# Coldwave Programs Ltd.
#
# This software is in the public domain.
#
# $Log: builder.csh,v $
# Revision 1.4  1997/04/14  22:46:10  johnh
# New category just for DO created.
#
# Revision 1.3  1997/04/13  22:36:38  johnh
# Simplification of Makefiles.
#
# Revision 1.2  1997/01/23  01:00:41  johnh
# Error reporting added and tidy up.
#
# Revision 1.1  1997/01/19  23:16:07  johnh
# Initial version.
#
#

set Operation=$1

switch ( $Operation )

#################################################################
#
# Turn class into category
#
case Categorise :

# break down full path of class source file into head, tail and root
set ClassSourcePath=$2
set ClassSourceDir=$ClassSourcePath:h
set ClassFilename=$ClassSourcePath:t
set ClassName=$ClassFilename:r

# find ClassBuilder subproject
cd $ClassSourceDir
set ClassBuilderDir=ClassBuilder.subproj
while ( ! -d  $ClassBuilderDir )
	set ClassBuilderDir=../$ClassBuilderDir
end

if ( ! -w $ClassSourcePath ) then
	$ClassBuilderDir/prompt.tproj/prompt ClassBuilder "$ClassSourcePath is not writable do you want to check it out?" "Check out" "Continue" "Cancel" 
	switch ( $status )
	case 2 :
		echo "Canceling"
		exit ( 0 )
	case 1 :
		echo "Continuing anyway"
		breaksw
	case 0 :
		echo "Cecking out $ClassFilename"
		dm co $ClassFilename
	endsw
endif

set CategoryProjectDir=$ClassBuilderDir/BundleProjects/ClassCategories.bproj

# Check if file has already been categorised 
if ( -f $CategoryProjectDir/$ClassFilename ) then
	$ClassBuilderDir/prompt.tproj/prompt ClassBuilder "$ClassSourcePath is already exists" "Cancel" "Overwrite" 
	switch ( $status )
	case 2 :
		echo "Canceling"
		exit ( 0 )
	case 1 :
		echo "Continuing anyway"
		breaksw
	case 0 :
		echo "Canceling"
		exit ( 0 )
	endsw
endif

echo "Converting Class file $ClassFilename in directory $ClassSourceDir into category in $CategoryProjectDir"

# construct new filenames

set CategoryFile=$CategoryProjectDir/${ClassName}Category.m
set ReturnPathFile=$CategoryProjectDir/.${ClassName}Category.m

# use awk to turn class implemntation into category implementation
/bin/awk -e '\
$1 ~ /^@implementation/	{ print $1 " " $2 " ( LoadableCategory )" }\
$1 !~ /^@implementation/	{ print $0 }\
' < $ClassFilename >$CategoryFile

echo $ClassSourcePath > $ReturnPathFile

set Service = $ClassBuilderDir/service.tproj/service
$Service "Project/Add To" `pwd`/$CategoryFile
$Service "Project/Build" `pwd`/$CategoryFile

breaksw

#################################################################
#
# Turn category back into class
#
case Decategorise :

# break down full path of class source file into head, tail and root
set CatSourcePath=$2
set CatSourceDir=$CatSourcePath:h
set CatFilename=$CatSourcePath:t
set CatName=$CatFilename:r

set ReturnPathFile=$CatSourceDir/.$CatFilename
set ClassSourcePath=`cat $ReturnPathFile`

# use awk to turn class implemntation into category implementation
/bin/awk -e '\
$1 ~ /^@implementation/	{ print $1 " " $2 " " $7 " " $8 }\
$1 !~ /^@implementation/	{ print $0 }\
' < $CatSourcePath >$ClassSourcePath

rm -f $CatSourceDir/.$CatFilename

breaksw

#################################################################
#
# Remove category
#
case Decategorise :

# break down full path of class source file into head, tail and root
set CatSourcePath=$2
set CatSourceDir=$CatSourcePath:h
set CatFilename=$CatSourcePath:t

set ReturnPathFile=$CatSourceDir/.$CatFilename

rm -f $CatSourcePath
rm -f $ReturnPathFile

breaksw

endsw
