#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
GIT_CHECK="git status --porcelain"

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_website_deploy

# echo empty line because the buffer starts with a tab for some reason
echo ''
echo "Current time: $(date)"
echo 'Updating website - please wait until the page finishes loading'

# Go to the root folder
cd /var/www/scummvm-web

# Pull the updated website from github
echo '$ git pull --rebase'
git pull --rebase
echo '$ git status'
git status

# Run composer install
export COMPOSER_HOME=/var/www/composer
echo "Composer home:" $COMPOSER_HOME
echo '$ composer build'
composer build 2>&1

# Update News file for translation
if [[ `${GIT_CHECK} data/en/news.json` ]]; then
    echo 'Update base news translation file'
    git add data/en/news.json
    git commit --author="$1" -m "L10N: Update base news translation file"
fi

# Update data files
if [[ `${GIT_CHECK} data/en/*.yaml` ]]; then
    echo 'Update website data files'
    git add data/en/*.yaml
    git commit --author="$1" -m "DATA: Update data files"
fi

# Push changes upstream
git push

echo 'Website update complete';
