#!/bin/sh

# Copy shared .../sde/examples and .../sde/kit directories to
# user's current directory so that they can be modified and 
# built locally

# Make sure we've got at least a minimal PATH
PATH=${PATH:-/usr/bin:/bin}
export PATH

# Find the directory containing this script and its parent
bindir="`dirname $0`"

# Convert relative name to absolute
case "${bindir}" in
/*)	;;
*)	bindir="`pwd`/${bindir}"
	;;
esac

nw=`echo ${bindir} | wc -w`
if test $nw -gt 1; then
    echo "Cannot copy from directory with spaces in name, please move files."
    exit 1
fi

# remove trailing "/."
bindir="`echo \"${bindir}\" | sed -e 's%[\\/][\\/]*\\.$%%'`"

# remove trailing "/bin"
defsrcdir="`dirname \"${bindir}\"`"
srcdir="$defsrcdir"

# Include prompt and readline support
if test -r ${defsrcdir}/etc/readline.sh ; then
. ${defsrcdir}/etc/readline.sh
else
  echo "Cannot find component readline.sh"
  exit 1
fi

if test -r ${defsrcdir}/sde/kit/sdeversion; then
  . ${defsrcdir}/sde/kit/sdeversion
fi
version=${SDE_MAJOR:-6}.${SDE_MINOR:-xx}
fullversion=${version}.${SDE_PATCH:-00}

# Default destination directory
defdstdir="`pwd`/sde-$fullversion"
dstdir="$defdstdir"
addlib=
addincl=
force=
verbose=
debug=

# Parse command-line options
optprev=
for option
do
  # If the previous option needs an argument, assign it.
  if test -n "$optprev"; then
    eval "$optprev=\$option"
    optprev=
    continue
  fi

  case "$option" in
  -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$option" in
  -srcdir | --srcdir)
    optprev=srcdir ;;
  -srcdir=* | --srcdir=*)
    srcdir="$optarg" ;;
  -dstdir | --dstdir)
    optprev=dstdir ;;
  -dstdir=* | --dstdir=*)
    dstdir="$optarg" ;;
  -add-libsrc | --add-libsrc)
    addlib=yes ;;
  -add-headers | --add-headers)
    addincl=yes ;;
  -f | -force | --force)
    force=yes ;;
  -d | --debug | -debug)
    debug=yes ;;
  -v | --verbose | -verbose)
    verbose=yes ;;
  -help | --help | --hel | --he)
    # Omit some internal or obsolete options to make the list less imposing.
    # This message is too long to be a string in the A/UX 3.1 sh.
    cat << EOF
Usage: sdemklocal [options]
Options: [defaults in brackets after descriptions]
  --help                  Print this message
  --srcdir=SRCDIR	  Override the location of installed SDE tree
                          [$defsrcdir]
  --dstdir=DSTDIR	  Override the default destination directory
                          [$defdstdir]
  --add-libsrc            Copy library source too
  --add-headers           Copy header files too
  --force | -f		  Force copy, no questions or warnings
  --verbose | -v          Display files being copied
  --debug | -d            Output debug tracing
EOF
    exit 0
    ;;
  -*) { echo "error: $option: invalid option; use --help to show usage" 1>&2; exit 1; }
    ;;
  esac
done

if test -n "$optprev"; then
  { echo "error: missing argument to --`echo $optprev | sed 's/_/-/g'`" 1>&2; exit 1; }
fi

if test -n "$debug"; then
  # Display progress through script
  set -xv
fi

if [ ! -d $srcdir/sde/kit ]; then
  echo "./sde/kit not found in $srcdir"
  echo "Please check your --srcdir option"
  exit 1
fi
if [ ! -d $srcdir/sde/examples ]; then
  echo "./sde/examples not found in $srcdir"
  echo "Please check your --srcdir option"
  exit 1
fi

if test -z "$force"; then
  echo "Copying SDE run-time sources to $dstdir"
  if test -e $dstdir; then
    echo "WARNING: $dstdir already exists!"
  fi
  if ! question "Are you sure?" n; then
    echo "OK, I give up"
    exit 0
  fi
fi

if ! test -d $dstdir; then
  if ! mkdir -p $dstdir; then
    echo "Could not create directory $dstdir"
    exit 1
  fi
fi

# Convert relative name to absolute
case "${dstdir}" in
/*)	;;
*)	dstdir="`pwd`/${dstdir}"
	;;
esac

cd $srcdir/sde

dirs="examples kit"
test -n "$addincl" && test -d include && dirs="$dirs include"
test -n "$addlib" && test -d libsrc && dirs="$dirs libsrc"

# Strip out the pre-compiled objects if we have the full SDE source
prune=
test -f kit/fullsde.mk && prune="-path kit/free -prune -o"

# Set cpio verbose flag
v=
test -z "$verbose$debug" || v=v

# Set cpio unconditional flag
u=
test -z "$force" || u=u

# Finally, do the copy
find $dirs $prune -print | cpio -pdm$v$u $dstdir
