function all_packages ()
{
  top=$1
  shift
  if test -d ${base}/${release}/${top}/cmt; then
    cd ${base}/${release}/${top}/cmt
  else
    cd ${base}/${release}/${top}/*/cmt
    if test ! $? = 0; then
      echo "run_create_kit.sh> Cannot move to the top package as [${base}/${release}/${top}/*/cmt]"
      exit
    fi
  fi
  select_opt=
  if test ! "${selected}" = ""; then
    select_opt="-select='${selected}'"
  fi
  echo "all_packages> cmt broadcast -global -select=${selected} - ${dep}/create_kit.sh -release ${release} ${patch_opt} ${cycles_opt} ${platform_opt} ${pacman_cache} ${override_opt} ${source_opt}"
  cmt broadcast -global -select="${selected}" - \
    ${dep}/create_kit.sh \
      -release ${release} \
      ${patch_opt} \
      ${cycles_opt} \
      ${platform_opt} \
      ${pacman_cache} \
      ${override_opt} \
      ${source_opt} 2>&1 | cat
}
function CMT ()
{
  #
  # CMT itself 
  #
  cmtversion=`cmt -quiet version`
  echo "create_all_kits.sh> Constructing the pacman kit for CMT ${cmtversion} [${platform}] into ${pacman_cache}"
  cat ${dep}/CMTCONFIG.pacman | \
    sed -e 's#${cmtversion}#'"${cmtversion}"'#g' \
        -e 's#${CMTCONFIG}#'"${CMTCONFIG}"'#g' >| ${pacman_cache}/cache/CMTCONFIG-${cmtversion}-${platform}.pacman
  cat ${dep}/CMT.pacman | \
    sed -e 's#${cmtversion}#'"${cmtversion}"'#g' >| ${pacman_cache}/cache/CMT-${cmtversion}.pacman
}
function help ()
{
  echo "create_all_kits.sh"
  echo ""
  echo "  External parameters:"
  echo '    ${CMTPATH}       : as for the normal release usage'
  echo '    ${PACMAN_CACHE}  : where to create the kit'
  echo ""
  echo "  Arguments:"
  echo "    --help           : get this help"
  echo "    -base      : the base directory of the release [no default]"
  echo "    -release    : the release id [no default]"
  echo "    -top        : the top package [no default]"
  echo "    -p      : select this package instead of all [can be multiple]"
  echo "    -cmtversion   : the CMT version"
  echo "    -patches    : location of patches for requirements files"
  echo "    -cycles    : text file containing the output of cmt show cycles"
  echo "    -platform  : suffix characterizing the platform"
  echo "    -override        : override existings kits [default is to keep them]"
  echo "    -step      : one of (projects|all|CMT)"
  echo "    -source          : create source kits"
  echo "    [ ...]    : list of missing packages to manually add to this release"
  echo ""
}
#-----------------------------------------
# main
#-----------------------------------------
##set -x
echo "create_all_kits> args=[$*]"
# generic definitions
dep=`dirname $0`
dep=`(cd ${dep}; pwd)`
export SITEROOT=/afs/cern.ch
top=
cmtversion=`cmt version`
patch_dir=
patch_opt=
cycles_file=
cycles_opt=
platform=
platform_suffix=
platform_opt=
override_opt=
selected=
source_opt=
while test ! $# = 0; do
  arg=$1
  ##echo "arg=[${arg}]"
  if test "${arg}" = "-step"; then
    shift
    step=$1
  elif test "${arg}" = "-base"; then
    shift
    base=$1
  elif test "${arg}" = "-top"; then
    shift
    top=$1
  elif test "${arg}" = "-release"; then
    shift
    release=$1
  elif test "${arg}" = "-p"; then
    shift
    package=$1
    package=`echo ${package} | sed -e 's#^[^/]#/' -e 's#[^/]$#&/#'`
    selected="${selected} ${package}"
  elif test "${arg}" = "-cmtversion"; then
    shift
    cmtversion=$1
  elif test "${arg}" = "-patches"; then
    shift
    patch_dir=$1
    patch_opt="-patches ${patch_dir}"
  elif test "${arg}" = "-cycles"; then
    shift
    cycles_file=$1
    cycles_opt="-cycles ${cycles_file}"
  elif test "${arg}" = "-platform"; then
    shift
    platform=$1
    platform_suffix="-${platform}";
    platform_opt="-platform ${platform}";
  elif test "${arg}" = "-override"; then
    shift
    override_opt="-override"
  elif test "${arg}" = "-source"; then
    shift
    source_opt="-source"
    platform="src"
    platform_suffix="-${platform}";
    platform_opt="-platform ${platform}";
  elif test "${arg}" = "--help"; then
    help
    exit
  fi
  shift
done
#
# Checking effective arguments
#
if test "${CMTPATH}" = ""; then
  echo "CMTPATH must be defined first."
  help
  exit
fi
if test "${PACMAN_CACHE}" = ""; then
  echo "PACMAN_CACHE must be defined first."
  help
  exit
fi
if test "${base}" = ""; then
  echo "Please specify the base directory of the release using the -base option."
  help
  exit
fi
if test "${release}" = ""; then
  echo "Please specify the release id using the -release option."
  help
  exit
fi
if test "${top}" = ""; then
  echo "Please specify the top package using the -top option."
  help
  exit
fi
pacman_cache=${PACMAN_CACHE}
kits_dir=${pacman_cache}/kits
cache_dir=${pacman_cache}/cache
if test ! -d ${kits_dir}; then
  mkdir -p ${kits_dir}
fi
if test ! -d ${cache_dir}; then
  mkdir -p ${cache_dir}
fi
#echo step=${step}
#echo "selected=[${selected}]"
#echo "patch_opt=[${patch_opt}]"
#echo "cycles_opt=[${cycles_opt}]"
#echo "platform_opt=[${platform_opt}]"
#echo "pacman_cache=[${pacman_cache}]"
#echo "override_opt=[${override_opt}]"
#echo "source_opt=[${source_opt}]"
if test ! "${source_opt}" = ""; then
  ${dep}/create_project_kits.sh ${platform_opt}
  all_packages ${top}
elif test "${step}" = ""; then
  CMT
  ${dep}/create_project_kits.sh ${platform_opt}
  all_packages ${top}
else
  if test "${step}" = "projects"; then
    ${dep}/create_project_kits.sh ${platform_opt}
  elif test "${step}" = "all"; then
    all_packages ${top}
  elif test "${step}" = "CMT"; then
    CMT
  fi
fi