#!/bin/csh -f
#
# @(#)er_rm.csh 1.17 96/09/13
#"@(#)RELEASE VERSION Workshop 4.0 er_rm"
#
# Remove/Delete experiments 
#
# Usage:
#
#	er_rm experiment ...
#
# This script removes experiments
#

#
# Look for the -version flag first
#
if ( $#argv > 0 ) then
	foreach ptr_file ( $* )
        	if ( ${ptr_file} =~ -version ) then
                	echo SPARCworks er_rm 3.1
                	exit 0
        	endif
	end
endif

#
# Make sure there are appropriate number of arguments
#
if ( $#argv == 0 ) then
	goto usage
endif

foreach ptr_file ( $* )

#
# If ptr_file is not a file then it is not an experiment
#
	if ( ! -f ${ptr_file} ) then
		echo ${0}: ${ptr_file}: No such experiment
		continue
	endif

	set data = `cat ${ptr_file}`

#
# This part should probably be tightened up.  Check at least the magic number
# and hope that everything is OK.
#

	if ( $#data != 4 ) then
		echo ${0}: ${ptr_file}: incorrectly formed pointer file
		continue
	endif

#
# set magic number by platform (reversed for intel only)
#
	switch (`uname -r`)
		case 5.*:
			switch (`uname -p`)
				case i386:
					set MAGIC = "-<nJ"
					breaksw
				case ppc:
					set MAGIC = "-<nJ"
					breaksw
				default:
					set MAGIC = "Jn<-"
					breaksw
		default:
			set MAGIC = "Jn<-"
			breaksw
	endsw

	if ( $data[1] !~ ${MAGIC} ) then
       		echo ${0}: ${ptr_file}: Not an experiment
		continue
	endif

	set EXP_DIR = $data[4]

	set full_ptr_file = ${ptr_file}

	if ( ${full_ptr_file} !~ /* ) then
		set full_ptr_file = `pwd`/${ptr_file}
	endif

#	Make EXP_DIR an absolute path
	if ( ${EXP_DIR} !~ /* ) then
		set EXP_DIR = `dirname ${full_ptr_file}`/${EXP_DIR}
	endif

	if ( ! -w `dirname ${EXP_DIR}` || ! -w `dirname ${full_ptr_file}` ) then
		echo ${0}: ${ptr_file} not removed: Perimission denied
		continue
	endif

# Disable interrupts
	onintr -


#
# If for whatever reason rm fails, report it
#
	rm -rf ${EXP_DIR}

	if ( $status != 0 ) then
		echo ${0}: ${EXP_DIR}: Could not be removed
	endif

	rm -f ${ptr_file}

	if ( $status != 0 ) then
		echo ${0}: ${ptr_file}: Could not be removed
	endif

# Enable interrupts
	onintr

#
# End of 'for' loop
#
end

#
# Success
#
exit 0

usage:
	echo Usage: ${0} experiment... or ${0} -version
	exit 1
