#!/usr/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.


##############################################################################
# HOUSEKEEPING
##############################################################################

umask 022

export PATH=/usr/bin:/usr/sbin

##############################################################################
# GLOBAL VARIABLES
##############################################################################

# For those who do not want to have to specify the '-s' option with every
# invokation of this command, just uncomment BASEDIR and set its value
# accordingly.

export BASEDIR=""
export REPOSITORY=""
export SHOW_DETAIL=0
export LOCAL_ONLY=0
export ENABLE_FOLDING=0

export COLLECT_PKGMAP=${COLLECT_PKGMAP:=1}
export COLLECT_NAMES=${COLLECT_NAMES:=1}
export COLLECT_DEPENDENCIES=${COLLECT_DEPENDENCIES:=1}
export COLLECT_REVERSE_DEPENDENCIES=${COLLECT_REVERSE_DEPENDENCIES:=1}

##############################################################################
# Function: registerEntry
##############################################################################

registerEntry()
{
   fileName="${1}"
   item="${2}"

   [ -z "${fileName}" ] && return
   [ -z "${item}" ] && return

   if [ -f ${fileName} ]; then
      if [ $(cat "${fileName}" | grep -c "^${item}$") -eq 0 ]; then
         echo "${item}" >> ${fileName}
      fi
   elif [ -d "$(dirname ${fileName})" ]; then
      # GMB: This is a small hack to avoid generating an error message
      #      when a package is listed in a "contents" file but it does
      #      not otherwise exist (e.g., SUNWphx on snv_18)
      echo "${item}" >> ${fileName}
   fi
}

##############################################################################
# Function: getPackageMap
##############################################################################

getPackageMap()
{
   [ -z "${1}" ] && return
   [ -z "${2}" ] && return

   [ ${COLLECT_PKGMAP} = 0 ] && return

   if [ -f ${1}/pkgmap ]; then
      cp ${1}/pkgmap ${2}/pkgmap 2>/dev/null
   fi
}

##############################################################################
# Function: getPackageName
##############################################################################

getPackageName()
{
   [ -z "${1}" ] && return
   [ -z "${2}" ] && return

   [ ${COLLECT_NAMES} = 0 ] && return

   name=""
   name="$(pkgparam -d ${BASEDIR} ${1} NAME 2>/dev/null)"
   if [ -z "${name}" ]; then
      for instance in $((cd ${BASEDIR}; ls -d ${1}.*) 2>/dev/null); do
         name="$(pkgparam -d ${BASEDIR} ${instance} NAME 2>/dev/null)"
         [ ! -z "${name}" ] && break
      done
   fi
   if [ -z "${name}" ]; then
      # This method should only be trusted when in "local only" mode.
      if [ ${LOCAL_ONLY} -eq 1 ]; then
         name="$(pkgparam ${1} NAME 2>/dev/null)"
      fi
   fi
   if [ ! -z "${name}" ]; then
      if [ ! -f ${2} ]; then
         echo "${name}" > ${2}
      fi
   fi
}

##############################################################################
# Function: addMissingPackages
##############################################################################

addMissingPackages()
{
   for pkgdir in $(cd ${BASEDIR}; ls); do
      [ ! -f ${BASEDIR}/${pkgdir}/pkginfo ] && continue
      pkg=`nawk -F= '($1 == "PKG") { print $2 }' ${BASEDIR}/${pkgdir}/pkginfo`
      mkdir -p ${P_DIR}/${pkg}         
      getPackageName ${pkg} ${P_DIR}/${pkg}/name
      getPackageMap ${BASEDIR}/${pkgdir} ${P_DIR}/${pkg}
      if [ ! -f ${P_DIR}/${pkg}/.processed.${pkgdir} ]; then
         if [ -f ${BASEDIR}/${pkgdir}/install/depend ]; then
            awk '/^[PR][ 	]/ { print $1" "$2 }' ${BASEDIR}/${pkgdir}/install/depend |\
               while read pkgtype pkgname; do
                  if [[ ${COLLECT_DEPENDENCIES} -eq 1 && ${pkgtype} = "P" ]]; then
                     registerEntry ${P_DIR}/${pkg}/depend.packages ${pkgname}
                     mkdir -p ${P_DIR}/${pkgname}
                     registerEntry ${P_DIR}/${pkgname}/dependby.packages ${pkg}
                  elif [[ ${COLLECT_REVERSE_DEPENDENCIES} -eq 1 && ${pkgtype} = "R" ]]; then
                     registerEntry ${P_DIR}/${pkg}/dependby.packages ${pkgname}
                     mkdir -p ${P_DIR}/${pkgname}
                     registerEntry ${P_DIR}/${pkgname}/depend.packages ${pkg}
                  fi
            done
         fi
         touch ${P_DIR}/${pkg}/.processed.${pkgdir}
      fi
   done
}

