-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-stub2.sh
150 lines (149 loc) · 3.69 KB
/
create-stub2.sh
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
#!/bin/bash
## create-stub2
## - create program stub
## =standalone=
## version 0.1.2 - allow head and entry override
## =compatability=
## 4.2.46-34.43 - test working
## 4.1.2-15.25 - tested not working
## - failed to import bash function
##################################################
set -e # exit on error
shopt -s expand_aliases # enable alias expansion
##################################################
alias if-location="test ! \${location}"
alias if-programs="test ! \${programs}"
##################################################
test "$( declare -f create-stub-generate-stub-head )" || {
create-stub-generate-stub-head() {
cat << EOF
#!/bin/bash
## ${program}
## =stub=
## version 0.0.0 - stub
exit 0
EOF
}
}
#-------------------------------------------------
create-stub-generate-stub-imports-head() {
cat << EOF
##################################################\
$( create-stub-generate-stub-imports-head-location-line )
EOF
}
#-------------------------------------------------
create-stub-generate-stub-imports-head-location-line() {
if-location || {
cat << EOF
location=${location}
EOF
}
}
#-------------------------------------------------
for-each-create-stub-generate-stub-import-line() {
local candidate_program
for candidate_program in ${programs}
do
create-stub-generate-stub-import-line
done
}
#-------------------------------------------------
create-stub-generate-stub-import-line() {
cat << EOF
${candidate_program}() { \
$( if-location || echo "\${location}/" )\
${candidate_program}.sh \${@} \
; }
EOF
}
#-------------------------------------------------
create-stub-generate-stub-imports() {
if-programs || {
create-stub-generate-stub-imports-head
for-each-create-stub-generate-stub-import-line
}
}
#-------------------------------------------------
test "$( declare -f create-stub-generate-stub-entry )" || {
create-stub-generate-stub-entry() {
cat << EOF
##################################################
${program}() {
true
}
##################################################
if [ \${#} -eq 0 ]
then
true
else
exit 1 # wrong args
fi
##################################################
${program}
##################################################
## generated by $( basename ${0} ) v$( grep -e 'version' ${0} | head -n 1 | cut '-f3' '-d ' )
## on $( date --rfc-2822 )
## see <https://github.com/temptemp3/sh2>
##################################################
EOF
}
}
#-------------------------------------------------
create-stub-generate-stub() {
create-stub-generate-stub-head
create-stub-generate-stub-imports
create-stub-generate-stub-entry
}
#-------------------------------------------------
create-stub-validate-arguments() {
true
#------------------------------------------------
# TO-DO
# true if all conditions are satisfied for each [argument]
# [location], l
# 1. exists
# [program], p
# 1. does not exist in l
# [programs], Q
# 1. all q in P exist in l
# else
# false
#------------------------------------------------
}
#-------------------------------------------------
create-stub-list() {
create-stub-validate-arguments # check conditions
create-stub-generate-stub
}
#-------------------------------------------------
create-stub-help() {
cat << EOF
create-stub
1 - location
2 - stub name
3- list of programs
EOF
}
#-------------------------------------------------
create-stub2() {
create-stub-list
}
#-------------------------------------------------
if [ ${#} -ge 2 ]
then
location="${1}"
program="${2}"
programs="${@:3}"
elif [ ${#} -eq 1 ]
then
location=
program="${1}"
programs=
else
create-stub-help
exit 1 # wrong args
fi
##################################################
create-stub2
##################################################