source: branches/confman-1.9/confmancommon.sh.in @ 586

Revision 586, 4.3 KB checked in by blee, 5 months ago (diff)

Merge fix for #139 into confman-1.9.

Closes #139

  • Property svn:keywords set to Id
Line 
1# Copyright (c) 2008, Christopher Cowart and contributors
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# * Redistributions of source code must retain the above copyright
8#   notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above copyright
10#   notice, this list of conditions and the following disclaimer in the
11#   documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24#
25# $Id$
26#
27# This file is a global include for all confman-based utilities. It
28# provides the common interfaces for loading the shell library, setting
29# up the use of syslog, and reading configurations.
30
31# Some global definitions. Defaults that can be overriden by options.
32DEFAULTS="@sysconfdefaultsdir@/confman.conf"
33UCONF="${HOME}/.confmanrc"
34MYNAME=`basename $0`
35if [ `id -u` != "0" ]; then
36    SUDO="@SUDO@"
37fi
38
39# First things first. Check to see if the logging fd is open. If it's not,
40# we re-spawn confman inside a pipe.
41if (true >&5) 2>/dev/null ; then
42        exec 4>&2 2>&5 >&3
43else
44        exec 3>&1
45        exec $0 "$@" 5>&1 1>&3 | logger -t "$MYNAME[$$]: $USER" -s
46        exit ${PIPESTATUS[0]}
47fi
48
49# After the fork, let's MY_PID so subshells can throw errors that will
50# actually kill the parent
51MY_PID="$$"
52
53# Suck in the default configuration
54if [ -f "$DEFAULTS" ] ; then
55    . "$DEFAULTS"
56else
57        echo "$DEFAULTS couldn't be located" >&2
58        exit 1
59fi
60
61# Get the global config
62if [ -f "$GCONF" ] ; then
63        . "$GCONF"
64else
65        echo "Global config file couldn't be located" >&2
66        exit 1
67fi
68
69if [ -z "$UNAME" ] ; then
70    UNAME=`uname`
71fi
72case $UNAME in
73    FreeBSD)
74    install_cmd="install -Sp"
75    mktemp_file="mktemp -t confman"
76    mktemp_dir="mktemp -d -t confman"
77    sed_cmd="sed -E"
78    function sed_i_cmd {
79        ${sed_cmd} -i '' "$@"
80    }
81    stat_cmd="stat -f"
82    stat_opts="mode=%Mp%Lp owner=%Su group=%Sg"
83    readlink_cmd="@GREADLINK@"
84    ;;
85    Linux)
86    install_cmd="install -p"
87    mktemp_file="mktemp -t confman.XXXXXX"
88    mktemp_dir="mktemp -t -d confman.XXXXXX"
89    sed_cmd="sed -r"
90    function sed_i_cmd {
91        ${sed_cmd} -i "$@"
92    }
93    stat_cmd="stat -c"
94    stat_opts="mode=%a owner=%U group=%G"
95    readlink_cmd="readlink"
96    ;;
97    *)
98    install_cmd="install -Sp"
99    mktemp_file="mktemp -t confman"
100    mktemp_dir="mktemp -d -t confman"
101    sed_cmd="sed -E"
102    function sed_i_cmd {
103        ${sed_cmd} -i '' "$@"
104    }
105    stat_cmd="stat -f"
106    stat_opts="mode=%Mp%Lp owner=%Su group=%Sg"
107    readlink_cmd="@GREADLINK@"
108    ;;
109esac
110
111# Things that don't vary by OS
112fetch_cmd="@WGET@ -q -O"
113
114# Create a base temporary directory for this process
115export TMPDIR=`$mktemp_dir`
116
117# Get the user config
118if [ -f "$UCONF" ] ; then
119        . "$UCONF"
120fi
121
122# Set debugging:
123if [ -z $DEBUG ] ; then
124        DEBUG="false"
125fi
126
127# Now, we source the library
128if [ -f $REPO_LIBRARY ] ; then
129        . $REPO_LIBRARY
130else
131        echo "Couldn't source the confman shell library" >&2
132        exit 1
133fi
134
135# Source the filedb library
136if [ -r "${FILEDB_LIBRARY}" ]; then
137    . "${FILEDB_LIBRARY}"
138else
139    echo "Could not load filedb library: ${FILEDB_LIBRARY}" >&2
140    exit 1
141fi
142
143# Create and/or fix permissions on our local state directory
144if ! [ -d "$REPO_DB" ] ; then
145    $SUDO $install_cmd -d -o root -g $CONF_GROUP -m 2770 "$REPO_DB"
146fi
147find $REPO_DB -not -group $CONF_GROUP -exec $SUDO chgrp $CONF_GROUP {} \; \
148    -exec chmod g+r {} \;
149
150# And we'll set a trap:
151trap "conf_cleanExit" SIGINT SIGTERM SIGHUP EXIT
152
Note: See TracBrowser for help on using the repository browser.