-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild_dist.sh
executable file
·161 lines (137 loc) · 4.15 KB
/
build_dist.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
151
152
153
154
155
156
157
158
159
#!/bin/bash
# build_dist.sh - Builds the iDigi Connector for Arduino Distribution Archive
# Copyright (c) 2012 Jordan Husney
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#############
# Settinigs #
#############
# Max number of arguments, empty vaule = unlimited arguments
SCRIPT_MAX_ARGS=0
#########################
# Common Initialization #
#########################
SCRIPT_NAME="$(basename "$0")"
# Stores arguments
SCRIPT_ARGS=()
# Stores option flags
SCRIPT_OPTS=()
# For returning value after calling SCRIPT_OPT
SCRIPT_OPT_VALUE=
#############
# Functions #
#############
usage () {
echo "Usage: $SCRIPT_NAME [options] [arguments]
Options:
-o, --output_file output ZIP archive filename
-h, --help display this help and exit
"
}
parse_options() {
while (( $#>0 )); do
opt="$1"
arg="$2"
case "$opt" in
-o|--output_file)
SCRIPT_OPT_SET "output_file" "$arg" 1
shift
;;
-h|--help)
usage
exit 0
;;
-*)
echo "$SCRIPT_NAME: invalid option -- '$opt'" >&2
echo "Try \`$SCRIPT_NAME --help' for more information." >&2
exit 1
;;
*)
if [[ ! -z $SCRIPT_MAX_ARGS ]] && (( ${#SCRIPT_ARGS[@]} == $SCRIPT_MAX_ARGS )); then
echo "$SCRIPT_NAME: cannot accept any more arguments -- '$opt'" >&2
echo "Try \`$SCRIPT_NAME --help' for more information." >&2
exit 1
else
SCRIPT_ARGS=("${SCRIPT_ARGS[@]}" "$opt")
fi
;;
esac
shift
done
}
###########################
# Script Template Functions
# Stores options
# $1 - option name
# $2 - option value
# $3 - non-empty if value is not optional
SCRIPT_OPT_SET () {
if [[ ! -z "$3" ]] && [[ -z "$2" ]]; then
echo "$SCRIPT_NAME: missing option value -- '$opt'" >&2
echo "Try \`$SCRIPT_NAME --help' for more information." >&2
exit 1
fi
# XXX should check duplication, but doesn't really matter
SCRIPT_OPTS=("${SCRIPT_OPTS[@]}" "$1" "$2")
}
# Checks if an option is set, also set SCRIPT_OPT_VALUE.
# Returns 0 if found, 1 otherwise.
SCRIPT_OPT () {
local i opt needle="$1"
for (( i=0; i<${#SCRIPT_OPTS[@]}; i+=2 )); do
opt="${SCRIPT_OPTS[i]}"
if [[ "$opt" == "$needle" ]]; then
SCRIPT_OPT_VALUE="${SCRIPT_OPTS[i+1]}"
return 0
fi
done
SCRIPT_OPT_VALUE=
return 1
}
########
# Main #
########
parse_options "$@"
# start to do something
OUTPUT_FILE="idigi-connector-arduino.zip"
if SCRIPT_OPT "output_file"; then
OUTPUT_FILE=${SCRIPT_OPT_VALUE}
fi
touch $OUTPUT_FILE
OUTPUT_FILE=`ls -1 "$(pwd)/${OUTPUT_FILE}"`
TEMPDIR=`mktemp -d -t ${SCRIPT_NAME}`
DESTDIR="${TEMPDIR}/$(basename "$OUTPUT_FILE" .zip)/iDigi"
mkdir -p $DESTDIR
echo "Copying README.md to ${DESTDIR}"
cp README.md $DESTDIR
echo "Copying iDigi.h ${DESTDIR}"
cp iDigi.h $DESTDIR
echo "Copying keywords.txt ${DESTDIR}"
cp keywords.txt $DESTDIR
echo "Copying examples/ ${DESTDIR}"
cp -r examples $DESTDIR
echo "Copy utility/ ${DESTDIR}"
mkdir -p "${DESTDIR}/utility"
cp -L utility/* "${DESTDIR}/utility"
echo "Compressing to ${OUTPUT_FILE}..."
PREVWD=$(pwd); cd ${TEMPDIR}
rm -f "$OUTPUT_FILE"
zip -9r "$OUTPUT_FILE" *
echo "Cleaning up..."
rm -rf $TEMPDIR