#!/bin/sh
#
#  MKSRC -- Make a pure source tree and configure the links for the 
#  current architecture.

set -e

# Initialize the $iraf and environment.
if [ -z "$iraf" ]; then
  if [ -e "$HOME/.iraf/setup.sh" ]; then
    . "$HOME/.iraf/setup.sh"
  else
    . unix/hlib/setup.sh
  fi
else
    . "$iraf/unix/hlib/setup.sh"
fi

# Utility aliases.
. "$iraf/unix/hlib/util.sh"

if [ ! -n "$iraf" ];  then
    echo
    echo "Error:  You must have the iraf env variable defined !"
    echo
    exit 1
fi

exec=yes
arch=$("$iraf/unix/hlib/irafarch.sh")


#=============================================================================
# Process any command line arguments.
#=============================================================================
while [ -n "$1" ]; do
    case "$1" in
    "-n")                            # no execute
        exec=no
	;;
    *)
        ERRMSG  "$0: unknown argument $1"
	;;
    esac

    if [ -n "$2" ]; then
        shift
    else
        break
    fi
done


#----------------------------------
# Determine platform architecture.
#----------------------------------

echo "Clean '$arch' host directories ...."
if [ $exec = "yes" ]; then
    rm -f "unix/bin.$arch/"[agm-z]*.e "unix/bin.$arch/"lib[bco]*.a
else
    ls -l "unix/bin.$arch/"[agm-z]*.e "unix/bin.$arch/"lib[bco]*.a
fi

echo "Clean src directories ...."
pats="local math pkg sys noao/[adfimnorst]*"
for i in $pats ; do
  if [ $exec = "yes" ]; then
    find "$i" -type f -name \*.a -exec rm -f {} \;
    find "$i" -type f -name \*.e -exec rm -f {} \;
    find "$i" -type f -name \*.o -exec rm -f {} \;
  else
    find "$i" -type f -name \*.a -exec ls -l {} \;
    find "$i" -type f -name \*.e -exec ls -l {} \;
    find "$i" -type f -name \*.o -exec ls -l {} \;
  fi
done

echo "Clean '$arch' bin directories ...."
if [ $exec = "yes" ]; then
    rm -rf "bin.$arch/"lib[a-z]*.a
    rm -rf "bin.$arch/"*.[eoZ]
    rm -rf "noao/bin.$arch/"*.[aeoZ]
else
    ls -l  "bin.$arch/"lib[a-z]*.a
    ls -l  "bin.$arch/"*.[aeoZ]
    ls -l  "noao/bin.$arch/"*.[aeoZ]
fi

echo "Cleaning spool files ...."
if [ $exec = "yes" ]; then
    find . -name spool\* -print -exec rm -f {} \;
else
    find . -name spool\* -exec ls -l {} \;
fi

exit 0
