#!/bin/sh -e

case $(auto-ostype) in
FreeBSD)
    : ${PORTSDIR:=/usr/ports}
    LOCALBASE=$(cluster-localbase)

    auto-check-ports-branch
    save_cwd=$(pwd)
    cd $PORTSDIR
    printf "Stashing bsd.local.mk...\n"
    git stash push Mk/bsd.local.mk

    printf "Pulling...\n"
    git pull

    printf "Unstashing...\n"
    git stash pop
    cd $save_cwd
    
    if [ -e $LOCALBASE/sbin/wip-update ]; then
	wip-update
    fi
    
    procs=0
    for node in `cluster-all-nodes`; do
	printf "Syncing ports to $node...\n"
	if ! rsync --archive --inplace --delete \
		/usr/ports ${node}:/usr; then
	    printf "$node\n" >> cluster-ports-update-failed-nodes
	fi
    done
    ;;

*)
    printf "$0: Not supported on $(auto-ostype).\n"
    exit 1
    ;;

esac
