1
- .PHONY : clean check all install uninstall \
2
- install-dev uninstall-dev
1
+ .PHONY : all install-dev uninstall-dev
3
2
4
3
.ONESHELL :
5
- .DEFAULT_GOAL := all
6
-
7
- SHELL := /bin/bash
4
+ .DEFAULT_GOAL := all
8
5
6
+ SHELL := /bin/bash
9
7
CUSTOM_TARGETS =
10
8
11
- PREFIX := /usr
12
9
NAME := $(notdir $(realpath .) )
13
10
VERSION := 0
14
11
UPDATED := $(shell date +'% Y-% m-% d')
15
- CREATED := $(UPDATED )
16
12
AUTHOR := anon
17
- CONTACT := address
18
- ORGANISATION :=
19
13
CACHE_DIR := .cache
20
14
DOCS_DIR := docs
21
15
CONF_DIR := conf
22
16
AWK_DIR := awklib
23
17
FUNCS_DIR := func
18
+ FILE_EXT := .sh
24
19
INDENT := $(shell echo -e " ")
25
20
USAGE = $(NAME ) [OPTIONS]
26
21
OPTIONS_FILE := options
27
- MANPAGE = $(NAME ) .1
28
- MONOLITH = _$(NAME ) .sh
29
- BASE := _init.sh
22
+ MONOLITH = _$(NAME )$(FILE_EXT )
23
+ BASE := _init$(FILE_EXT )
30
24
SHBANG := \# !/bin/bash
31
25
OPTIONS_ARRAY_NAME := _o
32
- MANPAGE_OUT = _$(MANPAGE )
33
26
FUNC_STYLE := "() {"
34
27
35
- ifneq ($(wildcard config.mak) ,)
28
+ config_mak := config.mak
29
+ help_table := $(CACHE_DIR ) /help_table.txt
30
+ long_help := $(CACHE_DIR ) /long_help.md
31
+ getopt := $(CACHE_DIR ) /getopt
32
+ print_help := $(CACHE_DIR ) /print_help$(FILE_EXT )
33
+ print_version := $(CACHE_DIR ) /print_version$(FILE_EXT )
34
+
35
+ ifneq ($(wildcard $(config_mak ) ) ,)
36
36
include config.mak
37
+ else
38
+ config_mak :=
37
39
endif
38
40
39
- manpage_section = $(subst .,,$(suffix $(MANPAGE ) ) )
40
- function_createconf := $(FUNCS_DIR ) /_createconf.sh
41
- function_awklib := $(FUNCS_DIR ) /_awklib.sh
41
+ ifeq ($(wildcard $(OPTIONS_FILE ) ) ,)
42
+ OPTIONS_FILE :=
43
+ help_table :=
44
+ long_help :=
45
+ getopt :=
46
+ print_help :=
47
+ print_version :=
48
+ endif
49
+
50
+ function_createconf := $(FUNCS_DIR ) /_createconf$(FILE_EXT )
51
+ function_awklib := $(FUNCS_DIR ) /_awklib$(FILE_EXT )
42
52
43
53
ifneq ($(wildcard $(CONF_DIR ) /* ) ,)
44
54
include_createconf = $(function_createconf )
45
- conf_dirs = $(shell find $(CONF_DIR ) -type d)
46
- conf_files = $(shell find $(CONF_DIR ) -type f)
55
+ conf_dirs = $(patsubst ./ % , % , $( shell find "./ $(CONF_DIR ) " -type d) )
56
+ conf_files = $(patsubst ./ % , % , $( shell find "./ $(CONF_DIR ) " -type f) )
47
57
else
48
58
$(shell rm -f $(function_createconf))
49
59
endif
@@ -59,8 +69,9 @@ option_docs = $(wildcard $(DOCS_DIR)/options/*)
59
69
60
70
generated_functions := $(function_err ) $(include_createconf ) $(include_awklib )
61
71
function_files := \
62
- $(generated_functions ) \
63
- $(filter-out $(generated_functions ) ,$(wildcard $(FUNCS_DIR ) /* ) )
72
+ $(generated_functions ) \
73
+ $(filter-out $(generated_functions ) ,$(wildcard $(FUNCS_DIR ) /* ) )
74
+
64
75
65
76
# this hack writes 1 or 0 to the file .cache/got_func
66
77
# depending on existence of files in FUNC_DIR
@@ -95,36 +106,35 @@ install-dev: $(BASE) $(NAME)
95
106
uninstall-dev : $(PREFIX ) /bin/$(NAME )
96
107
rm $^
97
108
98
- check : all
99
- shellcheck $(MONOLITH )
100
-
101
- $(BASE ) : config.mak $(CACHE_DIR ) /getopt $(CACHE_DIR ) /print_help.sh $(CACHE_DIR ) /print_version.sh $(CACHE_DIR ) /got_func
109
+ $(BASE ) : $(getopt ) $(print_help ) $(print_version ) $(CACHE_DIR ) /got_func
102
110
@$(info making $@ )
103
111
{
104
112
printf ' %s\n' ' $(SHBANG)' ' '
105
113
106
- grep -vhE -e '^# !/' $(CACHE_DIR)/print_version.sh | sed '0,/2/s//3/'
107
- grep -vhE -e '^# !/' $(CACHE_DIR)/print_help.sh | sed '0,/2/s//3/'
114
+ [[ -f $${pv:=$(print_version)} ]] \
115
+ && grep -vhE -e '^# !/' $(print_version) | sed '0,/2/s//3/'
116
+ [[ -f $${ph:=$(print_help)} ]] \
117
+ && grep -vhE -e '^# !/' $(print_help) | sed '0,/2/s//3/'
108
118
109
119
echo
110
120
111
- [[ -d $(FUNCS_DIR) ]] && {
121
+ [[ -d $${fd:=$ (FUNCS_DIR)} ]] && {
112
122
printf '%s\n' \
113
123
'for ___f in "$$__dir/$(FUNCS_DIR)"/*; do' \
114
124
'$(INDENT). "$$___f" ; done ; unset -v ___f'
115
125
}
116
126
117
127
echo
118
128
119
- cat $(CACHE_DIR)/ getopt
129
+ [[ -f $${go:=$(getopt)} ]] && cat $( getopt)
120
130
121
131
echo "((BASHBUD_VERBOSE)) && _o[verbose]=1"
122
132
echo
123
133
124
134
echo 'main "$$@"'
125
135
} > $@
126
136
127
- $(MONOLITH ) : $(CACHE_DIR ) / print_version.sh $(NAME ) $(CACHE_DIR ) / print_help.sh $(function_files ) $(CACHE_DIR ) / getopt
137
+ $(MONOLITH ) : $(print_version ) $(NAME ) $(print_help ) $(function_files ) $(getopt )
128
138
@$(info making $@ )
129
139
{
130
140
printf ' %s\n' ' $(SHBANG)' ' '
@@ -141,29 +151,12 @@ $(MONOLITH): $(CACHE_DIR)/print_version.sh $(NAME) $(CACHE_DIR)/print_help.sh $(
141
151
142
152
chmod +x $@
143
153
144
- $(CACHE_DIR ) /copyright.txt : config.mak
145
- @$(info making $@ )
146
- year_created=$(CREATED ) year_created=$$ {year_created%%-* }
147
- year_updated=$$(date +'%Y' )
148
-
149
- copy_text="Copyright (c) "
150
-
151
- ((year_created == year_updated)) \
152
- && copy_text+=$$year_created \
153
- || copy_text+="$${year_created}-$${year_updated}, $(AUTHOR)"
154
-
155
- [[ $${org:=$(ORGANISATION)} ]] \
156
- && copy_text+=" of $(ORGANISATION) "
157
-
158
- printf '%s\n' \
159
- "$$copy_text" "SPDX-License-Identifier: $(LICENSE)" > $@
160
-
161
154
# if a file in docs/options contains more than
162
155
# 2 lines, it will get added to the file .cache/long_help.md
163
156
# like this:
164
157
# ### -s, --long-option ARG
165
158
# text in docs/options/long-option after the first 2 lines
166
- $(CACHE_DIR ) / long_help.md : $(CACHE_DIR ) /options_in_use $(option_docs )
159
+ $(long_help ) : $(CACHE_DIR ) /options_in_use $(option_docs )
167
160
@$(info making $@ )
168
161
for option in $( file < $( CACHE_DIR) /options_in_use) ; do
169
162
[[ $$ (wc -l < $( DOCS_DIR) /options/$$ option) -lt 2 ]] \
@@ -179,7 +172,7 @@ $(CACHE_DIR)/synopsis.txt: $(OPTIONS_FILE) | $(CACHE_DIR)/
179
172
@$(info making $@ )
180
173
sed ' s/^/$(NAME) /g;s/*//g' $< > $@
181
174
182
- $(CACHE_DIR ) / help_table.txt : $(CACHE_DIR ) / long_help.md
175
+ $(help_table ) : $(long_help )
183
176
@$(info making $@ )
184
177
for option in $$ (cat $( CACHE_DIR) /options_in_use); do
185
178
[[ -f $( CACHE_DIR) /options/$$ option ]] \
@@ -193,7 +186,7 @@ $(CACHE_DIR)/help_table.txt: $(CACHE_DIR)/long_help.md
193
186
paste <(echo "$$frag") <(echo "$$desc") | tr -d '\t'
194
187
done > $@
195
188
196
- $(CACHE_DIR ) / print_version.sh : config.mak | $(CACHE_DIR ) /
189
+ $(print_version ) : $( config_mak ) | $(CACHE_DIR ) /
197
190
@$(info making $@ )
198
191
echo $(SHBANG )
199
192
fstyle=$(FUNC_STYLE )
@@ -205,7 +198,7 @@ $(CACHE_DIR)/print_version.sh: config.mak | $(CACHE_DIR)/
205
198
" }" \
206
199
" " >> $@
207
200
208
- $(CACHE_DIR ) / print_help.sh : $(CACHE_DIR ) / help_table.txt $(CACHE_DIR ) /synopsis.txt
201
+ $(print_help ) : $(help_table ) $(CACHE_DIR ) /synopsis.txt
209
202
@$(info making $@ )
210
203
{
211
204
echo $(SHBANG )
@@ -219,7 +212,7 @@ $(CACHE_DIR)/print_help.sh: $(CACHE_DIR)/help_table.txt $(CACHE_DIR)/synopsis.tx
219
212
printf ' %s\n' ' usage: $(USAGE)' ' '
220
213
echo
221
214
fi
222
- cat $(CACHE_DIR ) / help_table.txt
215
+ cat $(help_table )
223
216
printf ' %s\n' ' EOB' ' }'
224
217
} > $@
225
218
@@ -283,11 +276,11 @@ $(FUNCS_DIR)/:
283
276
@$(info creating $(FUNCS_DIR ) / dir)
284
277
mkdir -p $(FUNCS_DIR )
285
278
286
- $(CACHE_DIR ) /options_in_use $(CACHE_DIR ) / getopt & : $(OPTIONS_FILE ) | $(CACHE_DIR ) /
279
+ $(CACHE_DIR ) /options_in_use $(getopt ) & : $(OPTIONS_FILE ) | $(CACHE_DIR ) /
287
280
@$(info parsing $(OPTIONS_FILE ) )
288
281
mkdir -p $(DOCS_DIR ) /options
289
282
gawk '
290
- BEGIN { RS=" |\\ n" }
283
+ BEGIN { RS=" |\\ n" ; longest = length( " version " ) }
291
284
292
285
/./ {
293
286
if (match($$0,/^\[?--([^][|[:space:]]+)(([|]-)(\S))?\]?$$/,ma))
@@ -447,11 +440,61 @@ $(CACHE_DIR)/options_in_use $(CACHE_DIR)/getopt &: $(OPTIONS_FILE) | $(CACHE_DIR
447
440
}
448
441
' $(OPTIONS_FILE) \
449
442
cache=$(CACHE_DIR) \
450
- name=$(NAME) > $(CACHE_DIR)/getopt
443
+ name=$(NAME) > $(getopt)
444
+
445
+ $(CACHE_DIR ) /copyright.txt : $(config_mak )
446
+ @$(info making $@ )
447
+ year_created=$(CREATED ) year_created=$$ {year_created%%-* }
448
+ year_updated=$$(date +'%Y' )
449
+ author=" $( AUTHOR) " org=$(ORGANISATION )
450
+
451
+ copy_text="Copyright (c) "
452
+
453
+ ((year_created == year_updated)) \
454
+ && copy_text+=$$year_created \
455
+ || copy_text+="$${year_created}-$${year_updated}"
456
+
457
+ [[ $$author ]] && copy_text+=", $$author"
458
+ [[ $$org ]] && copy_text+=" of $$org "
459
+
460
+ printf '%s\n' \
461
+ "$$copy_text" "SPDX-License-Identifier: $(LICENSE)" > $@
462
+
463
+ .PHONY : check
464
+ check : all
465
+ shellcheck $(MONOLITH )
466
+
467
+ .PHONY : install uninstall
468
+
469
+ installed_script := $(DESTDIR )$(PREFIX ) /bin/$(NAME )
470
+ installed_license := $(DESTDIR )$(PREFIX ) /share/licenses/$(NAME ) /LICENSE
471
+ installed_manpage := \
472
+ $(DESTDIR )$(PREFIX ) /share/man/man$(subst .,,$(suffix $(MANPAGE ) ) ) /$(MANPAGE )
473
+
474
+ install : all
475
+ @[[ -f $$ {manpage:= $( MANPAGE) } ]] && {
476
+ echo " install -Dm644 $( MANPAGE) $( installed_manpage) "
477
+ install -Dm644 $(MANPAGE ) $(installed_manpage )
478
+ }
479
+ [[ -f LICENSE ]] && {
480
+ echo " install -Dm644 LICENSE $( installed_license) "
481
+ install -Dm644 LICENSE $(installed_license )
482
+ }
483
+
484
+ echo "install -Dm755 $(MONOLITH) $(installed_script)"
485
+ install -Dm755 $(MONOLITH) $(installed_script)
486
+
487
+ uninstall :
488
+ @for f in $(installed_script ) $(installed_manpage ) $(installed_license ) ; do
489
+ [[ -f $$ f ]] || continue
490
+ echo " rm $$ f"
491
+ rm " $$ f"
492
+ done
493
+
451
494
452
- other_maks := $(filter-out config.mak ,$(wildcard * .mak) )
495
+ other_maks := $(filter-out $( config_mak ) ,$(wildcard * .mak) )
453
496
-include $(other_maks )
454
497
455
498
# by having all: last, it is possible to add CUSTOM_TARGETS
456
- # in " other_maks" , and have them automatically apply
499
+ # in ' other_maks' , and have them automatically apply
457
500
all : $(CUSTOM_TARGETS ) $(MONOLITH ) $(BASE )
0 commit comments