#!/bin/csh -f
#
# @(#)er_rm.csh 1.19 98/03/24
#"@(#)RELEASE VERSION WorkShop 5.0 12/15/98 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} =~ -V ) then
                	echo WorkShop 5.0 12/15/98 er_rm
                	exit 0
        	endif
	end
endif

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

set REVMAGIC = "-<nJ"
set MAGIC = "Jn<-"

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}`

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

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

	if ( ($data[1] != ${MAGIC}) && ($data[1] != ${REVMAGIC}) ) then
		echo ${0}: 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
