debian_template.sh 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#!/bin/bash

# Deployer for Travis-CI
# Debian package templating
#
# Call this script BEFORE running debuild!
# source ./debian_template.sh [clean] [main/asset]
#
# Before running this script,
# you should also set PACKAGE_NAME_EMAIL="John Doe <jdoe@example.com>" to match
# the identity of the key you will use to sign the package.
#

# Get script's actual path
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

# Recursive function for directory crawling
# $1 = Directory root to crawl
# $2 = Code to eval on file
# $3 = Code to eval on directory
# Exposes $dirtails, $dirlevel, and $dirtailname
dirlevel=0 # initialize
dirtails=()

# Utility function to make dira/dirb/dirc string
makedirtailname () {
	dirtailname=""
	for tail in $dirtails; do
		if [[ "$dirtailname" == "" ]]; then
			dirtailname="/$tail";
		else
			dirtailname="$dirtailname/$tail";
		fi;
	done;
}

evaldirectory () {
	if [ -d "$1" ]; then
		# Set contextual variables
		# dirtails is an array of directory basenames after the crawl root
		if (( $dirlevel > 0 )); then
			dirtails+=( "$(basename $1)" );
		else
			dirtails=();
		fi;
		dirlevel=$((dirlevel+1));

		# Generate directory path after the crawl root
		makedirtailname;

		# Eval our directory with the latest contextual info
		# Don't eval on root
		if (( $dirlevel > 1 )) && [[ "$3" != "" ]]; then
			eval "$3";
		fi;

		# Iterate entries
		for name in $1/*; do
			if [ -d "$name" ]; then
				# Name is a directory, but don't eval yet
				# Recurse so our vars are updated
				evaldirectory "$name" "$2" "$3";

				# Decrement our directory level and remove a dirtail
				unset 'dirtails[ ${#dirtails[@]}-1 ]';
				dirlevel=$((dirlevel-1));
				makedirtailname;
			else
				# Name is a file
				if [ -f "$name" ] && [[ "$2" != "" ]]; then
					eval "$2";
				fi;
			fi;
		done;

		# Reset our variables; we're done iterating
		if (( $dirlevel == 1 )); then
			dirlevel=0;
		fi;
	fi;
}

#
# Initialize package parameter defaults
#
if [[ "$__DEBIAN_PARAMETERS_INITIALIZED" != "1" ]]; then
	. ${DIR}/deployer/travis/deployer_defaults.sh;
fi;

# Clean up after ourselves; we only expect to run this script once
# during buildboting
__DEBIAN_PARAMETERS_INITIALIZED=0

# for envsubst
export __PACKAGE_DATETIME="$(date '+%a, %d %b %Y %H:%M:%S %z')"
export __PACKAGE_DATETIME_DIGIT="$(date -u '+%Y%m%d%H%M%S')"

if [[ "$PACKAGE_REVISION" == "" ]]; then
	PACKAGE_REVISION="-$__PACKAGE_DATETIME_DIGIT";
	__PACKAGE_REVISION_BY_DATE=1;
	export PACKAGE_REVISION=${PACKAGE_REVISION}; # for envsubst
fi;

#
# Clean the old debian/ directories
#
if [[ "$1" == "clean" ]]; then
	toclean=$2;
else
	toclean=$1;
fi;

if [[ "$toclean" == "" ]] || [[ "$toclean" == "main" ]]; then
	echo "Cleaning main package scripts";
	if [[ ! -f ${DIR}/debian ]]; then
		rm -rf ${DIR}/debian;
	fi;
fi;
if [[ "$toclean" == "" ]] || [[ "$toclean" == "asset" ]]; then
	echo "Cleaning asset package scripts";
	if [[ ! -f ${DIR}/assets/debian ]]; then
		rm -rf ${DIR}/assets/debian;
	fi;
fi;

#
# Make new templates
#
if [[ "$1" != "clean" ]]; then
	totemplate=$1;

	# HACK: ${shlibs:Depends} in the templates make the templating fail
	# So just define replacemment variables
	export SHLIBS_DEPENDS=${SHLIBS_DEPENDS};
	export MISC_DEPENDS=${MISC_DEPENDS};
	export DEBFILEVAR='$$file'; # used in assets/debian/rules

	# Package parameters are exported for envsubst in deployer_defaults.sh

	if [[ "$totemplate" == "" ]] || [[ "$totemplate" == "main" ]]; then
		echo "Generating main package scripts";
		fromroot=${DIR}/debian-template;
		toroot=${DIR}/debian;
		mkdir ${toroot};

		evaldirectory ${fromroot} \
			"cat \$name | envsubst > ${toroot}\${dirtailname}/\$( basename \$name )" \
			"mkdir \"${toroot}\${dirtailname}\"";
	fi;

	if [[ "$totemplate" == "" ]] || [[ "$totemplate" == "asset" ]]; then
		echo "Generating asset package scripts";
		fromroot=${DIR}/assets/debian-template;
		toroot=${DIR}/assets/debian;
		mkdir ${toroot};

		# Root dir to crawl; file eval; directory eval
		evaldirectory ${fromroot} \
			"cat \$name | envsubst > ${toroot}\${dirtailname}/\$( basename \$name )" \
			"mkdir \"${toroot}\${dirtailname}\"";
	fi;
fi;

if [[ "$__DPL_ACTIVE" != "1" ]] && [[ "$__PACKAGE_REVISION_BY_DATE" == "1" ]]; then
	unset PACKAGE_REVISION; # so we can reset the date on subsequent runs
fi;