#!/bin/bash

GITADDR="{$GITROOTURL}/{$USERNAME}/{$PROJECT}"
GIT_SDIR="{$CODE_DIR}"

GIT_USER_DIR="${GIT_SDIR}/{$USERNAME}"
GIT_PROJECT_DIR="${GIT_USER_DIR}/{$PROJECT}"

# echo $GIT_PROJECT_DIR
if [ ! -d $GIT_PROJECT_DIR ];then
	mkdir -p $GIT_USER_DIR && cd $GIT_USER_DIR
	git clone $GITADDR
fi

unset GIT_DIR
cd $GIT_PROJECT_DIR && git pull

# func 2
# cd $GIT_PROJECT_DIR && env -i git pull origin master



WEB_PATH={$WEB_ROOT}/{$USERNAME}/{$PROJECT}
mkdir -p $WEB_PATH

rsync -vauP --delete --exclude=".*" $GIT_PROJECT_DIR/ $WEB_PATH

sysName=`uname`
if [ $sysName == 'Darwin' ]; then
	USER=$(who | sed -n "2,1p" |awk '{print $1}')
	chown -R $USER:staff $WEB_PATH
else
	chown -R www:www $WEB_PATH
fi