20829N/A#!/bin/bash
20829N/A
20829N/A# This script runs after the %install scriptlet of the Desktop spec files,
20829N/A# The purpose of the script is post-processing files before packaging.
20829N/A#
20829N/A# Currently it does two things:
20829N/A#
20829N/A# 1) delete CDDL header from text files, if found
20829N/A# 2) validate smf manifests
20829N/A#
20829N/A# command line arguments:
20829N/A#
20829N/A# spec-post-install [DIR|FILE]...
20829N/A#
20829N/A# Processes a single file or all files in a directory, recursively
20829N/A#
20829N/A# returns 0 on success or 1 on failure (breaks the build)
20829N/A#
20829N/A# requires file/gnu-coreutils
20829N/A
20829N/ASED=/usr/xpg4/bin/sed
20829N/ATR=/usr/xpg4/bin/tr
20829N/ANL=/usr/xpg4/bin/nl
20829N/ASTAT=/usr/bin/stat
20829N/A
20829N/A# deletes the CDDL header from a text file
20829N/A# returns 0 on success, 1 on error
20829N/Adelete_cddl() {
20829N/A mode=$(${STAT} -c '%a' "$1")
20829N/A chmod +w "$1"
20829N/A tmpfile=$(mktemp)
20829N/A ${SED} -e '/CDDL HEADER START/,/CDDL HEADER END/d' < "$1" > $tmpfile \
20829N/A || return 1
20829N/A mv $tmpfile "$1" || return 1
20829N/A chmod $mode "$1"
20829N/A return 0
20829N/A}
20829N/A
20829N/A# return 0 is arg is an integer number
20829N/Ais_int() {
20829N/A echo "$1" | grep '^[1-9][0-9]*$' > /dev/null
20829N/A}
20829N/A
20829N/A# verifies if a given file needs the CDDL header removed
20829N/Ahas_cddl() {
20857N/A start_line=$(${NL} -ba "$1" | ${TR} -cd 'a-z0-9A-Z \n' | \
20857N/A grep '^ *[0-9]*[ ]*CDDL HEADER START *$' | awk '{print $1}')
20857N/A end_line=$(${NL} -ba "$1" | ${TR} -cd 'a-z0-9A-Z \n' | \
20857N/A grep '^ *[0-9]*[ ]*CDDL HEADER END *$' | awk '{print $1}')
20829N/A is_int "$start_line" || return 1
20829N/A is_int "$end_line" || return 1
20829N/A if [ $start_line -gt $end_line ]; then
20829N/A return 1
20829N/A fi
20857N/A if [ $start_line -gt 15 ]; then
20857N/A echo "WARNING: CDDL-like header starts after the 15th line"
20829N/A return 1
20829N/A fi
20829N/A diff=$(($end_line - $start_line))
20857N/A if [ $diff != 17 -a $diff != 18 -a $diff != 11 -a $diff != 12 ]; then
20857N/A echo "WARNING: $1: unrecognised CDDL-like header, $diff lines long"
20829N/A return 1
20829N/A fi
20829N/A}
20829N/A
20829N/A# runs svccfg validate on arg
20829N/Avalidate_manifest() {
20829N/A svccfg validate "$1" || return 1
20829N/A
20829N/A return 0
20829N/A}
20829N/A
20829N/A# processes a single file
20829N/Aprocess_file() {
20829N/A grep 'CDDL HEADER' "$1" > /dev/null && has_cddl "$1" && {
20829N/A echo "Deleting CDDL header from $1"
20829N/A delete_cddl "$1" || return 1
20829N/A }
20829N/A
20829N/A echo "$1" | egrep '/(var|lib)/svc/manifest/' > /dev/null && {
20829N/A echo "Validating SMF manifest $1"
20829N/A validate_manifest "$1" || return 1
20829N/A }
20829N/A
20829N/A return 0
20829N/A}
20829N/A
20829N/A# processes all files in a directory, recursively
20829N/Aprocess_dir() {
20829N/A files=$(find "$1" -type f -print)
20829N/A for file in $files; do
20829N/A process_file "$file" || return 1
20829N/A done
20829N/A
20829N/A return 0
20829N/A}
20829N/A
20829N/Afor arg in "${@}"; do
20829N/A echo "Post-processing $arg"
20829N/A if [ -d "$arg" ]; then
20829N/A process_dir "$arg"
20829N/A elif [ -f "$arg" ]; then
20829N/A process_file "$arg"
20829N/A else
20829N/A echo "$0: $arg not found" 1>&2
20829N/A exit 1
20829N/A fi
20829N/Adone