#!/bin/sh
# -*- indent-tabs-mode: nil; tab-width: 2 -*-
#
# Copyright 2009-2012 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

setup_srcdir()
{
  srcdir=${PBUILDER_SRCDIR:-$HOME/Projects}
  srcdir=$(readlink -m "$srcdir")
}

setup_proj()
{
  proj=""
  while getopts ':p:' OPTION
  do
    case $OPTION in
    p) proj="$OPTARG"
       ;;
    esac
  done

  if [ -z "$proj" ]; then
    # Auto detect project by going up directory tree
	  dir="$PWD"
	  while [ "$dir" != "/" ]; do
		  base=$(basename "$dir")
		  dir=$(dirname "$dir")
		  if [ "$dir" = "$srcdir" ]; then
			  proj="$base"
			  break;
		  fi
	  done

    if [ -z "$proj" ]; then
      echo "Could not determine project.  Please specify one with -p." >&2
      exit 1
    fi
  fi

  mkdir -p "$srcdir/$proj"
}

setup_all()
{
  setup_srcdir $*
  setup_proj $*
}

