-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew
More file actions
executable file
·96 lines (80 loc) · 2.01 KB
/
Copy pathnew
File metadata and controls
executable file
·96 lines (80 loc) · 2.01 KB
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
#!/bin/sh
# Copyright (c) 2023 Sebastian LaVine <mail@smlavine.com>
# SPDX-License-Identifier: MPL-2.0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# new
# Prefills a new script file with some boilerplate.
# Arguments:
# Name of the script to create (in the src/ directory).
# If the script already exists, new text is appended to the file.
# Prints help information from the script's comment header.
print_help()
{
< "$0" awk '
/^#$/ {
getline # skip the /^#$/ line
# loop until empty line
while (length) {
sub(/^# ?/, "")
print
getline
}
exit
}
'
}
options='h'
while getopts "$options" o; do
case "$o" in
h) print_help; exit ;;
*) print_help >&2; exit 1 ;;
esac
done
shift $((OPTIND - 1))
if [ "$#" -ne 1 ]; then
echo "$0: illegal arguments provided" >&2
print_help >&2
exit 1
fi
path="src/$1" # XXX: exact wording here important for awk matching below
if ! touch "$path"; then
echo "$0: touch failed. Does '$1' contain a subdirectory?" >&2
exit 1
fi
if ! [ -f "$path" ]; then
echo "$0: '$path' is not a regular file" >&2
exit 1
fi
# copy THIS file to the path for the new script, with some adjustments.
< "$0" awk -v progname="$1" '
/^# Copyright \(c\) [0-9]+/ {
# Keep the copyright year updated
"date +%Y" | getline year
sub(/[0-9]+/, year)
print
next
}
/^# new$/ {
print "# " progname
getline # skip description string of new
print "#\t>>> (INSERT PROGRAM DESCRIPTION HERE) <<<"
print "# Options:"
print "#\t>>> (DOCUMENT PROGRAM OPTIONS HERE) <<<"
getline # get "# Arguments:"
print
getline # skip actual arguments of new
getline # XXX: dependent on new having two lines here
print "#\t>>> (DOCUMENT PROGRAM ARGUMENTS HERE) <<<"
next
}
/^path=/ {
# do not include the rest of this logic in the copied file.
print "echo Hello, \"$1\"!"
exit
}
{ print }
' >> "$path"
chmod +x "$path"