-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.sh
executable file
·87 lines (69 loc) · 2.55 KB
/
upload.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
#!/usr/bin/env bash
# usage:
# ./upload.sh theme-name user:pass http://yourserver.org
THEMENAME=$1
USER_PASS=$2
SERVER=$3
[[ -z $1 ]] && echo "File name required." && exit 1;
[[ -z $2 ]] && echo "Username (and optional password) required." && exit 1;
# [[ $2 != *:* ]] && echo "Please enter username and password in the format 'username:password'." && exit
[[ -z $3 ]] && echo "Server hostname required. ['http://server.org']" && exit 1;
# strip '.zip' from THEMENAME argument if entered
[[ ${THEMENAME} == *.zip ]] && THEMENAME=${THEMENAME%.zip}
# strip 'themes/' if entered
[[ ${THEMENAME} =~ themes/* ]] && THEMENAME=${THEMENAME##*themes/}
cd ./themes
echo ""
echo "Getting '${THEMENAME}' theme..."
echo ""
[[ -e "${THEMENAME}.zip" ]] && PACKAGE="${THEMENAME}.zip"
[[ -z ${PACKAGE} && -e "${THEMENAME}" ]] \
&& { echo "Creating theme package..."; echo ""; } \
&& zip -r ${THEMENAME} ${THEMENAME} -x \*__MAC* \*.DS_Store \
&& PACKAGE="${THEMENAME}.zip" \
&& { echo ""; echo "Theme packaged into .zip file."; echo ""; }
[[ -z ${PACKAGE} || ! -e ${PACKAGE} ]] && echo "Theme package '${THEMENAME}' not found." && exit 1;
# add XAPI endpoint for URL
URL="${SERVER}/xapi/theme"
# NAME_PARAM="name=\"themePackage\""
# FILE_PARAM="filename=\"${PACKAGE}\""
# FILE_PARAM="filename='${PACKAGE}';type='application/zip'"
# Complex file data upload via curl
# https://stackoverflow.com/questions/10765243/
delim="-----MultipartDelimeter$$$RANDOM$RANDOM$RANDOM"
nl=$'\r\n'
mime="application/zip"
# This is the "body" of the request.
formData() {
# Also make sure to set the fields you need.
printf %s "--${delim}${nl}"
printf %s "Content-Disposition: form-data; name=\"themePackage\"; filename=\"${PACKAGE}\"${nl}"
printf %s "Content-Type: application/zip${nl}${nl}"
cat "${PACKAGE}"
printf %s "${nl}--${delim}--${nl}"
}
response=`formData | curl -v -X POST "${URL}" -u "${USER_PASS}" -H "content-type: multipart/form-data; boundary=${delim}" --data-binary @-`
if [[ ! -z ${response} ]]; then
echo ""
echo "Theme uploaded."
echo "response: ${response}"
echo ""
read -p "Would you like to set the active theme to '${THEMENAME}'? [y/N] " YN
echo ""
if [[ ${YN} =~ [Yy] ]]; then
activeTheme=`curl -v -X PUT "${URL}/${THEMENAME}" -u "${USER_PASS}"`
echo ""
echo "Theme uploaded and activated."
echo "response: ${activeTheme}"
echo ""
else
echo "Theme uploaded but not activated."
echo ""
fi
else
echo ""
echo "Upload failed."
echo ""
fi
cd - >/dev/null
exit 0