# 
# take-from-cache.sh - try to steal a revision from the cache
################################################################
# Copyright (C) 2001, 2002 Tom Lord
# 
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
# 

set -e 

command_line="$*"

################################################################
# special options
# 
# Some options are special:
# 
#       --version | -V
#       --help | -h
# 
if test $# -ne 0 ; then

  for opt in "$@" ; do
    case $opt in

      --version|-V) exec larch --version
                    ;;


      --help|-h)
                printf "try to steal a revision from a cache\\n"
                printf "usage: take-from-cache [options] revision\\n"
                printf "\\n"
                printf " -V --version                  print version info\\n"
                printf " -h --help                     display help\\n"
                printf "\\n"
                printf " -R --root root                specify the local archive root\\n"
                printf " -A --archive archive          specify the archive name\\n"
                printf " -d --dir DIR                  switch to DIR first\\n"
                printf "\\n"
                printf " --silent                      no output (except odd errors)\\n"
                printf " --quiet                       brief output\\n"
                printf " --report                      default output\\n"
                printf " --verbose                     maximal output\\n"
                printf " --debug                       debugging output\\n"
                printf "\\n"
                printf "Look for a cached copy of REVISION in the project tree\\n"
                printf "containing DIR (or the current directory).  If one is found,\\n"
                printf "move it to the current directory and exit with status 0.  Otherwise,\\n"
                printf "exit with status 1.\\n"
                printf "\\n"
                exit 0
                ;;

      *)
                ;;
    esac
  done
fi

################################################################
# Ordinary Options
# 
# 

archroot=
archive=
dir=

quiet=--quiet
report=--report
verbose=

while test $# -ne 0 ; do

  case "$1" in 

    --silent)   shift
                quiet=
                report=
                verbose=
                ;;

    --quiet)    shift
                quiet=--quiet
                report=
                verbose=
                ;;

    --report)   shift
                quiet=--quiet
                report=--report
                verbose=
                ;;

    --verbose)  shift
                quiet=--quiet
                report=--report
                verbose=--verbose
                ;;

    --debug)    shift
                larch heading "take-from-cache: debugging output enabled\\n"
                set -x
                ;;


    -d|--dir)           shift
                        if test $# -eq 0 ; then
                          printf "take-from-cache: -d and --dir require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        dir="$1"
                        shift
                        ;;

    -R|--root)          shift
                        if test $# -eq 0 ; then
                          printf "make-category: -R and --root require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        archroot="$1"
                        shift
                        ;;

    -A|--archive)       shift
                        if test $# -eq 0 ; then
                          printf "make-category: -A and --archive require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        archive="$1"
                        shift
                        ;;

    --)                 shift
                        break
                        ;;

    -*)                 printf "take-from-cache: unrecognized option (%s)\\n" "$1" 1>&2
                        printf "try --help\\n" 1>&2
                        exit 1
                        ;;

    *)                  break
                        ;;

  esac

done



################################################################
# Ordinary Arguments
# 

if test $# -ne 1 ; then
  printf "usage: take-from-cache [options] revision\\n" 1>&2
  printf "try --help\\n" 1>&2
  exit 1
fi

revision="$1"
shift

################################################################
# Sanity Check and Process Defaults
# 

larch valid-package-name -l -e take-from-cache -- "$revision"

archive=`larch parse-package-name -R "$archroot" -A "$archive" --arch "$revision"`
category=`larch parse-package-name -b $revision`
branch=`larch parse-package-name $revision`
vsn=`larch parse-package-name -v $revision`
lvl=`larch parse-package-name -l $revision`

here="`pwd`"

cd "$dir"
dir="`pwd`"
wdroot="`larch tree-root`"

################################################################
# Greetings
# 
#  This is an internal command.

if test ! -z "$verbose" ; then 
  ARCH__OUTLINE_DEPTH="$ARCH__OUTLINE_DEPTH*"
  larch heading "take-from-cache\\n"
  printf "arguments: %s\\n" "$command_line" | fold -w 60 | larch body-indent
  larch heading --sub "take-from-cache start time: %s\\n" "`date`"
  larch heading --sub "seeking revision: %s\\n" "$branch--$vsn--$lvl"
  larch heading --sub "to copy to: %s\\n" "$here"
  larch heading --sub "project tree: %s\\n" "$wdroot"
fi

################################################################
# Look for Cached Copies
# 

if test -d "$wdroot/{arch}/++pristine-trees/unlocked/$category/$branch/$branch--$vsn/$archive/$branch--$vsn--$lvl" ; then

  if test ! -z "$report" ; then
    larch heading --sub "taking revision from local cache: %s\\n" $branch--$vsn--$lvl
  fi

  cd "$here"
  rm -rf ,,take-from-cache.$$
  mv "$wdroot/{arch}/++pristine-trees/unlocked/$category/$branch/$branch--$vsn/$archive/$branch--$vsn--$lvl" ,,take-from-cache.$$

  cd ,,take-from-cache.$$ ; 
  ls -a \
  | grep -v -E "^\\.\\.?\$" \
  | awk '{ print "mv \"" $1 "\" .."; }' \
  | $ARCH_SHELL

  cd ..
  rmdir ,,take-from-cache.$$
  rm -f ++cached-index

  if test ! -z "$verbose" ; then
    larch heading --sub "take-from-cache finish time: %s\\n" "`date`"
  fi

  exit 0

fi

if test ! -z "$verbose" ; then
  larch heading --sub "no cached copy found\\n"
  larch heading --sub "take-from-cache finish time: %s\\n" "`date`"
fi

exit 1