##############################################################################
# Function: processClusterTOC
##############################################################################

processClusterTOC()
{
   if [ -d ${REPOSITORY} ]; then
      if [ -f ${REPOSITORY}/.created ]; then
         return
      fi
   else
      mkdir -p ${REPOSITORY}
   fi
 
   mkdir -p ${REPOSITORY}

   exec 3<&0
   exec < ${R_CTOC}

   inCluster=0
   mCluster=0
   pkgList=""
   clusterList=""
   clusterName=""
   itemName=""

   while read line; do

      if [ ${inCluster} -eq 0 ]; then
         token=$(echo ${line} | awk -F= '$1 == "CLUSTER" { print $2 }')
         if [ ! -z "${token}" ]; then
            clusterName="${token}"
            mkdir -p ${C_DIR}/${clusterName}
            inCluster=1
         else
            token=$(echo ${line} | awk -F= '$1 == "METACLUSTER" { print $2 }')
            if [ ! -z "${token}" ]; then
               clusterName="${token}"
               mkdir -p ${MC_DIR}/${clusterName}
               inCluster=1
               mCluster=1
            fi
         fi
      elif [ "${line}" = "END" ]; then
         if [ ! -z "${itemName}" ]; then
            if [ ${COLLECT_NAMES} = 1 ]; then
               if [ ${mCluster} -eq 0 ]; then
                  echo ${itemName} > ${C_DIR}/${clusterName}/name
               else
                  echo ${itemName} > ${MC_DIR}/${clusterName}/name
               fi
            fi
         fi
         for c in ${clusterList}; do
            if [ ${mCluster} -eq 0 ]; then
               echo "WARNING: Cluster ${c} found in Cluster ${clusterName}"
            else
               registerEntry ${MC_DIR}/${clusterName}/contents.clusters ${c}
               registerEntry ${C_DIR}/${c}/partof.metacluster ${clusterName}
            fi
         done

         for p in ${pkgList}; do
            mkdir -p ${P_DIR}/${p}
            getPackageName ${p} ${P_DIR}/${p}/name
            getPackageMap ${BASEDIR}/${p} ${P_DIR}/${p}
            if [ ${mCluster} -eq 0 ]; then
               registerEntry ${C_DIR}/${clusterName}/contents.packages ${p}
               registerEntry ${P_DIR}/${p}/partof.cluster ${clusterName}
            else
               registerEntry ${MC_DIR}/${clusterName}/contents.packages ${p}
               registerEntry ${P_DIR}/${p}/partof.metacluster ${clusterName}
            fi
         
            if [ ! -f ${P_DIR}/${p}/.processed.${p} ]; then
               if [ -f ${BASEDIR}/${p}/install/depend ]; then
                  awk '/^[PR][ 	]/ { print $1" "$2 }' ${BASEDIR}/${p}/install/depend |\
                     while read pkgtype pkgname; do
                        if [[ ${COLLECT_DEPENDENCIES} -eq 1 && ${pkgtype} = "P" ]]; then
                           registerEntry ${P_DIR}/${p}/depend.packages ${pkgname}
                           mkdir -p ${P_DIR}/${pkgname}
                           registerEntry ${P_DIR}/${pkgname}/dependby.packages ${p}
                        elif [[ ${COLLECT_REVERSE_DEPENDENCIES} -eq 1 && ${pkgtype} = "R" ]]; then
                           registerEntry ${P_DIR}/${p}/dependby.packages ${pkgname}
                           mkdir -p ${P_DIR}/${pkgname}
                           registerEntry ${P_DIR}/${pkgname}/depend.packages ${p}
                        fi
                  done
               fi
               touch ${P_DIR}/${p}/.processed.${p}
            fi
         done

         clusterName=""
         itemName=""
         pkgList=""
         clusterList=""
         token=""
         inCluster=0
         mCluster=0

      else

         tmpItemName=$(echo ${line} | awk -F= '$1 == "NAME" { print $2 }')
         if [ ! -z "${tmpItemName}" ]; then
            itemName="${tmpItemName}"
            continue
         fi

         member=$(echo ${line} | awk -F= '$1 == "SUNW_CSRMEMBER" { print $2 }')
         if [ ! -z "${member}" ]; then
            if [ -d ${C_DIR}/${member} ]; then
               clusterList="${clusterList} ${member}"
            else
               pkgList="${pkgList} ${member}"
            fi
         fi
      fi
   done
       
   exec 0<&3
   exec 3<&-

   addMissingPackages
   echo "${R_CTOC}" > ${REPOSITORY}/.created
}

