#! /bin/csh -f
#
# MKARCH.CSH -- Install the indicated version of the binaries, i.e.,
# archive the current objects and libraries, set BIN to point to bin.FFF,
# and set mkpkg to produce FFF binaries (FFF = ssun, sparc, etc.).
# Based on the IRAF hlib$mkfloat.csh script.

set DIRS 	= "cdl obm obmsh vximtool xaw3d xgterm ximtool xpm xtapemon"
set new_arch 	= "$1"
set clean	= 1

unalias	ls rm cat grep tar cmp diff echo ln mv zcat
unset noclobber


# Get the current platform architecture.
set UNAME=""
if (-e /usr/bin/uname) then
    set uname_cmd = /usr/bin/uname
    set UNAME=`/usr/bin/uname | tr '[A-Z]' '[a-z]'`
endif
if (-e /bin/uname) then
    set uname_cmd = /bin/uname
    set UNAME=`/bin/uname | tr '[A-Z]' '[a-z]'`
endif

switch ($UNAME) 
    case sunos:
        if (`$uname_cmd -m | cut -c2-` == "86pc") then
            set mach = "sunos"
        else
            setenv OSVERSION `uname -r | cut -c1`
            if ($OSVERSION == 5) then
                set mach = "ssun"
            else
                set mach = "sparc"
            endif
        endif
	breaksw
    case linux:
        if (`$uname_cmd -m` == "ppc") then
	     set mach = "linuxppc"
	else
            if (-f /etc/redhat-release) then
                set mach = "redhat"
            else
                set mach = "linux"
            endif
        endif
	breaksw
    case darwin: 
    case macosx:
    case macintel:
        if (`$uname_cmd -m` == "i386") then
            set mach            = "macintel"
        else
            set mach            = "macosx"
        endif
	breaksw
    case freebsd: 
	set mach = "freebsd"
	breaksw
    case cygwin:    
	set mach = "cygwin"
	breaksw
    case sunos:    
	set mach = "sunos"
	breaksw

    case generic:    
	set mach = "generic"
	breaksw
    default:	  
	set mach = "unknown"
	breaksw
endsw


# Process command line options.
set cur_arch = `ls -l bin | sed -e 's+^.*bin\.++'`
if ("$1" == "-show") then
    echo "System is currently configured for $cur_arch."
    exit 0

else if ("$1" == "-arch") then
    echo $cur_arch
    exit 0

else if ("$1" == "-current") then
    set new_arch = $mach
    echo "Configuring X11IRAF for $new_arch..."

else if ("$1" == "-noclean") then
    set new_arch = $mach
    set clean = 0
    echo "Configuring X11IRAF for $new_arch..."

else if ($#argv > 1) then
    # Get the list of directories to be changed.
    shift
    if ("$1" == "-d") then
        set DIRS = ""
        shift
        while ("$1" != "")
            set DIRS = "$DIRS $1"
            shift
        end
    endif
endif


# See if we're already there...
if ($cur_arch == $new_arch) then
    echo "System is already configured for '$new_arch'."
    exit 0
#else if ($new_arch != $mach) then
#    echo "Cannot configure for '$new_arch' on a '$mach' system."
#    exit 1
endif


# Create the bin/lib directories if needed.
if ($new_arch != "generic") then
    if (! -e bin.$new_arch) then
        mkdir bin.$new_arch
    endif
    if (! -e lib.$new_arch) then
        mkdir lib.$new_arch
    endif
endif


# Archive the current architecture files for later use.
if ($cur_arch != "generic") then
    echo "Archive and delete $cur_arch objects..."
    if (-e bin.$cur_arch) then
        rm -f _files
        foreach i (. $DIRS)
            find $i \( -name Makefile -o -name '*.[ao]' \) -print >> _files
        end
        tar -cf - `cat _files` | compress > bin.$cur_arch/OBJS.arc.Z
        rm -f _files

        if ($clean == 1) then
            echo "Cleaning Makefiles..."
            rm -f Makefile */Makefile */*/Makefile
            echo "Rebuilding Makefiles..."
            xmkmf
	    make Makefiles

            echo "Cleaning..."
            make clean >& /dev/null
        endif
    else
        echo "Old objects won't be archived, no bin.$cur_arch directory found."
    endif
endif


# Restore the old files if they exist.
if ($new_arch != "generic") then
    echo "Restore archived $new_arch objects..."
    if (-e bin.$new_arch/OBJS.arc.Z) then
	if ({ (zcat bin.$new_arch/OBJS.arc.Z | tar -xpf -) }) then
	    #rm -f bin.$new_arch/OBJS.arc.Z
	    echo rm -f bin.$new_arch/OBJS.arc.Z
	endif
    else if (-e bin.$new_arch/OBJS.arc) then
	if ({ (cat bin.$new_arch/OBJS.arc | tar -xpf -) }) then
	    #rm -f bin.$new_arch/OBJS.arc
	    echo rm -f bin.$new_arch/OBJS.arc
	endif
    else
	echo "No object archive found; full sysgen will be needed."
    endif
endif


# Set BIN to point to new directory.
rm -f bin; ln -s bin.$new_arch bin
rm -f lib; ln -s lib.$new_arch lib

