#!/bin/csh -f
#
# @(#)er_mv.csh 1.17 96/09/13
#"@(#)RELEASE VERSION Workshop 4.0 er_mv"
#
# Move experiments 
#
# Usage:
#
#	er_mv er1 er2
#	       OR
#	er_mv er1 dir
#
# This script copies/moves the experiment er1 to er2 or to the directory dir
# If the file er2 already exists the script will clobber existing file.
#

if ( $#argv > 0 ) then
	foreach ptr_file ( $* )
		if ( $argv[1] =~ -version ) then
			echo SPARCworks er_mv 3.1
			exit 0
		endif
	end
endif

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

#
# If er1 is not a file then it is not an experiment
#
if ( ! -f $argv[1] ) then
	echo ${0}: $argv[1] Not an experiment
	exit 2
endif

if ( ! -d $argv[2] ) then
	set DEST_DIR = `dirname $argv[2]`
else
	set DEST_DIR = $argv[2]
endif

# Make destination directory an absolute patch
if ( ${DEST_DIR} !~ /* ) then
	set DEST_DIR = `pwd`/${DEST_DIR}
endif

set SRC_FILE = $argv[1]

# Make src_file an absolute patch
if ( ${SRC_FILE} !~ /* ) then
	set SRC_FILE = `pwd`/${SRC_FILE}
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

#
# set the destination pointer file
#
if ( -d $argv[2] ) then
	set DEST_FILE = ${DEST_DIR}/`basename ${SRC_FILE}`
else
	set DEST_FILE = $argv[2]
endif

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

set data = `cat $argv[1]`

#
# 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}: Not an experiment
	exit 3
endif

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

set EXP_DIR = $data[4]

#
# Make data directory as absolute path
#
if ( ${EXP_DIR} !~ /* ) then
	set EXP_DIR = `dirname ${SRC_FILE}`/${EXP_DIR}
endif

#
# Bogus pointer file...  The experiment directory does not exist.
#
if ( ! -d ${EXP_DIR} ) then
	echo ${0}: Experiment directory ${EXP_DIR} does not exist
	exit 4
endif

#
# Check to see if experiment can be moved at all
#
if ( ! -w `dirname ${EXP_DIR}` ||  ! -w `dirname ${SRC_FILE}` ) then
	echo ${0}: Experiment cannot be moved
	exit 6
endif

if ( ! -w ${DEST_DIR} ) then
	echo ${0}: Destination directory ${DEST_DIR} is not writable
	exit 5
endif

#
# Start moving the actualy experiment directory
#
onintr -

set FINAL_DIR = ${DEST_DIR}/.`basename ${DEST_FILE}`

if ( -d ${FINAL_DIR} ) then
	echo ${0}: ${FINAL_DIR} already exists, cannot overwrite
	exit 6
endif

mv ${EXP_DIR} ${FINAL_DIR}
#
# If for whatever reason mv fails then abort the move.
#
if ( $status != 0) then
	echo ${0}: mv ${EXP_DIR} ${FINAL_DIR} failed
	exit 4
endif

#
# Move the pointer file
#
set data[4] = ${FINAL_DIR}

#
# Write out new pointer file
#
mv ${SRC_FILE} ${DEST_FILE}

#
# If pointer file cannot be moved then report error
#
if ( $status != 0 ) then
	echo ${0}: Pointer file move failed, data directories: ${EXP_DIR} and ${FINAL_DIR} affected
	exit 5
endif
echo $data >! ${DEST_FILE}

#
# Restore interrupts
#
onintr

#
# Success
#
exit 0

usage:
	echo Usage: ${0} er1 er2 or ${0} er1 dir or ${0} -version
	exit 1
