forked from lua/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_origin.sh
executable file
·104 lines (84 loc) · 1.65 KB
/
fetch_origin.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
#!/usr/bin/env bash
PROGNAME=${0##*/}
VERSION="0.1"
origin=https://github.com/lua/lua
branch=master
dir=origin
fetch_origin() {
git clone --depth 1 ${origin} ${dir}
mkdir ${dir}/src ${dir}/include
mv ${dir}/*.h include/.
mv ${dir}/*.c src/.
return
}
clean_up() { # Perform pre-exit housekeeping
rm -rf ${dir}
return
}
error_exit() {
echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2
clean_up
exit 1
}
graceful_exit() {
clean_up
exit
}
signal_exit() { # Handle trapped signals
case $1 in
INT)
error_exit "Program interrupted by user"
;;
TERM)
echo -e "\n$PROGNAME: Program terminated" >&2
graceful_exit
;;
*)
error_exit "$PROGNAME: Terminating on unknown signal"
;;
esac
}
usage() {
echo -e "Usage: $PROGNAME [-h|--help] [-o|--origin] [-b|--branch]"
}
help_message() {
cat <<-_EOF_
$PROGNAME ver. $VERSION
Fetch from lua origin and auto arrange files
$(usage)
Options:
-h, --help Display this help message and exit.
-o, --origin Origin to fetch
-b, --branch Branch to fetch
_EOF_
return
}
# Trap signals
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT
# Parse command-line
while [[ -n $1 ]]; do
case $1 in
-h | --help)
help_message
graceful_exit
;;
-o | --origin)
origin=$2
echo "Fetch from $origin"
;;
-b | --branch)
branch=$2
echo "On branch $branch"
;;
-* | --*)
usage
error_exit "Unknown option $1"
;;
*) ;;
esac
shift
done
# Main logic
fetch_origin
graceful_exit