forked from Atha/update-conf.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-conf.d.simple.in
127 lines (111 loc) · 3.94 KB
/
update-conf.d.simple.in
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
#!/bin/sh
#
# update-conf.d SIMPLE VERSION
# Version 2012-09-15
#
# Script for flexible /etc/*.d configuration
# Originally from Atha, with a lot of improvements from truc - thanks!
# Generalized for /etc/*.d by Nicolas Bercher [email protected]
# Fixes by javeree
#
# The included makefile assists easy installation:
# "make clean" will clean previously built versions of this script.
# "make simple install" will build and install it.
# "make build install" builds and installs both versions.
#
# For installing, this script is copied into /usr/local/sbin and is called
# update-conf.d for both the SIMPLE and the COMPLEX version.
# However, you may find /usr/sbin a more suited place. (Edit the Makefile.)
#
# You need to configure your <conf> entries in separate files in /etc/<conf>.d;
# from the .d'ed directory only filenames starting with two digits are included!
# Other files are "inactive" which is the intended way of doing it.
#
# Examples: /etc/fstab.d/01base or /etc/hosts.d/61nfs-dm8000. If one is renamed,
# say to /etc/hosts.d/.61nfs-dm8000 or /etc/hosts.d/nfs-dm8000, it is ignored by
# the script i.e. inactive.
#
# Copyright 2013 javeree
# Copyright 2011 Nicolas Bercher
# Copyright 2010 truc (on improvements)
# Copyright 2008-2013 Atha
# Distributed under the terms of the GNU General Public License v2 or later
#
# The home of this script is https://github.com/Atha/update-conf.d
# It first appeared at http://forums.gentoo.org/viewtopic.php?p=6364143
#
## SETUP
# scriptname:
scriptname=$(basename "$0")
# root of configuration files:
root="@CONFIGDIR@"
# script configuration path:
scriptconf="${root}/update-conf.d.conf"
# name of the configuration file to process:
conf="${1}"
# configuration file path:
confpath="${root}/${conf}"
# path to the backup of the current/previous configuration file:
bkpconfpath="${confpath}.d.bak"
# <conf.d> directory path:
dpath="${root}/${conf}.d"
# path to the intermediate location of the new configuration file based on
# <conf>.d/[0-9][0-9]* files:
dconfpath="${dpath}/$(basename ${conf})"
# flag_verbose sets the verbosity level.
# Default is 1 (verbose output). If you want no messages set this to 0.
flag_verbose="1"
## FUNCTIONS
message () {
[ ${flag_verbose} -gt 0 ] && echo "$@"
}
## MAIN PROGRAM
if [ ! -f "${scriptconf}" ]; then
echo "${scriptname}: Script configuration file '${scriptconf}' not found!" >&2
exit 1
fi
if [ $# -lt 1 ]; then
echo "${scriptname}: <conf> entry must be specified as a command-line argument.
${scriptname}: type \"${scriptname} ?\" for a list of valid <conf> entries." >&2
exit 1
fi
if [ "${conf}" = "?" ]; then
echo "Usage: ${scriptname} <conf>
List of valid <conf> entries:"
cat ${scriptconf}
echo "
Edit ${scriptconf} to add or delete entries."
exit 0
fi
if ! grep "^${conf}$" "${scriptconf}"; then
echo "${scriptname}: Not allowed to process file ${confpath}, see ${scriptconf}." >&2
exit 1
fi
if [ ! -d "${dpath}" ] ; then
echo "${scriptname}: ${dpath} is not present!" >&2
exit 1
fi
if [ -e "${dconfpath}" ] ; then
echo "${scriptname}: please remove ${dconfpath} before you run this script.
${scriptname}: NOTE: It may have been left by a previous run, but you should check anyway." >&2
exit 2
fi
cat << 'EOT' > "${dconfpath}" && message "${dconfpath} created, header added"
# Configuration file automatically generated by the update-conf.d
# script.
#
# Please change the according lines in /etc/<conf.d>/* if you want
# them to be permanent, otherwise they will not survive the next
# invocation of update-conf.d!
#
EOT
for dconf_file in "${dpath}"/[0-9][0-9]* ; do
echo "" >> "${dconfpath}"
echo "# ${dconf_file}:" >> "${dconfpath}"
grep '^[^#].*' "${dconf_file}" >> "${dconfpath}"
message "Added: ${dconf_file}"
done
mv -f "${confpath}" "${bkpconfpath}" && message "Existing ${confpath} renamed to ${bkpconfpath}"
mv -f "${dconfpath}" "${confpath}" && message "New configuration file ${dconfpath} moved to ${confpath}"
echo "${scriptname}: ${confpath} updated."
exit 0