#!/bin/bash
set -e

# setup test fixtures
BASE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd $BASE_PATH/test/fixtures/git_submodule

if [ "$1" == "-f" ]; then
  git clean -ffX .
fi

FIXTURES="$(pwd)/nested $(pwd)/submodule $(pwd)/project"
SUBMODULE=""
echo "setting up git_submodule test fixtures"
for fixture in $FIXTURES; do
  mkdir -p $fixture
  pushd $fixture >/dev/null

  # fixture is already set up, use "-f" to force a refresh
  if [ -e ".git" ]; then
    continue
  fi

  git init -q .
  cp $BASE_PATH/LICENSE .
  git add LICENSE
  git commit -q -m "init"

  if [ -n "$SUBMODULE" ] ; then
    git submodule add -q "$SUBMODULE" "vendor/$(basename $SUBMODULE)"
    git submodule update --recursive --init -q
    git add . && git commit -q -m "update submodule"
  fi

  echo "$(basename $fixture) created"

  SUBMODULE="$(pwd)"
  popd >/dev/null
done
