-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashfoo
More file actions
executable file
·135 lines (124 loc) · 3.53 KB
/
bashfoo
File metadata and controls
executable file
·135 lines (124 loc) · 3.53 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
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
#!/usr/bin/env bash
show_help()
{
echo "usage: bashfoo --eval-out | --libdir | --bootstrap-script | --help"
echo ""
echo " * bashfoo --eval-out"
echo " outputs Bash script that shall be evaulated by bash script"
echo " to bootstrap bashfoo env in script"
echo ""
echo " * bashfoo --libdir"
echo " outputs bashfoo library path, one shall source bashfoo.sh from"
echo " this folder to bootstrap bashfoo env"
echo ""
echo " * bashfoo --bootstrap-script"
echo " outputs PATH of bashfoo bootstrap script"
echo ""
echo "bashfoo is reusable shell script function library."
echo "See http://idf2hotpo.pl/index.php/p/bashfoo."
}
show_function_help()
{
local fff="$1"
awk '
function basename(name) {
n = split(name, names, "/");
return names[n];
}
function module_name(name)
{
idx = index(name, ".sh")
if( idx ) {
return substr(name, 0,idx-1)
} else {
return name;
}
}
/^'$function'\(/ {
in_function=1;
printf("synopsis:\n");
printf(" bashfoo_require %s\n",module_name(basename(FILENAME)));
printf("\n",FN);
next;
}
/^([ \\t])*##/ {
if( in_function ) {
hashash_idx=index($0, "##")
printf("%s\n", substr($0, hashash_idx+3));
}
next;
}
// {
in_function=0
}
' $bashfoo_libdir/*.sh
}
show_eval()
{
ensure_location_variables_initialized
echo "bashfoo_libdir=\"$bashfoo_libdir\" ;"
echo "source \$bashfoo_libdir/bashfoo.sh"
}
ensure_location_variables_initialized()
{
if [ -z "$bashfoo_bin_prefix" ] ; then
bashfoo_bin_prefix=$(dirname $BASH_SOURCE)
fi
export bashfoo_bin_prefix
if [ -z "$bashfoo_libdir" ] ; then
if [ -f "$bashfoo_bin_prefix/bashfoo.sh" ] ; then
bashfoo_libdir="$bashfoo_bin_prefix"
elif [ -f "$bashfoo_bin_prefix/../share/bashfoo/bashfoo.sh" ] ; then
bashfoo_libdir="$bashfoo_bin_prefix/../share/bashfoo"
elif [ -f "$bashfoo_bin_prefix/../lib/bashfoo/bashfoo.sh" ] ; then
bashfoo_libdir="$bashfoo_bin_prefix/../lib/bashfoo"
else
echo "bashfoo: unable to find bashfoo libdir in $bashfoo_bin_prefix and in $bashfoo_bin_prefix/../lib/bashfoo" >&2
exit 1
fi
fi
export bashfoo_libdir
bashfoo_source_path="$bashfoo_libdir/bashfoo.sh"
}
if [ -z "$1" ] ; then
show_help
exit 1
fi
# args parse
while [ -n "$1" ] ; do
if [ "$1" = --help -o "$1" = "-h" ] ; then
show_help
exit
elif [ "$1" = --eval-out ] ; then
show_eval
exit
elif [ "$1" = --libdir ] ; then
ensure_location_variables_initialized
echo "$bashfoo_libdir"
exit
elif [ "$1" = --bootstrap-script ] ; then
ensure_location_variables_initialized
echo "$bashfoo_source_path"
exit
elif [ "$1" = help ] ; then
ensure_location_variables_initialized
function=$2
show_function_help
exit
elif [ -f "$1" ] ; then
ensure_location_variables_initialized
source "$bashfoo_libdir/bashfoo.sh"
SCRIPT_NAME="$1"
script="$1"
shift
source "$script" "$@"
exit
else
(
echo "bashfoo: invalid argument -- $1"
show_help
) >&2
exit 1
fi
shift
done