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