#!/bin/bash
appDir=$(dirname $(dirname $0))
destinationDir=$(pwd)
tempDir=$(mktemp -d)

rsync -a --delete $appDir/ $tempDir

# Cleaning up the built website
find $tempDir \
    -mindepth 1 -maxdepth 1 -and \
    -name "*" -and \
    -not \( \
        \( \
            -path "$tempDir/app" -or \
            -path "$tempDir/public" -or \
            -path "$tempDir/themes" \
        \) \
        -prune \
    \) -and \
    -not -name ".htaccess" -and \
    -not -name "composer.*" \
    -exec rm -rf {} +

# Cleaning up the destination directory
find $destinationDir \
    -mindepth 1 -maxdepth 1 -and \
    -name "*" -and \
    -not -name ".env" \
    -exec rm -rf {} +

# Copying the built website to the destination
rsync -a $tempDir/ $destinationDir

# Installing the website
cd $destinationDir
composer install
composer exec -- sake dev/build