#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-update-scripts
# BEFORE: middlewared earlykld

. /etc/rc.freenas

name="ix-update-scripts"
start_cmd="update_scripts"
stop_cmd=":"
status_cmd=":"

update_scripts() {
    # If there are update-related scripts, run themm, clean up, and reboot
    if [ -d /update-scripts ]; then
	echo "Running upgrade scripts..."
	mount -uw /
	# fdescfs required for faster python
	if ! mount | grep -q 'on /dev/fd (' 2> /dev/null; then
		mount -t fdescfs fdescfs /dev/fd
	fi
	if [ -f /update-scripts/order ]; then
	    cat /update-scripts/order | while read name
	    do
		sh -x /update-scripts/${name}
	    done
	else
	    # Just run them alphabetically
	    for name in /update-scripts/*
	    do
		sh -x ${name}
	    done
	fi
	rm -rfx /update-scripts
	reboot
    fi
    return 0
}

load_rc_config ${name}
run_rc_command $*