##############################################################################
# Function: checkItemInList
##############################################################################

ppkg=""

checkItemInList()
{
   typeset list="${1}"
   typeset item="${2}"
   typeset i=""
   typeset notFound=1

   [ -z "${list}" ] && return ${notFound}
   [ -z "${item}" ] && return ${notFound}

   for i in ${list}; do
      if [ "${i}" = "${item}" ]; then
         notFound=0
         break
      fi
   done

   return ${notFound}
}

##############################################################################
# Function: dependencies
##############################################################################

dependencies()
{
   if [ $# -gt 1 ]; then

      typeset file="$1"
      shift
      typeset args="$*"
      typeset p=""
 
      for p in ${args}; do 
         checkItemInList "${ppkg}" "${p}"
         if [ $? -eq 1 ]; then
            ppkg="${ppkg} ${p}"
            dependencies "${file}" $(cat ${P_DIR}/${p}/${file} 2>/dev/null)
            # for d in $(cat ${P_DIR}/${p}/${file} 2>/dev/null); do
            #    dependencies "${file}" "${d}"
            # done
         fi
      done
   fi
}

##############################################################################
# Function: dependenciesByPackage
##############################################################################

dependenciesByPackage()
{
   [ $# -eq 0 ] && return
   ppkg=""
   for p in $*; do
      dependencies depend.packages \
         $(cat ${P_DIR}/${p}/depend.packages 2>/dev/null)
   done
   echo "${ppkg}"
}

##############################################################################
# Function: dependenciesByCluster
##############################################################################

dependenciesByCluster()
{
   [ $# -eq 0 ] && return
   for c in $*; do
      dependenciesByPackage \
         $(cat ${C_DIR}/${c}/contents.packages 2>/dev/null)
   done
}

##############################################################################
# Function: packageToCluster
##############################################################################

packageToCluster()
{
   for p in $*; do
      if [ -f ${P_DIR}/${p}/partof.cluster ]; then
         cat ${P_DIR}/${p}/partof.cluster
      else
         echo "${p}"
      fi
   done
}

##############################################################################
# Function: dependsOnPackage
##############################################################################

dependsOnPackage()
{
   [ $# -eq 0 ] && return
   ppkg=""
   for p in $*; do
      dependencies dependby.packages \
         $(cat ${P_DIR}/${p}/dependby.packages 2>/dev/null)
   done
   echo "${ppkg}"
}

##############################################################################
# Function: dependsOnPackage
##############################################################################

dependsOnCluster()
{
   [ $# -eq 0 ] && return
   for c in $*; do
      dependsOnPackage \
         $(cat ${C_DIR}/${c}/contents.packages 2>/dev/null)
   done
}

##############################################################################
# Function: metaClusterByCluster
##############################################################################

metaClusterByCluster()
{
   [ -z "${1}" ] && return
 
   if [ -f ${C_DIR}/${1}/partof.metacluster ]; then
      cat ${C_DIR}/${1}/partof.metacluster
   fi
}

##############################################################################
# Function: metaClusterByPackage
##############################################################################

metaClusterByPackage()
{
   [ -z "${1}" ] && return

   if [ -f ${P_DIR}/${1}/partof.metacluster ]; then
      cat ${P_DIR}/${1}/partof.metacluster
   fi
   if [ -f ${P_DIR}/${1}/partof.cluster ]; then
      metaClusterByCluster $(cat ${P_DIR}/${1}/partof.cluster)
   fi
}

##############################################################################
# Function: clusterByPackage
##############################################################################

clusterByPackage()
{
   [ -z "${1}" ] && return

   if [ -f ${P_DIR}/${1}/partof.cluster ]; then
      cat ${P_DIR}/${1}/partof.cluster
   fi
}

##############################################################################
# Function: packageByCluster
##############################################################################

packageByCluster()
{
   [ -z "${1}" ] && return

   if [ -f ${C_DIR}/${1}/contents.packages ]; then
      cat ${C_DIR}/${1}/contents.packages
   fi
}

##############################################################################
# Function: packageByMetaCluster
##############################################################################

packageByMetaCluster()
{
   [ -z "${1}" ] && return
   metaCluster="${1}"

   if [ -f ${MC_DIR}/${metaCluster}/contents.packages ]; then
      cat ${MC_DIR}/${metaCluster}/contents.packages
   fi

   if [ -f ${MC_DIR}/${metaCluster}/contents.clusters ]; then
      for c in $(cat ${MC_DIR}/${metaCluster}/contents.clusters); do
         packageByCluster ${c}
      done
   fi
}

##############################################################################
# Function: clusterByMetaCluster
##############################################################################

clusterByMetaCluster()
{
   [ -z "${1}" ] && return
   metaCluster="${1}"

   if [ -f ${MC_DIR}/${metaCluster}/contents.clusters ]; then
      cat ${MC_DIR}/${metaCluster}/contents.clusters
   fi
}

##############################################################################
# Function: clusterTree
##############################################################################

clusterTree()
{
   [ -z "${1}" ] && return
   cluster="${1}"

   echo "${cluster}"
   if [ -f ${C_DIR}/${cluster}/contents.packages ]; then
      cat ${C_DIR}/${cluster}/contents.packages
   fi
}

##############################################################################
# Function: metaClusterTree
##############################################################################

metaClusterTree()
{
   [ -z "${1}" ] && return
   metaCluster="${1}"

   echo "${metaCluster}"
   if [ -f ${MC_DIR}/${metaCluster}/contents.clusters ]; then
      for cluster in $(cat ${MC_DIR}/${metaCluster}/contents.clusters); do
         clusterTree ${cluster}
      done
   fi

}

##############################################################################
# Function: packageByFile
##############################################################################

packageByFile()
{
   [ -z "${1}" ] && return
   if [ ${LOCAL_ONLY} -eq 1 ]; then
      file="${1}"
      nawk ' \
         (( $2 == "f" || $2 == "e" || $2 == "v" || $2 == "s" || $2 == "l") \
         && (( $1 == arga ) || ($1 ~ argb))) { printf("%s\n", $NF); } \
      ' arga="${file}" argb="^${file}=.*" /var/sadm/install/contents
   else
      file="$(echo ${1} | sed 's|^/\(.*\)|\1|g')"
      for pkg in $(cd ${P_DIR}; ls); do 
         targetFile="${P_DIR}/${pkg}/pkgmap"
         [ ! -f ${targetFile} ] && continue
         nawk ' \
            (( $2 == "f" || $2 == "e" || $2 == "v" || $2 == "s" || $2 == "l") \
            && (( $4 == arga ) || ($4 ~ argb))) { printf("%s\n", pkg); } \
         ' arga="${file}" argb="^${file}=*" pkg="${pkg}" ${targetFile}
      done
   fi
}

##############################################################################
# Function: evaluateByMetaCluster
##############################################################################

evaluateByMetaCluster()
{
   [ -z "${1}" ] && return

   metaCluster="${1}"
 
   for pkg in $(packageByMetaCluster ${metaCluster}); do
      pkginfo -q ${pkg}
      if [ $? -eq 1 ]; then
         echo "${pkg}"
      fi
   done
}

##############################################################################
# Function: displayItems
##############################################################################

displayItems()
{

   [ -z "${1}" ] && return
   [ -z "${2}" ] && return

   displayMode="${1}"
   itemType="${2}"
   function="${3}"
   shift 3
   args="$*"

   case "${displayMode}" in
      C) dispDirectory="${C_DIR}"  ;;
      M) dispDirectory="${MC_DIR}" ;;
      P) dispDirectory="${P_DIR}"  ;;
      *) gettext "Error: Invalid display mode in displayItems.\n" ;;
   esac

   case "${itemType}" in
      C) itemClass="Cluster"     ; itemDirectory="${C_DIR}"    ;;
      F) itemClass="File"        ; itemDirectory=""            ;;
      M) itemClass="Metacluster" ; itemDirectory="${MC_DIR}"   ;;
      P) itemClass="Package"     ; itemDirectory="${P_DIR}"    ;;
      *) gettext "Error: Invalid item type in displayItems.\n" ;;
   esac

   if [ "${MODE}" = "clusterTree" -o "${MODE}" = "metaClusterTree" ]; then
      spacer="   "
      postCmd="uniq"
   else
      spacer=""
      postCmd="sort -u"
   fi

   itemList=""
   for item in ${args}; do
      if [ "${itemType}" != "F" ]; then
         if [ ! -d ${itemDirectory}/${item} ]; then
            gettext "${itemClass} ${item} does not exist in the repository.\n"
            continue
         fi
      fi
      itemList="${itemList} $(${function} ${item})"
   done

   tmpItemList="$(echo ${itemList} | xargs -n 1 echo | ${postCmd})"
   if [ ${ENABLE_FOLDING} = 1 -a "${displayMode}" = "P" ]; then
      newItemList="$(packageToCluster ${tmpItemList})"
   else 
      newItemList="${tmpItemList}"
   fi
 
   type=""
   for i in $(echo ${newItemList} | xargs -n 1 echo | ${postCmd}); do
      if [ -f ${MC_DIR}/${i}/name ]; then
         type="M"
         name="$(cat ${MC_DIR}/${i}/name)"
      elif [ -f ${C_DIR}/${i}/name ]; then
         type="C"
         name="$(cat ${C_DIR}/${i}/name)"
      elif [ -f ${P_DIR}/${i}/name ]; then
         type="P"
         name="$(cat ${P_DIR}/${i}/name)"
      else
         type="-"
         name="-- NOT DEFINED --"
      fi

      if [ ${SHOW_DETAIL} -eq 1 ]; then
         t="[${type}] "
      else
         t=""
         name=""
      fi

      if [ "${type}" = "C" ]; then
         printf "%s%s%-25s %s\n" "${spacer}" "${t}" "${i}" "${name}"
      elif [ "${type}" = "P" ]; then
         printf "%s%s%-25s %s\n" "${spacer}${spacer}" "${t}" "${i}" "${name}"
      else
         printf "%s%-25s %s\n" "${t}" "${i}" "${name}"
      fi
   done
}

