#!/usr/bin/env bash

# Do this before "set -e", as sage-env might give (harmless) errors.
source local/bin/sage-env

set -e

# People often move the Sage install right before doing the upgrade, so it's
# important to fix any path hardcoding issues first, or certain library
# links will fail.
sage-location

usage () {
    CMD="${0##*/}"
    echo "usage: $CMD [version]"
}

if [ "$#" -gt 1 ]; then
    usage
    exit 1
fi

if [ "$#" -gt 0 ]; then
    BRANCH="$1"
    shift
    echo "Upgrading to $BRANCH"
else
    BRANCH="master"
    echo "Upgrading to the latest development version"
fi

git fetch origin "$BRANCH"
git merge --ff-only FETCH_HEAD
