1*11be35a1SLionel Sambuc# Copyright 2012 Google Inc. 2*11be35a1SLionel Sambuc# All rights reserved. 3*11be35a1SLionel Sambuc# 4*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without 5*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions are 6*11be35a1SLionel Sambuc# met: 7*11be35a1SLionel Sambuc# 8*11be35a1SLionel Sambuc# * Redistributions of source code must retain the above copyright 9*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer. 10*11be35a1SLionel Sambuc# * Redistributions in binary form must reproduce the above copyright 11*11be35a1SLionel Sambuc# notice, this list of conditions and the following disclaimer in the 12*11be35a1SLionel Sambuc# documentation and/or other materials provided with the distribution. 13*11be35a1SLionel Sambuc# * Neither the name of Google Inc. nor the names of its contributors 14*11be35a1SLionel Sambuc# may be used to endorse or promote products derived from this software 15*11be35a1SLionel Sambuc# without specific prior written permission. 16*11be35a1SLionel Sambuc# 17*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*11be35a1SLionel Sambuc# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*11be35a1SLionel Sambuc# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*11be35a1SLionel Sambuc# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*11be35a1SLionel Sambuc# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*11be35a1SLionel Sambuc# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*11be35a1SLionel Sambuc# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*11be35a1SLionel Sambuc# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*11be35a1SLionel Sambuc# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*11be35a1SLionel Sambuc# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*11be35a1SLionel Sambuc# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*11be35a1SLionel Sambuc 29*11be35a1SLionel Sambuc# \file lib.subr 30*11be35a1SLionel Sambuc# Common initialization and functions for shell scripts. 31*11be35a1SLionel Sambuc 32*11be35a1SLionel Sambuc 33*11be35a1SLionel Sambucset -e 34*11be35a1SLionel Sambuc 35*11be35a1SLionel Sambuc 36*11be35a1SLionel Sambuc# Directory where the running script lives. 37*11be35a1SLionel SambucLib_DirName="$(dirname ${0})" 38*11be35a1SLionel Sambuc 39*11be35a1SLionel Sambuc 40*11be35a1SLionel Sambuc# Base name of the running script. 41*11be35a1SLionel SambucLib_ProgName="${0##*/}" 42*11be35a1SLionel Sambuc 43*11be35a1SLionel Sambuc 44*11be35a1SLionel Sambuc# Path to the temporary directory for this execution. 45*11be35a1SLionel SambucLib_TempDir= 46*11be35a1SLionel Sambuc 47*11be35a1SLionel Sambuc 48*11be35a1SLionel Sambuc# List of cleanup functions to execute on exit. 49*11be35a1SLionel Sambuc_Lib_Cleanup_Hooks= 50*11be35a1SLionel Sambuc 51*11be35a1SLionel Sambuc 52*11be35a1SLionel Sambuc# Catch unexpected exits and perform the required cleanups. In particular, 53*11be35a1SLionel Sambuc# ensure that the temporary directory in Lib_TempDir, if any, is removed. 54*11be35a1SLionel Sambuctrap 'lib_cleanup ; exit 2' HUP INT QUIT TERM 55*11be35a1SLionel Sambuc 56*11be35a1SLionel Sambuc 57*11be35a1SLionel Sambuc# Prints an informational message. 58*11be35a1SLionel Sambuc# 59*11be35a1SLionel Sambuc# \param ... The message to print. Can be provided as multiple words and, in 60*11be35a1SLionel Sambuc# that case, they are joined together by a single whitespace. 61*11be35a1SLionel Sambuclib_info() { 62*11be35a1SLionel Sambuc echo "${Lib_ProgName}: I: $*" 1>&2 63*11be35a1SLionel Sambuc} 64*11be35a1SLionel Sambuc 65*11be35a1SLionel Sambuc 66*11be35a1SLionel Sambuc# Prints a runtime error and exits. 67*11be35a1SLionel Sambuc# 68*11be35a1SLionel Sambuc# \param ... The message to print. Can be provided as multiple words and, in 69*11be35a1SLionel Sambuc# that case, they are joined together by a single whitespace. 70*11be35a1SLionel Sambuclib_error() { 71*11be35a1SLionel Sambuc echo "${Lib_ProgName}: E: $*" 1>&2 72*11be35a1SLionel Sambuc exit 1 73*11be35a1SLionel Sambuc} 74*11be35a1SLionel Sambuc 75*11be35a1SLionel Sambuc 76*11be35a1SLionel Sambuc# Prints a runtime warning. 77*11be35a1SLionel Sambuc# 78*11be35a1SLionel Sambuc# \param ... The message to print. Can be provided as multiple words and, in 79*11be35a1SLionel Sambuc# that case, they are joined together by a single whitespace. 80*11be35a1SLionel Sambuclib_warning() { 81*11be35a1SLionel Sambuc echo "${Lib_ProgName}: W: $*" 1>&2 82*11be35a1SLionel Sambuc} 83*11be35a1SLionel Sambuc 84*11be35a1SLionel Sambuc 85*11be35a1SLionel Sambuc# Prints an usage error and exits. 86*11be35a1SLionel Sambuc# 87*11be35a1SLionel Sambuc# \param ... The message to print. Can be provided as multiple words and, in 88*11be35a1SLionel Sambuc# that case, they are joined together by a single whitespace. 89*11be35a1SLionel Sambuclib_usage_error() { 90*11be35a1SLionel Sambuc echo "${Lib_ProgName}: E: $*" 1>&2 91*11be35a1SLionel Sambuc usage "${Lib_ProgName}" 1>&2 92*11be35a1SLionel Sambuc exit 1 93*11be35a1SLionel Sambuc} 94*11be35a1SLionel Sambuc 95*11be35a1SLionel Sambuc 96*11be35a1SLionel Sambuc# Executes the registered cleanup hooks. 97*11be35a1SLionel Sambuclib_cleanup() { 98*11be35a1SLionel Sambuc local hook 99*11be35a1SLionel Sambuc for hook in ${_Lib_Cleanup_Hooks}; do 100*11be35a1SLionel Sambuc "${hook}" 101*11be35a1SLionel Sambuc done 102*11be35a1SLionel Sambuc} 103*11be35a1SLionel Sambuc 104*11be35a1SLionel Sambuc 105*11be35a1SLionel Sambuc# Installs a new cleanup hook. 106*11be35a1SLionel Sambuc# 107*11be35a1SLionel Sambuc# \param ... The names of the cleanup functions to register. 108*11be35a1SLionel Sambuclib_register_cleanup() { 109*11be35a1SLionel Sambuc _Lib_Cleanup_Hooks="${_Lib_Cleanup_Hooks} ${*}" 110*11be35a1SLionel Sambuc} 111*11be35a1SLionel Sambuc 112*11be35a1SLionel Sambuc 113*11be35a1SLionel Sambuc# Creates a temporary directory for this execution. 114*11be35a1SLionel Sambuc# 115*11be35a1SLionel Sambuc# The temporary directory is unique to this script and execution. A cleanup 116*11be35a1SLionel Sambuc# hook is installed to delete such directory whenever lib_cleanup is called or 117*11be35a1SLionel Sambuc# when the program abruptly exits. 118*11be35a1SLionel Sambuc# 119*11be35a1SLionel Sambuc# \post Lib_TempDir is set to the path of the created temporary directory. 120*11be35a1SLionel Sambuclib_init_tempdir() { 121*11be35a1SLionel Sambuc Lib_TempDir=$(mktemp -d -t "${_Lib_ProgName}.XXXXXX") 122*11be35a1SLionel Sambuc lib_register_cleanup "lib_clean_tempdir" 123*11be35a1SLionel Sambuc} 124*11be35a1SLionel Sambuc 125*11be35a1SLionel Sambuc 126*11be35a1SLionel Sambuc# Cleanup hook to delete the temporary directory. 127*11be35a1SLionel Sambuc# 128*11be35a1SLionel Sambuc# This operation is idempotent. 129*11be35a1SLionel Sambuclib_clean_tempdir() { 130*11be35a1SLionel Sambuc if [ -n "${Lib_TempDir}" ]; then 131*11be35a1SLionel Sambuc rm -rf "${Lib_TempDir}" 132*11be35a1SLionel Sambuc Lib_TempDir= 133*11be35a1SLionel Sambuc fi 134*11be35a1SLionel Sambuc} 135