##############################################################################
# Function: initializeRepository
##############################################################################

initializeRepository()
{
   initialize=0
   if [ ! -d ${REPOSITORY} ]; then
      initialize=1
   elif [ ! -f ${REPOSITORY}/.created ]; then
      initialize=1
   fi

   if [ ${initialize} -eq 1 ]; then
      if [ -z "${BASEDIR}" ]; then
         gettext "Error: No Solaris OS distribution directory defined.\n"
         exit
      fi
   
      if [ ! -d ${BASEDIR} ]; then
         gettext "Error: Solaris OS distribution directory was not found.\n"
         exit
      fi

      if [ ${LOCAL_ONLY} -eq 1 ]; then
         CLUSTERTOC="/var/sadm/system/admin/.clustertoc"
      else
         CLUSTERTOC="${BASEDIR}/.clustertoc"
      fi

      if [ ! -f ${CLUSTERTOC} ]; then
         gettext "Error: Solaris OS .clustertoc file was not found.\n"
         exit
      fi

      export R_CTOC="${CLUSTERTOC}"

      processClusterTOC
   fi
}

##############################################################################
# setMode
##############################################################################

setMode()
{
   [ -z "${1}" ] && return

   if [ -z "${MODE}" ]; then
      MODE="${1}"
   else
      gettext "Error: Only one operating mode can be specified at a time.\n"
      exit
   fi
}

