#!/bin/bash

set -e

confirm_changes() {
    echo -e "\nDoes this look OK [yes/no]? "
    read -e input
    input=$(echo "${input}" | tr '[:upper:]' '[:lower:]')

    if [ "${input}" != "yes" ]; then
        exit 1
    fi
}

gitWorkdirStatus=$(git status --porcelain | grep -v -e "vendor/" -e scripts/update-govendor-dependencies || echo '')

if [[ -n "${gitWorkdirStatus}" ]]; then
    echo "Your git working directory contains uncommited changes and/or untracked files"
    echo "Please cleanup your working directory (commit, stash or remove changes) before updating govendor dependencies"
fi

echo "Removing unused packages"
govendor remove +unused

echo "Adding missing and external packages"
for package in $(govendor list --no-status +outside | grep -v "^appengine"); do
    echo "Updating ${package}..."
    govendor add ${package}
done

echo -e "\nChanges in vendor/"
git status --short
confirm_changes

echo -e "\nDiff of vendor/vendor.json"
git diff vendor/vendor.json
confirm_changes

git add vendor/
git commit -m "Updating govendor dependencies"