-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·132 lines (105 loc) · 2.88 KB
/
main.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
#!/usr/bin/env bash
sourceable_script='false'
if [[ "$sourceable_script" != 'true' && ! "${BASH_SOURCE[0]}" -ef "$0" ]]
then
echo "Do not source this script! Execute it with bash instead."
return 1
fi
unset sourceable_script
########################
### Library sourcing ###
########################
library_sourcing()
{
# Unset as only called once and most likely overwritten when sourcing libs
unset -f library_sourcing
local -r THIS_SCRIPT_PATH="$(tmp_find_script_path)"
# Store $THIS_SCRIPT_PATH as unique or local variables
# LIB_PATH is needed by sourced libraries as well
readonly PROJECT_BASE_PATH="$THIS_SCRIPT_PATH"
export PROJECT_BASE_PATH
readonly LIB_PATH="$THIS_SCRIPT_PATH/lib"
export LIB_PATH
### Source libraries ###
source "$LIB_PATH/lib_core.bash" || exit 1
source_lib "$LIB_PATH/lib_handle_input.bash"
source_lib "$LIB_PATH/lib.bash"
}
# Minimal version of find_path(). Should only be used within this script to source library defining find_path().
tmp_find_script_path() {
unset -f tmp_find_script_path; local s="${BASH_SOURCE[0]}"; local d
while [[ -L "$s" ]]; do d=$(cd -P "$(dirname "$s")" &>/dev/null && pwd); s=$(readlink "$s"); [[ $s != /* ]] && s=$d/$s; done
echo "$(cd -P "$(dirname "$s")" &>/dev/null && pwd)"
}
library_sourcing
########################
### GLOBAL CONSTANTS ###
########################
# readonly <var>
############
### MAIN ###
############
############
### MAIN ###
############
main()
{
_handle_args_main "$@"
"${PROJECT_BASE_PATH}/files_to_link/link_files.sh"
}
###################
### END OF MAIN ###
###################
register_help_text 'script name' \
'<script name> [arg1]
<description>
[arg1] (Optional): The content of arg1'
register_function_flags 'script name' \
'-v' '--verbose' 'false' \
'Verbose output.' \
'-e' '--echo' 'true' \
'Echo string given after flag.'
_handle_args_main()
{
_handle_args 'script name' "$@"
###
# Non-flagged arguments
if (( ${#non_flagged_args[@]} != 0 ))
then
# Optional argument 1
main_input_dir=${non_flagged_args[0]}
if ! [[ -d "$main_input_dir" ]]
then
define error_info << END_OF_ERROR_INFO
Given [ DIR ] is not a directory: '$main_input_dir'
END_OF_ERROR_INFO
invalid_function_usage 2 'script name' "$error_info"
exit 1
fi
fi
###
###
# -e, --echo
if [[ "$echo_flag" == 'true' ]]
then
echo
echo "$echo_flag_value"
fi
###
###
# -v --verbose
if [[ "$verbose_flag" == 'true' ]]
then
verbose='true'
fi
###
}
main_stderr_red()
{
main "$@" 2> >(sed $'s|.*|\e[31m&\e[m|' >&2)
}
#################
### Call main ###
#################
main_stderr_red "$@"
#################