##############################################################################
# displayHelp
##############################################################################

helpMessage="
General Options:

   -F           [fold packages into their respect clusters - experimental]
   -h, -?       [show help]
   -l           [local mode - use local system repositories and data]
   -r           [repository location]
   -s           [solaris distribution location]
   -v           [show package details]

Processing Options:

   To initialize the information repository used by this tool.
      -i (used with either the -s or -l options)

   To display the list of packages contained in the specified cluster(s).
      -c <package cluster list>

   To display the metacluster(s) that contain the specified cluster(s).
      -C <package cluster list>

   To display the dependencies for the specified package(s).
      -d <package list>

   To display the dependencies for the specified cluster(s).
      -D <package cluster list>
 
   To evaluate install packages on a system against a specified metacluster.
      -e <metacluster>

   To determine what package contains a given file (experimental).
      -f <file name> (fully qualified path names only)

   To display the packages contained in the specified metacluster(s).
      -m <metacluster list>

   To display the clusters contained in the specified metacluster(s).
      -M <metacluster list>

   To display the cluster that contains the specified package(s).
      -p <package list>

   To display the metacluster(s) that contain the specified package(s).
      -P <package list>

   To display a tree view of the specified cluster(s) (experimental).
      -t <cluster list>

   To display a tree view of the specified metacluster(s) (experimental).
      -T <metacluster list>

   To display the list of packages that depend on the specified package(s).
      -z <package list>

   To display the list of packages that depend on the specified cluster(s).
      -Z <package cluster list>

