1
+ #! /usr/bin/env bash
2
+ # shellcheck disable=SC2034
3
+
4
+ # # Makes debuggers' life easier - Unofficial Bash Strict Mode
5
+ # # BASHDOC: Shell Builtin Commands - Modifying Shell Behavior - The Set Builtin
6
+ set -o errexit
7
+ set -o errtrace
8
+ set -o nounset
9
+ set -o pipefail
10
+
11
+ # # Runtime Dependencies Checking
12
+ declare\
13
+ runtime_dependency_checking_result=still-pass\
14
+ required_software
15
+
16
+ for required_command in \
17
+ basename\
18
+ cp\
19
+ dirname\
20
+ git\
21
+ realpath; do
22
+ if ! command -v " ${required_command} " & > /dev/null; then
23
+ runtime_dependency_checking_result=fail
24
+
25
+ case " ${required_command} " in
26
+ basename\
27
+ |cp\
28
+ |dirname\
29
+ |realpath)
30
+ required_software=' GNU Coreutils'
31
+ ;;
32
+ git)
33
+ required_software=' Git'
34
+ ;;
35
+ * )
36
+ required_software=" ${required_command} "
37
+ ;;
38
+ esac
39
+
40
+ printf --\
41
+ ' Error: This program requires "%s" to be installed and its executables in the executable searching paths.\n' \
42
+ " ${required_software} " 1>&2
43
+ unset required_software
44
+ fi
45
+ done ; unset required_command required_software
46
+
47
+ if [ " ${runtime_dependency_checking_result} " = fail ]; then
48
+ printf --\
49
+ ' Error: Runtime dependency checking fail, the progrom cannot continue.\n' 1>&2
50
+ exit 1
51
+ fi ; unset runtime_dependency_checking_result
52
+
53
+ # # Non-overridable Primitive Variables
54
+ # # BASHDOC: Shell Variables » Bash Variables
55
+ # # BASHDOC: Basic Shell Features » Shell Parameters » Special Parameters
56
+ if [ -v ' BASH_SOURCE[0]' ]; then
57
+ RUNTIME_EXECUTABLE_PATH=" $( realpath --strip " ${BASH_SOURCE[0]} " ) "
58
+ RUNTIME_EXECUTABLE_FILENAME=" $( basename " ${RUNTIME_EXECUTABLE_PATH} " ) "
59
+ RUNTIME_EXECUTABLE_NAME=" ${RUNTIME_EXECUTABLE_FILENAME% .* } "
60
+ RUNTIME_EXECUTABLE_DIRECTORY=" $( dirname " ${RUNTIME_EXECUTABLE_PATH} " ) "
61
+ RUNTIME_COMMANDLINE_BASECOMMAND=" ${0} "
62
+ declare -r\
63
+ RUNTIME_EXECUTABLE_FILENAME\
64
+ RUNTIME_EXECUTABLE_DIRECTORY\
65
+ RUNTIME_EXECUTABLE_PATHABSOLUTE\
66
+ RUNTIME_COMMANDLINE_BASECOMMAND
67
+ fi
68
+ declare -ar RUNTIME_COMMANDLINE_PARAMETERS=(" ${@ } " )
69
+
70
+ # # init function: entrypoint of main program
71
+ # # This function is called near the end of the file,
72
+ # # with the script's command-line parameters as arguments
73
+ init (){
74
+ if ! process_commandline_parameters; then
75
+ printf --\
76
+ ' Error: %s: Invalid command-line parameters.\n' \
77
+ " ${FUNCNAME[0]} " \
78
+ 1>&2
79
+ print_help
80
+ exit 1
81
+ fi
82
+
83
+ declare GIT_DIR=" ${RUNTIME_EXECUTABLE_DIRECTORY} /.git"
84
+ declare GIT_WORK_TREE=" ${RUNTIME_EXECUTABLE_DIRECTORY} "
85
+ export GIT_DIR GIT_WORK_TREE
86
+
87
+ local version; version=" $( determine_package_revision) "
88
+
89
+ cp \
90
+ --force \
91
+ " ${RUNTIME_EXECUTABLE_DIRECTORY} /template.travis.yml" \
92
+ " ${RUNTIME_EXECUTABLE_DIRECTORY} /template-${version} .travis.yml"
93
+ exit 0
94
+ }; declare -fr init
95
+
96
+ # # This function requires GIT_DIR and GIT_WORK_TREE to be set and exported
97
+ determine_package_revision (){
98
+ if [ ! -d " ${GIT_DIR} " ]; then
99
+ # FIXME: This is a source tarball without git repository, currently we don't know how to deal with this case(may requires smudge filter)
100
+ printf ' unknown-%s' " $( basename " ${GIT_WORK_TREE} " ) "
101
+ return 0
102
+ fi
103
+
104
+ # Workaround: Make Git don't consider tree is dirty even when it shouldn't because of the existing clean filter
105
+ # Why does 'git status' ignore the .gitattributes clean filter? - Stack Overflow
106
+ # http://stackoverflow.com/questions/19807979/why-does-git-status-ignore-the-gitattributes-clean-filter
107
+ git add -u
108
+
109
+ if ! git rev-parse --verify HEAD & > /dev/null; then
110
+ # git repository is newly initialized
111
+ printf not-version-controlled
112
+ return 0
113
+ else
114
+ # Best effort to describe the revision
115
+ # version control - How do you achieve a numeric versioning scheme with Git? - Software Engineering Stack Exchange
116
+ # https://softwareengineering.stackexchange.com/questions/141973/how-do-you-achieve-a-numeric-versioning-scheme-with-git
117
+ printf ' %s' " $( git describe --tags --dirty --always) "
118
+ return 0
119
+ fi
120
+ }
121
+ readonly -f determine_package_revision
122
+
123
+ # # Traps: Functions that are triggered when certain condition occurred
124
+ # # Shell Builtin Commands » Bourne Shell Builtins » trap
125
+ trap_errexit (){
126
+ printf ' An error occurred and the script is prematurely aborted\n' 1>&2
127
+ return 0
128
+ }; declare -fr trap_errexit; trap trap_errexit ERR
129
+
130
+ trap_exit (){
131
+ return 0
132
+ }; declare -fr trap_exit; trap trap_exit EXIT
133
+
134
+ trap_return (){
135
+ local returning_function=" ${1} "
136
+
137
+ printf ' DEBUG: %s: returning from %s\n' " ${FUNCNAME[0]} " " ${returning_function} " 1>&2
138
+ }; declare -fr trap_return
139
+
140
+ trap_interrupt (){
141
+ printf ' \n' # Separate previous output
142
+ printf ' Recieved SIGINT, script is interrupted.' 1>&2
143
+ return 1
144
+ }; declare -fr trap_interrupt; trap trap_interrupt INT
145
+
146
+ print_help (){
147
+ printf ' Currently no help messages are available for this program\n' 1>&2
148
+ return 0
149
+ }; declare -fr print_help;
150
+
151
+ process_commandline_parameters () {
152
+ if [ " ${# RUNTIME_COMMANDLINE_PARAMETERS[@]} " -eq 0 ]; then
153
+ return 0
154
+ fi
155
+
156
+ # modifyable parameters for parsing by consuming
157
+ local -a parameters=(" ${RUNTIME_COMMANDLINE_PARAMETERS[@]} " )
158
+
159
+ # Normally we won't want debug traces to appear during parameter parsing, so we add this flag and defer it activation till returning(Y: Do debug)
160
+ local enable_debug=N
161
+
162
+ while true ; do
163
+ if [ " ${# parameters[@]} " -eq 0 ]; then
164
+ break
165
+ else
166
+ case " ${parameters[0]} " in
167
+ --help\
168
+ |-h)
169
+ print_help;
170
+ exit 0
171
+ ;;
172
+ --debug\
173
+ |-d)
174
+ enable_debug=Y
175
+ ;;
176
+ * )
177
+ printf ' ERROR: Unknown command-line argument "%s"\n' " ${parameters[0]} " >&2
178
+ return 1
179
+ ;;
180
+ esac
181
+ # shift array by 1 = unset 1st then repack
182
+ unset ' parameters[0]'
183
+ if [ " ${# parameters[@]} " -ne 0 ]; then
184
+ parameters=(" ${parameters[@]} " )
185
+ fi
186
+ fi
187
+ done
188
+
189
+ if [ " ${enable_debug} " = Y ]; then
190
+ trap ' trap_return "${FUNCNAME[0]}"' RETURN
191
+ set -o xtrace
192
+ fi
193
+ return 0
194
+ }; declare -fr process_commandline_parameters;
195
+
196
+ init " ${@ } "
197
+
198
+ # # This script is based on the GNU Bash Shell Script Template project
199
+ # # https://github.com/Lin-Buo-Ren/GNU-Bash-Shell-Script-Template
200
+ # # and is based on the following version:
201
+ # # GNU_BASH_SHELL_SCRIPT_TEMPLATE_VERSION="@@GBSST_VERSION@@"
202
+ # # You may rebase your script to incorporate new features and fixes from the template
0 commit comments