#!/bin/bash

####################################################################
# System Update
#
# This script does an in-place update of core system files including
# the `iqb` task runner and core system tasks
#
# By default, this script updates only the handful of core system
# files.  To update any other files you can run
#
#     iqb update <filenames>
#
# For example, to pull the updated .gitignore and .eslintrc:
#
#     iqb update .eslintrc .gitignore
#
####################################################################

if [ ! -z "$(git status -s)" ]; then
    echo
    echo '!! You have uncommitted changes.  Please stash or commit your changes before updating'
    echo
    exit 1
fi

git remote | grep bedrock > /dev/null
if [ $? -gt 0 ]; then
    git remote add bedrock git://github.com/relateiq/bedrock.git -t iqproj
fi

git fetch bedrock
git reset

if [ -z "$1" ]; then
    system_files=(
        bin/iqb
        build/deploy
        build/start-server
        build/update
        docker-dev/Dockerfile
        docker-dev/README.md
        docker/Dockerfile
        docker/package.json
        docker/README.md
    )

    # update the core system files
    git checkout bedrock/iqproj-webmodule "${system_files[@]}"
    git add -- "${system_files[@]}"
else
    # update one specific file (or set of files)
    git checkout bedrock/iqproj-webmodule "$@"
    git add -- "$@"
fi

if [ ! -z "$(git status -s)" ]; then
    git commit -m "update bedrock"
    echo '---- Done -----'
else
    echo 'Already up to date'
fi