"

displayHelp()
{
   gettext "${helpMessage}"
   exit
}

##############################################################################
# MAIN ROUTINE
##############################################################################

MODE=""
displayMode="P"
itemType=""

while getopts cCdDefFhilmMpPr:s:tTvzZ? v; do
   case ${v} in
      c) setMode "packageByCluster"      ; itemType="C" ;;
      C) setMode "metaClusterByCluster"  ; itemType="C" ;;
      d) setMode "dependenciesByPackage" ; itemType="P" ;;
      D) setMode "dependenciesByCluster" ; itemType="C" ;;
      e) setMode "evaluateByMetaCluster" ; itemType="M" ;;
      f) setMode "packageByFile"         ; itemType="F" ;;
      F) ENABLE_FOLDING=1                ;;
      h) displayHelp                     ;;
      i) setMode "initializeRepository"  ;;
      l) LOCAL_ONLY=1                    ;;
      m) setMode "packageByMetaCluster"  ; itemType="M" ;;
      M) setMode "clusterByMetaCluster"  ; itemType="M" ;;
      p) setMode "clusterByPackage"      ; itemType="P" ;;
      P) setMode "metaClusterByPackage"  ; itemType="P" ;;
      t) setMode "clusterTree"           ; itemType="C" ;;
      T) setMode "metaClusterTree"       ; itemType="M" ;;
      z) setMode "dependsOnPackage"      ; itemType="P" ;;
      Z) setMode "dependsOnCluster"      ; itemType="C" ;;
      r) REPOSITORY="${OPTARG}"          ;;
      s) BASEDIR="${OPTARG}"             ;;
      v) SHOW_DETAIL=1                   ;;
      ?) displayHelp                     ;;
      *) displayHelp                     ;;
   esac
done

shift $(expr $OPTIND - 1)

if [ -z "${MODE}" ]; then
   gettext "Error: No processing mode was selected for this program.\n"
   displayHelp
fi

if [ -z "${REPOSITORY}" ]; then
   gettext "Error: No working repository directory defined.\n"
   displayHelp
fi

export MC_DIR="${REPOSITORY}/metaclusters"
export C_DIR="${REPOSITORY}/clusters"
export P_DIR="${REPOSITORY}/packages"

if [ "${MODE}" = "initializeRepository" ]; then
   [ ${LOCAL_ONLY} -eq 1 ] && BASEDIR="/var/sadm/pkg"
   initializeRepository
   exit 0
fi

if [ ! -f ${REPOSITORY}/.created ]; then
   gettext "Error: Working repository has not been initialized.\n"
   displayHelp
fi

case ${MODE} in
   metaClusterByCluster|metaClusterByPackage|metaClusterTree)
      displayMode="M"
      ;;
   clusterByPackage|clusterByMetaCluster|clusterTree)
      displayMode="C"
      ;;
esac


if [ $# -ge 1 ]; then
   displayItems ${displayMode} ${itemType} ${MODE} $*
else
   gettext "Error: A list of items to process was not found.\n"
   displayHelp
fi

exit 0
