#!/bin/sh

# Author: Torsten Zuehlsdorff
# Date  : 2009-11-13
# Contact: foo@meisterderspiele.de 
# (add NOSPAM to subject, german mailfilters mistrust english language ;) )
# Tested with FreeBSD 8.0

# See copyright at bottom of script

#############################
### config section: start ###
#############################

NumOfJobsForMake=4 

###########################
### config section: end ###
###########################

echo "this script updates ALL jails!"
echo "this is dangerous and can cause REAL DAMAGE"
echo "make sure that you REALLY HAVE AN ACTUAL BACKUP!"
echo "do you HAVE AN ACTUAL BACKUP?"
echo "[yes|no]"

read BackupRequest

if [ "yes" != "$BackupRequest" ] ; then
  echo "come back if an actual backup exists ;)"
  exit 1
fi

echo ""
echo "please make sure, that you've alreay rebuild you world:"
echo "# cd /usr/src && make cleanworld && make buildworld"
echo "is the world already rebuild?"
echo "[yes|no]"
read IsWorldRebuilded

if [ "yes" != "$IsWorldRebuilded" ] ; then
  echo "rebuild the world and re-run the script"
  exit 1
fi

echo ""
echo "please make sure, that the update runs within a screen (port: sysutils/screen)"
echo "if you're session crash during a backup, the jails will be in an broken state"
echo "screen will reduce this risk"
echo "are you running the script in screen? (you're free to lie and hope)"
echo "[yes|no]"
read RunningInScreen

if [ "yes" != "$RunningInScreen" ] ; then
  echo "start again, if you're running the script within screen"
  exit 1
fi

# set pathes
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

# get a list of all pathes to the jails
echo "Get list of all jails"
JailList=$(jls -a | grep -v "^   JID" | awk '{ print $4 }')

# shutdown the jails for the update
echo "stop the jails"
/etc/rc.d/jail stop

# check if all jails are stopped
CheckJailShutdown=$(jls | grep -v "^   JID")

if [ -n "$CheckJailShutdown" ] ; then
  echo "Error: not all jails are stopped"
  exit 1
fi


echo "start updating the jails"

# iterate through jail list and update every one
for JailPath in ${JailList} ; do

  # check if the given path to the jail exists
  if [ ! -e ${JailPath} ] ; then
    echo "given path to jail do not exists: {$JailPath}"
    echo "Skipping the jail - hit any key to continue"
    read SkipMessage
    continue
  fi
  
  # upgrade the world of the jail
  echo "start install world for $JailPath"
  cd /usr/src && make -j $NumOfJobsForMake installworld DESTDIR=${JailPath}
  echo "finish install world for $JailPath"
  
  # merge the old config-files of the jails with their new versions
  echo "prepare for merging various config files for $JailPath"
  echo "hit any key to continue"
  read StartMerging
  
  mergemaster -iU -F -D ${JailPath}
  
  echo "finish merging the files for $JailPath"
  echo "hit any key to proceed with the next upgrade"
  read ProceedWithUpgrade
  
done

echo "finish upgrading the jails"
echo "starting the jails"

/etc/rc.d/jail start

echo "finish starting the jails"

echo "all done"

exit 0

# ---------------------------------
#  Copyright (c) 2009 Torsten Zuehlsdorff
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.