#!/bin/bash
PROJECT_DIR="$1"
GIT_URL="$2"

if [[ "$PROJECT_DIR" = "" || "$GIT_URL" = "" ]]; then
  echo "Usage: ./create_project <DIR> <GIT_URL>"
  exit 1
fi

set -e

project_dir_parent=`dirname "$PROJECT_DIR"`
fstype=`df -T "$project_dir_parent" | tail -n 1 | awk '{ print $2 }'`
if [[ "$fstype" == vmhgfs || "$fstype" == vboxfs ]]; then
  echo "You cannot put the project dir in a VMWare or VirtualBox shared folder."
  exit 1
fi

if [[ `id -u` != 0 ]]; then
  echo "You must run this command with root privileges."
  exit 1
fi

set -x

mkdir "$PROJECT_DIR"
mkdir "$PROJECT_DIR/git"
mkdir "$PROJECT_DIR/cache"
mkdir "$PROJECT_DIR/cache/root"
mkdir "$PROJECT_DIR/cache/mock_lib"
mkdir "$PROJECT_DIR/cache/mock_cache"
mkdir "$PROJECT_DIR/repo"
mkdir "$PROJECT_DIR/build"

echo "$GIT_URL" > "$PROJECT_DIR/git_url"
(cd "$PROJECT_DIR" && ln -s build/log/state.log state.log)

chown -R jenkins: "$PROJECT_DIR"
chmod u=rwx,g=x,o= "$PROJECT_DIR" "$PROJECT_DIR/cache"
setfacl -m u:www-data:rx "$PROJECT_DIR" "$PROJECT_DIR/repo"
setfacl -d -m u:www-data:rx "$PROJECT_DIR/repo"
