#!/bin/sh

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

options=""
while [ -n "$1" ]; do
    case "$1" in
    yes|-f)
	dont_force=false
	;;
    --image|-p|-N)
	options="-N $2"
	shift
	;;
    esac
    shift
done

if ${dont_force:-true}; then
    if dialog --title 'Destroying node and restarting in PicoBSD' --yesno '** Are you Sure **' 8 45; then
	true
    else
	exit
    fi
fi

    for i in 0 1 2 3; do
	for dev in ad; do
	    fdisk /dev/${dev}${i} > /dev/null 2>&1
	    if [ $? = 0 ]; then
		echo "  + /dev/${dev}${i}"
		if [ -z "${default_device}" ]; then
			default_device=/dev/${dev}${i}
		fi
	    fi
	done
    done
    echo -n "Device Choice (${default_device}): "
    read -t 20 DEVICE
    fdisk ${DEVICE:=${default_device}} > /dev/null 2>&1
    if [ $? = 1 ]; then
	echo "${DEVICE} is not a valid device choice"
	exit 1
    fi
    DEVICE=${DEVICE%s[0-9]*}
    DEVICE=${DEVICE#/dev/}

    if mount /dev/${DEVICE}s1a /usr/update/mnt >/dev/null 2>&1; then
	path=/usr/update/mnt
    else
	path=""
    fi
    mount_nfs -T picobsd.isilon.com:/picobsd /mnt &&\
    cp /mnt/kernel $path/ &&\
    disklabel -B -b /mnt/boot/boot1 \
	-s /mnt/boot/boot2 /dev/${DEVICE}s1 &&\
    rm -rf $path/boot.old &&\
    rm -rf $path/var/* &&\
    mv $path/boot $path/boot.old &&\
    reboot
