#!/bin/bash

# Mirrors a subset of CSW packages for pkgutil to install

### Configurable settings ####################################################

PKG_SUBSET_MAIN="pkgutil wget rsync gangliaweb nagios netsnmp git gitk \
        coreutils dtracetoolkit gtar openssl lsof netcat socat vim xtail \
        ganglia_modules_solaris"
MIRROR_MAIN=http://mirror.opencsw.org/opencsw/unstable
# If commented, won't try to fetch:
PKG_SUBSET_EXPERIMENTAL="sqsh:sqsh_ocs sqsh:sqsh_freetds"
MIRROR_EXPERIMENTAL=http://buildfarm.opencsw.org/opencsw/experimental

# Where to put the downloads:
PKG_DEST=${HOME}/csw/pkgmirror

# Which versions to fetch:
PKG_VERSIONS=5.10
#PKG_ARCH_SET="i386 sparc"
PKG_ARCH_SET="sparc"

# Which bin dir contains pkgutil and bldcat
# We don't use /opt/csw/bin for now because bldcat isn't in the package:
#PKGUTIL_BINDIR=/opt/csw/bin
# therefore, we are using pkgutil from SVN, as bldcat lives with it:
# to get it from SVN:
#  mkdir ${HOME}/csw && \
#  svn co https://pkgutil.svn.sourceforge.net/svnroot/pkgutil/trunk \
#      ${HOME}/csw/pkgutil-trunk
PKGUTIL_BINDIR=${HOME}/csw/pkgutil-trunk

# The binary names
PKGUTIL="${PKGUTIL_BINDIR}/pkgutil"
BLDCAT="${PKGUTIL_BINDIR}/bldcat"

### End of configurable settings #############################################

if [ ! -x ${PKGUTIL} ];
then
  echo "Not found/not executable: ${PKGUTIL}"
  exit 1
fi

if [ ! -x ${BLDCAT} ];
then
  echo "Not found/not executable: ${BLDCAT}"
  exit 1
fi

mkdir -p ${PKG_DEST} || exit 1

for PKG_ARCH in ${PKG_ARCH_SET} ;
do

  for PKG_VERSION in ${PKG_VERSIONS} ;
  do
    # Identify the platform, create a directory:
    PKG_PLATFORM="${PKG_ARCH}:${PKG_VERSION}"
    PKG_PLATFORM_DEST="${PKG_DEST}/${PKG_ARCH}/${PKG_VERSION}"
    mkdir -p ${PKG_PLATFORM_DEST} || exit 1

    # Get the main packages we want:
    ${PKGUTIL} -d -y -t ${MIRROR_MAIN} -T ${PKG_PLATFORM} \
       -P ${PKG_PLATFORM_DEST} ${PKG_SUBSET_MAIN} || exit 1

    # Get any experimental packages we want:
    if [ ! -z "${PKG_SUBSET_EXPERIMENTAL}" ];
    then
      for PKG_INFO in ${PKG_SUBSET_EXPERIMENTAL} ;
      do
        PKG_REPO=`echo ${PKG_INFO} | cut -f1 -d:`
        PKG_NAME=`echo ${PKG_INFO} | cut -f2 -d:`
        # Notice the URL is different for each package in experimental:
        ${PKGUTIL} -d -y -t ${MIRROR_EXPERIMENTAL}/${PKG_REPO} \
          -T ${PKG_PLATFORM} \
          -P ${PKG_PLATFORM_DEST} ${PKG_NAME} || exit 1
      done
    fi
    # Create the catalog file:
    ${BLDCAT} ${PKG_PLATFORM_DEST} || exit 1
  done
done


