1 #!/usr/bin/bash
   2 
   3 NOBANNER=1
   4 batch_flag=""
   5 if [ "${BATCH}" = 1 ]; then
   6     echo "Enabling batch mode."
   7     batch_flag="-b"
   8 fi
   9 . ../lib/functions.sh
  10 
  11 [ "${1}" == "licenses" ] && AUDIT_LICENSE=1
  12 
  13 # targets maps any valid package name to its build script.
  14 declare -A targets
  15 # fulltargets maps full package names to their build script.
  16 declare -A fulltargets
  17 declare -A licenses
  18 TCNT=0
  19 for build in */build*.sh
  20 do
  21     for PKG in $(grep -v '##IGNORE##' $build | sed -e 's/^ +//' -e 's/ +#.+//' -e 's/=/ /g' -e 's/^.+make_package/make_package/g' | awk '{if($1=="PKG"){PKG=$2; print $2;} if($1=="make_package"){print PKG"="$2;}}')
  22     do
  23         if [ -n "`echo ${PKG} | grep '.='`" ] ; then
  24             [ -z "${AUDIT_LICENSE}" ] && continue
  25             MOG=`echo ${PKG} | sed -e 's/^.*=//'`
  26             PKG=`echo ${PKG} | sed -e 's/=.*$//'`
  27             LOCALMOG=`echo ${build} | sed -e 's:/.*$:/local.mog:'`
  28             [ -f $MOG ] || MOG=""
  29             [ -f $LOCALMOG ] || LOCALMOG=""
  30             LICENSE=`nawk -F "[ =]" '/"/{gsub("\"", "")} /^license/ {print $3;}' $MOG $LOCALMOG /dev/null | xargs`
  31             licenses+=([$PKG]=$LICENSE)
  32             TCNT=$(($TCNT + 1))
  33             print -f "."
  34         else
  35             targets+=([$PKG]=$build)
  36             fulltargets+=([$PKG]=$build)
  37             #
  38             # Repeatedly strip off leading components to generate all valid
  39             # names for this package.
  40             #
  41             while [[ $PKG =~ '/' ]]; do
  42                 PKG=${PKG#*/}
  43                 targets+=([$PKG]=$build)
  44             done
  45         fi
  46     done
  47 done
  48 [ -n "${AUDIT_LICENSE}" ] && echo
  49 
  50 for manifest in */*.p5m
  51 do
  52     for PKG in $(awk '/^set name=pkg.fmri/ {print $3;}' $manifest | sed -e 's/value=//' -e 's/.*\/\/[^\/]*\///g' -e 's/@.*//')
  53     do
  54         targets+=([$PKG]=$manifest)
  55     done
  56 done
  57 
  58 usage() {
  59     echo $0
  60     echo "    list [grep pattern]"
  61     echo "    licenses"
  62     echo "    build <pkg> | all"
  63     exit
  64 }
  65 
  66 bail() {
  67     echo $*
  68     exit
  69 }
  70 
  71 list() {
  72     PAT=${1-.}
  73     for target in "${!fulltargets[@]}"
  74     do
  75         if [[ "$PAT" = "." ]]; then
  76             echo " * $target"
  77         elif [[ -n $(echo "$target" | grep "$PAT") ]]; then
  78             echo " * $target"
  79         fi
  80     done | sort
  81 }
  82 
  83 build() {
  84     if [[ -z "${targets[$1]}" ]]; then
  85         bail "Unknown package: $1"
  86     fi
  87     DIR=$(dirname ${targets[$1]})
  88     pushd $DIR > /dev/null || bail "Cannot chdir to $DIR"
  89         PKGSRVR=$DEFAULT_PKGSRVR
  90         PKGPUBLISHER=$DEFAULT_PKGPUBLISHER
  91         PKGROOT=`pwd`/root
  92         if [[ -f environment ]]; then
  93             logmsg "--- Setting new environment"
  94             . environment
  95         fi
  96         SCRIPT=$(basename ${targets[$1]})
  97         if [[ -n $(echo $SCRIPT | grep ".p5m$") ]]; then
  98             echo "Found a manifest file. Preparing it for publishing."
  99             sed -e "s/@PKGPUBLISHER@/$PKGPUBLISHER/g; s/@RELVER@/$RELVER/g; s/@PVER@/$PVER/g;" < $SCRIPT > $SCRIPT.final
 100             if [[ -f root.tar.bz2 ]]; then
 101                 echo "File archive found. Extracting..."
 102                 bzip2 -dc root.tar.bz2 | tar xf - || \
 103                     bail "Failed to extract root.tar.bz2"
 104                 echo "Publishing from $SCRIPT.final"
 105                 pkgsend -s $PKGSRVR publish -d $PKGROOT $SCRIPT.final || \
 106                     bail "pkgsend failed"
 107                 rm -rf $PKGROOT
 108             # In case we just have a tree of files and not a tarball
 109             elif [[ -d $PKGROOT ]]; then
 110                 echo "Publishing from $SCRIPT.final"
 111                 pkgsend -s $PKGSRVR publish -d $PKGROOT $SCRIPT.final || \
 112                     bail "pkgsend failed"
 113             # Else we just have a manifest to import
 114             else
 115                 echo "Simple manifest to import... importing to $PKGSRVR"
 116                 pkgsend -s $PKGSRVR publish $SCRIPT.final || \
 117                     bail "pkgsend failed"
 118                 rm $SCRIPT.final
 119             fi
 120         else
 121             PATH=$PATH:. $SCRIPT -r $PKGSRVR $batch_flag || \
 122                 logerr "Unable to run $SCRIPT"
 123         fi
 124     popd >/dev/null
 125 }
 126 
 127 licenses() {
 128     LCNT=0
 129     for target in "${!licenses[@]}"
 130     do
 131         if [[ -n "${licenses[$target]}" ]]; then
 132             echo " * $target     -> ${licenses[$target]}"
 133             LCNT=$(($LCNT + 1))
 134         fi
 135     done | sort
 136     if [ $LCNT -ne $TCNT ]; then
 137         echo
 138         echo "=== Packages missing license information ==="
 139         for target in "${!licenses[@]}"
 140         do
 141             if [[ -z "${licenses[$target]}" ]]; then
 142                 echo " * $target"
 143             fi
 144         done | sort
 145     fi
 146 }
 147 
 148 DEFAULT_PKGSRVR=$PKGSRVR
 149 DEFAULT_PKGPUBLISHER=$PKGPUBLISHER
 150 
 151 case "$1" in
 152     list)
 153         list $2
 154         exit
 155         ;;
 156 
 157     licenses)
 158         licenses
 159         exit
 160         ;;
 161 
 162     build)
 163         shift
 164         tobuild=$*
 165         if [ -z "$tobuild" ] || [ "$tobuild" == "all" ]; then
 166                 batch_flag="-b"
 167                 for tgt in "${!fulltargets[@]}"; do
 168                         build $tgt
 169                 done
 170         else
 171                 for tgt in $tobuild; do
 172                         build $tgt
 173                 done
 174         fi
 175         exit
 176         ;;
 177 
 178     *)
 179         usage
 180         ;;
 181 esac
 182