#!/bin/csh -f

##
## use_server [aw server dir]
##
## This command is used to setup a workstation which is planning
## on using a the Alias|Wavefront software from a file server.
## Change directory to the '/usr/aw/' directory on the fileserver.
## For example, this directory might be '/nfs/server/usr/aw'.
## Then type use_server.
##
## JNC - 29AUG95
##  1995 Alias|Wavefront
##


set me=$0
set mydir=$me:h
set me=$me:t
if ($mydir == $me) set mydir=$cwd

set aw = /usr/aw

alias Exit 'set x=\!$ ; goto EXIT'

while ($#argv)

    switch ($1)

        case -w:
                set WIN_STAT ; shift ; breaksw
        case -a:
                set ASK ; shift ; breaksw
        default:
		break

    endsw

end

if ($?WIN_STAT) then

        alias Echo 'echo \ \ \ \  \!* >> /tmp/.ab-$$'

        if ! (-x /usr/bin/X11/xconfirm) echo 'cat << EOF' > /tmp/.ab-$$
        Echo ""
        Echo ""
        Echo "Alias|Wavefront Use Server Program"
        Echo ""
        Echo "Version 1.0,   1995 Alias|Wavefront, Inc"
        Echo ""

else
        alias Echo echo
endif


if ($?ASK) then

    $mydir/ulaunch -geometry 432x130+431+376 -h : -m "Enter the path to the 'aw' directory on your file server" >& /tmp/.aa-$$

    set d = "`/bin/awk '{print $1}' /tmp/.aa-$$`"

    /bin/rm -f /tmp/.aa-$$

    if ("$d" == "") exit

    if (-d "$d") then
	cd $d
    else

	Echo ""
	Echo "$d is not a directory."
	Echo ""
	Echo "Please specify the 'aw' directory"
	Echo "on your fileserver and rerun $me."
	Echo ""

	Exit 1

    endif

endif


if ($#argv == 1) then

    if (-d $1) then
	cd $1
    else

	Echo ""
	Echo "$1 is not a directory."
	Echo ""
	Echo "Please specify the 'aw' directory"
	Echo "on your fileserver and rerun $me."
	Echo ""

	Exit 1

    endif

endif

set here = $cwd

if ! (-o /bin/su) then

	Echo -n "Please enter the Superuser "
	exec su - root -c "exec $mydir/$me $here"

	Echo exec failure
	Exit 1
endif

if ! (`ls | wc -w`) then

    Echo ""
    Echo "$here appears to be empty."
    Echo ""
    Echo "Please specify the 'aw' directory"
    Echo "on your fileserver and rerun $me."
    Echo ""

    Exit 1

endif

/usr/sbin/df . | awk '{print $2}' | grep nfs >& /dev/null

if ($status) then

    Echo ""
    Echo "`pwd` is not a remote directory." 
    Echo ""
    Echo "Please change directory to the 'aw' directory"
    Echo "on your fileserver and rerun $me."
    Echo ""

    Exit 1

endif 

if ! (-d $aw) then

	if (-f $aw) then

		Echo ""
		Echo "$aw is a file, and should be a directory."
		Echo ""
		Exit 1

	endif

	if (-l $aw) then

		Echo ""
		Echo "$aw is a dangling symbolic link, and should"
		Echo "be a directory."
		Echo ""
		Exit 1

	endif

	mkdir -m 755 $aw 
	
	if ($status) then

		Echo ""
		Echo "Unable to create $aw directory."
		Echo ""
		Exit 1

	endif

endif

if (-d $aw) then

    cd $aw

    /usr/sbin/df . | awk '{print $2}' | grep nfs >& /dev/null

    if ($status == 0) then

	Echo ""
	Echo "$aw is not a local directory."
	Echo ""
	Echo "It is recommended to have $aw be a local"
	Echo "directory and use symbolics from $aw to"
	Echo "each of the procduct directories."
	Echo ""

	Exit 1

    endif

    if ! (-w .) then

	Echo ""
	Echo "You do not have write permissions in"
	Echo "the `pwd` directory."
	Echo ""

	Exit 1

    endif


    if ($?WIN_STAT) then
	ln -s /usr/sbin/inform /tmp/.ad-$$
	echo "/tmp/.ad-$$ -b Dismiss -geometry 144x99+504+403 -t Working... &" | sh
    endif


    cd $here
    set awdir = $here:h

    foreach f (*)

	if !(-d $f) then

	    if ! (-l $f) continue

	    set val = `/bin/ls -al $f|awk -F\> '{print $2}'`

	    set mnt = $here:h
	    while (1)
		/bin/df $mnt | grep nfs >& /dev/null 
		if ($status) break
		set mnt = $mnt:h
	    end

	    echo $val | grep '^/' >& /dev/null
	    if ($status) then
				
		if ($?WIN_STAT) then
		    echo "skipping $f : Relative symbolic link." | awk -F: '{printf "     %-30s %s\n", $1, $2}' >> /tmp/.ab-$$
		else
		    echo "skipping $f : Relative symbolic link." | awk -F: '{printf "%-30s %s\n", $1, $2}'
		endif

		continue

	    endif

	    set f = $mnt$val

	    if ! (-d $f) then
				
		if ($?WIN_STAT) then
		    echo "skipping $f : Not a directory or link to directory." | awk -F: '{printf "     %-30s %s\n", $1, $2}' >> /tmp/.ab-$$
		else
		    echo "skipping $f : Not a directory or link to directory." | awk -F: '{printf "%-30s %s\n", $1, $2}'
		endif

		continue

	    endif

	endif
    
	cd $f

	set x = $cwd

	if ! (-f $aw/$x:t || -d $aw/$x:t) then
	    ln -s $cwd $aw >& /dev/null
	    if ($?WIN_STAT) then
		echo "linking $f : Link now exists in $aw." | awk -F: '{printf "     %-30s %s\n", $1, $2}' >> /tmp/.ab-$$
	    else
		echo "linking $f : Link now exists in $aw." | awk -F: '{printf "%-30s %s\n", $1, $2}'
	    endif
	else
	    if ($?WIN_STAT) then
		echo "skipping $f : Already exists in $aw." | awk -F: '{printf "     %-30s %s\n", $1, $2}' >> /tmp/.ab-$$
	    else
		echo "skipping $f : Already exists in $aw." | awk -F: '{printf "%-30s %s\n", $1, $2}'
	    endif
	endif

	cd $here

    end


    ## Handle special case symbolic links
    ##
    foreach s (COM alias studiopaint)
	if (-l $s) then
	    tar cf - $s | (cd $aw; tar xf -)
	endif
    end

endif


Exit 0


EXIT:


if ($?WIN_STAT) then

    ps -ef|grep .ad-$$|awk '{print $2}'|xargs kill >& /dev/null
    rm -f .ad-$$

    Echo ""
    Echo ""
    if ! (-x /usr/bin/X11/xconfirm) then
        Echo "Press <Enter> to continue."
        echo 'EOF' >> /tmp/.ab-$$
        echo 'line' >> /tmp/.ab-$$
        /bin/chmod 755 /tmp/.ab-$$

        /usr/sbin/xwsh -fn "-*-screen-bold-r-normal--16-*-*-*-m-90-iso8859-1" -bg lightgrey -fg black -cursorbg lightgrey -geometry 80x24+278+317 -name "Account Builder" -e sh /tmp/.ab-$$
    else
        /usr/bin/X11/xconfirm -font "-*-screen-medium-r-normal--15-*-*-*-m-80-iso8859-1" \
                              -geometry 800x500+250+300 -icon info -useslider -file /tmp/.ab-$$ >& /dev/null
    endif

    /bin/rm -f /tmp/.ab-$$

endif

exit $x
