-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkhlist.sht
156 lines (129 loc) · 2.9 KB
/
mkhlist.sht
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
#%shebang
#%version 5
#
# Generate host lists from master file
#
#%prolog
################
#%include sys/checkdir
#%include sys/checkfile
#%include sys/cmd
#%include sys/config
#%include sys/msg
#%include sys/pathabs
#%include sys/vars
################
#%embed mkhlist.awk mkhlist mkhlist.awk
################
# Print usage information
usage ()
{
cat << EOF
Usage: ${PROG} [-c config] [-r] -d outdir -f file
EOF
}
# Print help information
usage_help ()
{
usage
cat << EOF
Generate host lists from master file
-c config configuration file
-d outdir output directory
-f file source file
-r clean up output directory first
EOF
exit 0
}
####
# Parse command line options
get_options ()
{
local _opt=""
case "$1" in ( '-?' | '-help' | '--help' ) usage_help ;; esac
while getopts ":c:d:f:r" _opt ; do
case "${_opt}" in
( 'c' ) OPT_CONF_FILE="$(absolute_path "${OPTARG}")" ;;
( 'd' ) OPT_OUTPUT_DIR="${OPTARG}" ;;
( 'f' ) OPT_MASTER_LIST="${OPTARG}" ;;
( 'r' ) OPT_CLEANUP="true" ;;
( ':' )
err "missing argument for option -- '${OPTARG}'"
usage ; return 1 ;;
( '?' )
err "unknown option -- '${OPTARG}'"
usage ; return 1 ;;
( * )
err "no handler for option '${_opt}'"
return 1 ;;
esac
done
shift $((${OPTIND} - 1))
if test $# -ne 0 ; then
err "too many arguments"
usage
return 1
fi
}
####
# Generate host lists
mk_hlist ()
{
embed_mkhlist | awk -F ':' -f - -v OUT_DIR="${OUTPUT_DIR}" "${MASTER_LIST}"
}
####
# Cleanup directory
cleanup_dir ()
{
local _dir="$1"
test -n "${_dir}" || return 1
cmd find "${_dir}" -depth ! -type d -exec rm -f {} \+ &&
cmd find "${_dir}" -depth ! -path "${_dir}" -type d -exec rmdir {} \+
}
####
# Initialization subroutine
init ()
{
# Initialize variables
var_init MASTER_LIST OUTPUT_DIR CLEANUP &&
# Set up configuration file variables
var_init CONF_FILE &&
DEF_CONF_FILE="spxshell/mkhlist.conf" &&
# Get options
get_options "$@" &&
# Read configuration file
var_set CONF_FILE &&
read_config "${CONF_FILE}" &&
# Set variables
var_set MASTER_LIST OUTPUT_DIR CLEANUP
}
# Startup subroutine
startup ()
{
# Check file
test -n "${MASTER_LIST}" || { err "empty value for 'file'" ; return 1 ; }
MASTER_LIST="$(absolute_path "${MASTER_LIST}")"
check_file "${MASTER_LIST}" || return 1
# Check directory
test -n "${OUTPUT_DIR}" || { err "empty value for 'outdir'" ; return 1 ; }
OUTPUT_DIR="$(absolute_path "${OUTPUT_DIR}")"
check_dir "${OUTPUT_DIR}" || return 1
# Cleanup directory as requested
case "${CLEANUP}" in
( [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 )
cleanup_dir "${OUTPUT_DIR}" || return 1 ;;
esac
}
# Exit with error code
fail ()
{
exit "${1:-1}"
}
####
# Main subroutine
main ()
{
init "$@" && startup && mk_hlist || fail
}
# Call main subroutine
main "$@"