#!/bin/csh -f
set x=$0
set prog=$x:t

if ($#argv < 2) goto usage

set program=/usr/bin/file
set d_status=0
set f_status=(`$program $2`)
set errmsg=()
set dest=`echo $2| sed -e sx$1/xx`

if (! -d $1/) then
	set d_status=1
	set errmsg=($1 is not a directory.)
	goto abort
endif

switch($f_status[2])
case "No":				# No such file or directory
	(mkdir $dest; set d_status=$status)
	if ($d_status) goto abort
	breaksw
case "symbolic":			# symbolic link
	/bin/rm $dest; set d_status=$status
	if (! $d_status) then
		mkdir $dest; set d_status=$status
	endif
	if ($d_status) then
		set errmsg=(Cannot destroy/re-create $dest "(symbolic link)")
		goto abort
	endif
	breaksw
case "directory":
	if (! -w $dest) then
		set errmsg=(No write access to $dest)
		goto abort
	endif
	breaksw
default:
	set d_status=1
	set errmsg=($f_status)
	goto abort
	breaksw
endsw

foreach i ($1/{*,.*})
	if (($i != "$1/.") && ($i != "$1/..")) 
		echo ln -s $i $dest/
		ln -s $i $dest/
end
rm -f Makefile

end:
	exit $d_status
abort:
	if ($#errmsg) echo $prog": $errmsg"
	echo $prog": Aborting..."
	if (! $d_status) set d_status=1
	goto end
usage:
	echo "Usage: $prog source dest"
	exit 1
