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