--- shakeandbake.sh	2014-05-14 10:42:44.000000000 +0200
+++ shakeandbake.sh	2015-01-21 15:12:59.000000000 +0100
@@ -6,7 +6,18 @@
 fi
 
 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH
-PACKAGESITE=ftp://pkgmaster.slu.se/pub/FreeBSD/ports/amd64/packages-9-stable/Latest/; export PACKAGESITE
+
+RUNNING_FREEBSD_VERSION=$(uname -r | awk -F '-' '{print $1}')
+RUNNING_FREEBSD_ARCH=$(uname -m)
+
+case ${RUNNING_FREEBSD_VERSION} in
+  "9.2"|"9.3"|"10.0"|"10.1") ;;
+  *)
+    echo "Unsupported FreeBSD version \"${RUNNING_FREEBSD_VERSION}\"."
+    echo "Supported versions are \"9.2\", \"9.3\", \"10.0\" or \"10.1\". Exiting."
+    exit 1
+  ;;
+esac
 
 run_command() {
 
@@ -89,18 +100,34 @@
 if [ $(zfs list -o name | grep -c 'pool1/root') -ne 0 ]; then
   run_command "zfs set readonly=off pool1/root"
 fi
-run_command "pkg_add -r pkg"
-run_command "unset PACKAGESITE"
-run_command "echo 'WITH_PKGNG=\"YES\"' >>/etc/make.conf"
-run_command "pkg2ng"
-if [ $(uname -r | awk -F '.' '{print$1}') -ge 10 ]; then
-  if grep -q 'enabled: yes' /etc/pkg/FreeBSD.conf; then
+
+if ! TMPDIR=/dev/null ASSUME_ALWAYS_YES=yes \
+  PACKAGESITE=file:///nonexistent \
+  pkg info -x 'pkg(-devel)?$' >/dev/null 2>&1; then
+
+  PACKAGESITE=ftp://pkgmaster.slu.se/pub/FreeBSD/ports/amd64/packages-9-stable/Latest/; export PACKAGESITE
+  run_command "pkg_add -r pkg"
+  unset PACKAGESITE
+fi
+
+if ! grep -q 'WITH_PKGNG="YES"' /etc/make.conf 2>/dev/null; then
+  run_command "echo 'WITH_PKGNG=\"YES\"' >>/etc/make.conf"
+fi
+
+if [ -f /var/db/pkg/pkgdb.db ]; then
+  run_command "pkg2ng"
+fi
+
+if [ -f /etc/pkg/FreeBSD.conf ]; then
+  if grep -q 'enabled: yes' /etc/pkg/FreeBSD.conf 2>/dev/null; then
     run_command "sed -i '' 's/enabled: yes/enabled: no/' /etc/pkg/FreeBSD.conf"
   fi
 fi
+
 if [ ! -d /usr/local/etc/ssl/certs ]; then
   run_command "mkdir -p /usr/local/etc/ssl/certs"
 fi
+
 if [ ! -f /usr/local/etc/ssl/certs/pkg.cert ]; then
   run_command "cat > /usr/local/etc/ssl/certs/pkg.cert << EOF
 -----BEGIN PUBLIC KEY-----
@@ -119,14 +146,10 @@
 -----END PUBLIC KEY-----
 EOF"
 fi
+
 if [ ! -f /usr/local/etc/pkg.conf ]; then
-  case "$(uname -r | awk -F '-' '{print$1}')" in
-    "9.2") run_command "cat > /usr/local/etc/pkg.conf << EOF
-PUBKEY              : /usr/local/etc/ssl/certs/pkg.cert
-PACKAGESITE         : http://pkgmaster.slu.se/repo/release92amd64-default
-MIRROR_TYPE         : http://
-PORTAUDIT_SITE      : http://portaudit.FreeBSD.org/auditfile.tbz
-REPO_AUTOUPDATE	    : YES
+  run_command "cat > /usr/local/etc/pkg.conf << EOF
+REPO_AUTOUPDATE     : YES
 ALIAS               : {
   all-depends: query %dn-%dv,
   annotations: info -A,
@@ -146,35 +169,57 @@
   size: info -sq,
   }
 EOF"
