Index: freebsd-mgnt/system-update.sh
===================================================================
--- freebsd-mgnt/system-update.sh	(revision 192)
+++ freebsd-mgnt/system-update.sh	(revision 193)
@@ -6,4 +6,9 @@
 
 . /etc/rc.subr
+
+# Pointers to avoid confusion corresponts with logical boolean style values.
+# WARNING do not conpare with exit codes!
+FALSE=0
+TRUE=1
 
 ask_question() {
@@ -17,4 +22,14 @@
 }
 
+echo_list() {
+  PREFIX=$1
+  shift
+  for ENTRY in $*; do
+    echo "$PREFIX $ENTRY"
+  done
+}
+
+#
+# SuperCow power only
 if [ `id -u` -ne 0 ]; then
     echo "Sorry root only" 1>&2
@@ -22,8 +37,55 @@
 fi
 
-if [ -z "`whereis -qb portupgrade`" ]; then
-    echo "ERROR: ports-mgnt/portupgrade not found!" 
+
+#
+# Check which ports-mgnt tool to use to upgrade outdated ports
+#
+FORCE_PORTUPGRADE=${FORCE_PORTUPGRADE:-$FALSE}
+FORCE_PORTMASTER=${FORCE_PORTMASTER:-$FALSE}
+
+PORTUPGRADE_INSTALLED=$FALSE
+PORTMASTER_INSTALLED=$FALSE
+USE_PORTMGNT="none"
+if [ -n "`whereis -qb portupgrade`" ]; then
+  PORTUPGRADE_INSTALLED=$TRUE
+fi
+if [ -n "`whereis -qb portmaster`" ]; then
+  PORTMASTER_INSTALLED=$TRUE
+fi
+
+if [ $FORCE_PORTMASTER -eq $TRUE ]; then
+  if  [ $PORTMASTER_INSTALLED -eq $FALSE ]; then 
+    echo "# ERROR: FORCE_PORTMASTER set but binary not found"
     exit 1
-fi
+  fi
+  USE_PORTMGNT="portmaster"
+elif [ $FORCE_PORTUPGRADE -eq $TRUE ]; then
+  if [ $PORTUPGRADE_INSTALLED -eq $FALSE ]; then 
+    echo "# ERROR: FORCE_PORTUPGRADE set but binary not found"
+    exit 1
+  fi
+  USE_PORTMGNT="portupgrade"
+else
+  if [ $PORTUPGRADE_INSTALLED -eq $TRUE ]; then
+    if [ $PORTMASTER_INSTALLED -eq $TRUE ]; then
+      echo "# ERROR: both portmaster and portupgrade are installed"
+      echo "# Pick your favorite by setting either FORCE_PORTMASTER=1 or FORCE_PORTUPGRADE=1"
+      exit 1
+    fi
+    USE_PORTMGNT="portupgrade"
+  else
+    if [ $PORTMASTER_INSTALLED -eq $FALSE ]; then
+      echo "# WARNING: ports-mgnt/portupgrade not found!" 
+      echo "# WARNING: ports-mgnt/portmaster not found!" 
+      echo "# ERROR  : Not port upgrade tool installed"
+      exit 1
+    fi
+    USE_PORTMGNT="portmaster"
+  fi
+fi
+
+
+ 
+
 
 DIRNAME=`dirname $0`
@@ -32,4 +94,11 @@
 # Get list of rc.d marked for updates
 RC_FILES=`$DIRNAME/port-list.sh`
+RUNNING_DAEMONS=""
+
+for RC_FILE in $RC_FILES; do 
+  $RC_FILE onestatus | grep -q 'is running as' && {
+    RUNNING_DAEMONS="$RUNNING_DAEMONS $RC_FILE"
+  }
+done
 
 # Update port tree
@@ -39,8 +108,16 @@
 # Update ports listing
 echo "# Checking ports which needs updating ..."
-NEWPORTS=`pkg_version -qL=`
+NEWPORTS=`pkg_version -qol '<'`
+
+# Check for potentional trouble.
+# XXX: Weird -q does not strip at '!' check
+BROKENPORTS=`pkg_version -qol '!' | awk '{print $1}'`
+if [ -n "$BROKENPORTS" ]; then
+  echo "# WARNING comparion failed, fix by hand"; echo_list "# - " $BROKENPORTS
+fi
 
 if [ -z "$NEWPORTS" ]; then
-  echo "# System up2date"
+  echo "# No ports to update"
+  echo "# All done, goodbye"
   exit 0
 fi
@@ -68,28 +145,60 @@
   
   # agree to the terms of the game
-  echo $NEWPORTS
-  ask_question "Do you like to upgrade the affected ports" "no" || exit 1
+  echo "# Ports pending update:"
+  echo_list "# - " $NEWPORTS
+  ask_question "# Do you like to upgrade the affected ports" "no" || exit 1
 
   #
   # Stop daemons
+  DAEMONS_STOPPED=$FALSE
   if [ -n "$RC_FILES" ]; then
-    echo $RC_FILES
-    ask_question "Do you like to stop your deamons" "yes" && {
-      for RC_FILE in $RC_FILES; do
-        $RC_FILE stop
-      done
-    }
-  fi
-fi
-portupgrade -r -mBATCH=yes -a $NEWPORTS
-
-#
-# Restart daemons
+    echo "# Affected daemons:"; echo_list "# - " $RC_FILES
+    if [ -n "$RUNNING_DAEMONS" ]; then
+      echo "# Running  daemons:"; echo_list "# - " $RUNNING_DAEMONS
+      ask_question "# Do you like to stop running daemons" "yes" && {
+        for RC_FILE in $RUNNING_DAEMONS; do
+          $RC_FILE stop
+        done;
+        DAEMONS_STOPPED=$TRUE
+      }
+    fi
+  fi
+fi
+echo "# Using update tool '$USE_PORTMGNT"
+case $USE_PORTMGNT in 
+  portupgrade)
+    portupgrade -r -mBATCH=yes -a $NEWPORTS
+    ;;
+  portmaster)
+    portmaster -d --no-confirm --no-term-title --no-index-fetch -mBATCH=yes -a $NEWPORTS 
+    ;;
+  *)
+    echo "# ERROR: Failed to find a port management tool"
+    exit 1
+    ;;
+esac
+
+#
+# (Re)start daemons. Assume we only like to start the just stopped ones.
 if [ -n "$RC_FILES" ]; then
-  echo $RC_FILES
-  ask_question "Do you like to restart your deamons" "yes" && {
-    for RC_FILE in $RC_FILES; do
-      $RC_FILE restart 
-    done
-  }
-fi
+  echo "# Affected daemons:"; echo_list "# - " $RC_FILES
+  if [ -n "$RUNNING_DAEMONS" ]; then
+    if [ $DAEMONS_STOPPED -eq $TRUE ]; then
+      echo "# Enabled daemons: "; echo_list "# - " $RUNNING_DAEMONS
+      ask_question "# Do you like to start your daemons" "yes" && {
+        for RC_FILE in $RUNNING_DAEMONS; do
+          $RC_FILE start 
+        done
+      }
+    else
+      echo "# Running daemons: "; echo_list "# - " $RUNNING_DAEMONS
+      ask_question "# Do you like to restart your daemons" "yes" && {
+        for RC_FILE in $RUNNING_DAEMONS; do
+          $RC_FILE restart 
+        done
+      }
+    fi
+  fi
+fi
+
+echo "# All done, goodbye"
