{% if pillar['laradev'] is defined %}
  {%- for project in pillar['laradev'] %}
    {%- set repo = pillar['laradev'][project]['repo'] %}
#{{project}}
mkdir -p "/sites/{{project}}"
cd "/sites/{{project}}"
branches=`git ls-remote {{repo}} | grep -o -P "(?<=refs/heads/).*"`
echo "Branches:<br/>" > /sites/{{project}}/index.php
for branch in $branches; do
echo " <a href='$branch/public/'>$branch<br/>" >> /sites/{{project}}/index.php
  #create/update nginx conf file
  sed -e "s/-branch-/$branch/" -e "s/-server-/{{project}}/" /root/scripts/laradev-branch-nginx.template > /etc/nginx/conf.d/laradev-{{project}}-$branch.conf
  #clone new branch if necessary
  if [ ! -d "$branch" ];then
    git clone -b $branch {{repo}} "$branch"
    chown http:http -R "$branch"
    cd "$branch"
    cp .env.example .env
    mysql -u root -e "DROP DATABASE laradev_{{project}}_$branch"
    mysql -u root -e "CREATE DATABASE laradev_{{project}}_$branch"
    mysql -u root -e "GRANT ALL ON laradev_{{project}}_$branch.* TO laradev@localhost"
    sed -i "s/DB_DATABASE=.*/DB_DATABASE=laradev_{{project}}_$branch/" .env
    sed -i "s/DB_USERNAME=.*/DB_USERNAME=laradev/" .env
    sed -i "s/DB_PASSWORD=.*/DB_PASSWORD={{password}}/" .env
    su -s /bin/bash http -c "composer install"
    php artisan key:generate
    php artisan migrate
    php artisan db:seed
    php artisan vendor:publish --all
    php artisan storage:link
    cd "/sites/{{project}}"
  fi
  #update to latest commit, dropping all local changes
  cd "$branch"
  git fetch --all
  git reset --hard origin/$branch
  chown http:http -R ./
  su -s /bin/bash http -c "composer update"
  su -s /bin/bash http -c "composer install"
  php artisan migrate
  cd "/sites/{{project}}"
  chown http:http -R "$branch"
done
chown http:http "/sites/{{project}}" -R
  {%- endfor -%}
systemctl restart nginx
{% endif %}