#!/bin/sh INSTALLURL=$(awk '/^./ && ! /^#/ { print ; exit }' /etc/installurl) export PKG_PATH="${INSTALLURL}/$(uname -r)/packages-stable/$(uname -p)/" CACHE_DIR=/var/cache/pkgupdate/ SILENT=0 # hide output if SILENT is activated silent() { if [ "$SILENT" -eq 0 ] then echo "$1" fi } # curl can only be used for http:// , it's quite complicated with https:// # due to the way pkg_add calls $FETCH_CMD def_curl() { if type curl 2> /dev/null >/dev/null then export FETCH_CMD="/usr/local/bin/curl -L -s -q -N" fi } check_cache() { # creating cache directory if ! install -d -o root -g wheel -m 755 ${CACHE_DIR} then echo "Error when creating the cache directory ${CACHE_DIR}" exit 1 fi ftp -o - "$PKG_PATH" 2>/dev/null | grep tgz > ${CACHE_DIR}/index.now if [ $? -eq 0 ] then # check if we already used pkgupdate if [ ! -f ${CACHE_DIR}/index.previous ] then # first time we use it mv ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous else # compare current output with the previous one # if they are identical, no changes has been done if cmp ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous then rm ${CACHE_DIR}/index.now silent "No changes on the mirror." exit 0 else mv ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous fi fi else echo "error when downloading the index, return $?" exit 1 fi } do_http() { silent "Updating using $1 protocol" check_cache def_curl pkg_add -u 2>&1 | grep -v "^Couldn't find updates for " } do_https() { silent "Updating using $1 protocol" check_cache pkg_add -u 2>&1 | grep -v "^Couldn't find updates for " } unsupported() { echo "protocol not supported: $1" exit 1 } unknown() { echo "protocol not known for $1" exit 1 } if [ "$(id -u)" -ne 0 ] then echo "You need to run $0 as root" exit 1 fi if [ "$1" = "-q" ] then SILENT=1 fi if sysctl kern.version | grep -E -- "(-current|-beta)" >/dev/null then silent "Enabling -current mode" export PKG_PATH="${INSTALLURL}/snapshots/packages/$(uname -p)/" fi case "$INSTALLURL" in http://*) do_http "http://" ;; https://*) do_https "https://" ;; ftp://*) unsupported ftp ;; scp://*) unsupported scp ;; *) unknown "$INSTALLURL" ;; esac