#!/bin/sh
#
# Copyright (c) 2008 Daniel Schierbeck
#
# Export the contents of the git index to a specified directory

USAGE="[-f | --force] <destination>"
LONG_USAGE="Export the contents of the git index to the specified directory"
SUBDIRECTORY_OK=Yes
OPTIONS_SPEC=
. "`git --exec-path`/git-sh-setup"

force=0
destination=

args=`getopt -o fh -l force,help,he,hel -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$args"

while true ; do
  case "$1" in
    -f|--force)
      force=1
      shift
      ;;
    -h|--h|--he|--hel|--help)
      usage
      shift
      ;;
    --)
      shift; break ;;
    *)
      die "Invalid argument found!"
      exit 2;
      ;;
  esac
done

if [ $# -ne 1 ]
then 
  die "You can only export to a single destination"
  exit 1
fi

if [ -e $1 -a $force -ne 1 ]
then
  die "$1 already exists. Use --force to overwrite any existing files."
  exit 1
fi

destination=$1

cd_to_toplevel

echo "Exporting git repository to ${destination}"

git checkout-index -a --prefix=${destination}/
