#!/bin/sh
# Fetch the Base OS from a remote location
# Rick van der Zwet <info@rickvanderzwet.nl>

OS_RELEASE=`sysctl -n kern.osrelease`
BASE_URL=ftp://ftp.nl.freebsd.org/pub/FreeBSD/releases/i386/${OS_RELEASE}
RSYNC_AVAILABLE=0

rsync --version >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  RSYNC_AVAILABLE=1
fi

for TYPE in kernels base manpages; do
  mkdir -p os/$TYPE
  if [ $RSYNC_AVAILABLE -eq 0 ]; then
    echo -n "# fetching the binary $TYPE distribution using ftp ... "
    # Quick  hack to see we have downloaded all already
    (cd os/$TYPE; md5 *) | grep -v 'CHECKSUM' | diff os/$TYPE/CHECKSUM.MD5 -
    if [ $? -eq 0 ]; then
      echo "DONE (already fully downloaded)"
    else
       (progress; cd os/$TYPE; echo 'mget *' | ftp -i $BASE_URL/$TYPE/) || exit 1
      echo "DONE"
    fi
  else
    echo -n "# fetching the binary $TYPE distribution using rsync ... "
    rsync -az ftp.nl.freebsd.org::FreeBSD/releases/i386/${OS_RELEASE}/$TYPE os || exit 1
    echo "DONE"
  fi
done