+fi
+
+REPOS_DIR="/usr/local/etc/pkg/repos"
+
+if [ ! -d ${REPOS_DIR} ]; then
+  run_command "mkdir -p ${REPOS_DIR}"
+fi
+
+if [ ! -f ${REPOS_DIR}/pkgmaster.conf ]; then
+  run_command "echo 'pkgmaster: {' > ${REPOS_DIR}/pkgmaster.conf"
+  case "${RUNNING_FREEBSD_VERSION}" in
+    "9.2")
+      run_command "echo '    url             : \"http://pkgmaster.slu.se/repo/release92amd64-default\",' >> ${REPOS_DIR}/pkgmaster.conf"
     ;;
-    "10.0") run_command "cat > /usr/local/etc/pkg.conf << EOF
-PUBKEY              : /usr/local/etc/ssl/certs/pkg.cert
-PACKAGESITE         : http://pkgmaster.slu.se/repo/release10amd64-default
-MIRROR_TYPE         : http://
-PORTAUDIT_SITE      : http://portaudit.FreeBSD.org/auditfile.tbz
-REPO_AUTOUPDATE	    : YES
-ALIAS               : {
-  all-depends: query %dn-%dv,
-  annotations: info -A,
-  build-depends: info -qd,
-  download: fetch,
-  iinfo: info -i -g -x,
-  isearch: search -i -g -x,
-  leaf: query -e \"%a == 0\" \"%n-%v\",
-  leaf: query -e \"%a == 0\" \"%n-%v\",
-  list: info -ql,
-  origin: info -qo,
-  provided-depends: info -qb,
-  raw: info -R,
-  required-depends: info -qr,
-  shared-depends: info -qB,
-  show: info -f -k,
-  size: info -sq,
-  }
-EOF"
+    "9.3")
+      run_command "echo '    url             : \"http://pkgmaster.slu.se/repo/release93amd64-default\",' >> ${REPOS_DIR}/pkgmaster.conf"
+    ;;
+    "10.0")
+      case "${RUNNING_FREEBSD_ARCH}" in
+        "amd64")
+          run_command "echo '    url             : \"http://pkgmaster.slu.se/repo/release10amd64-default\",' >> ${REPOS_DIR}/pkgmaster.conf"
+        ;;
+        "i386")
+          run_command "echo '    url             : \"http://pkgmaster.slu.se/repo/release10i386-default\",' >> ${REPOS_DIR}/pkgmaster.conf"
+        ;;
+      esac
+    ;;
+    "10.1")
+      run_command "echo '    url             : \"http://pkgmaster.slu.se/repo/release101amd64-default\",' >> ${REPOS_DIR}/pkgmaster.conf"
     ;;
   esac
+  run_command "cat >> ${REPOS_DIR}/pkgmaster.conf << EOF
+    signature_type  : \"PUBKEY\",
+    pubkey          : \"/usr/local/etc/ssl/certs/pkg.cert\",
+    enabled         : yes,
+    mirror_type     : NONE,
+}
+EOF"
 fi
+
+if [ -f ${REPOS_DIR}/FreeBSD.conf ]; then
+  if grep -q 'enabled: yes' ${REPOS_DIR}/FreeBSD.conf 2>/dev/null; then
+    run_command "sed -i '' 's/enabled: yes/enabled: no/' ${REPOS_DIR}/FreeBSD.conf"
+  fi
+else
+  run_command "echo 'FreeBSD: { enabled: no }' > ${REPOS_DIR}/FreeBSD.conf"
+fi
+
+run_command "pkg upgrade -fy pkg"
+run_command "pkg update -f"
+
 run_command "pkg install -y bash smartmontools samba36 pam_mkhomedir sudo zfsnap subversion ssmtp pkgconf openssl"
 run_command "sed -i '' \"/# root:	me@my.domain/{G;s/$/root:	storageadm@slu.se/;}\" /etc/aliases"
 run_command "newaliases"
