11#! /bin/sh
22
3- APP_NAME=" elixir-get"
4- APP_VERSION=" 0.0.2-dev"
3+ trap " exit 1" TERM
4+ export TOP_PID=$$
5+
6+ APP_NAME=" get-elixir"
7+ APP_VERSION=" 0.0.2"
8+ # DEV_MODE="true" # true/false
59APP_COMMAND=" ./get-elixir.sh"
6- ELIXIR_CSV_URL=" https://raw.githubusercontent.com/elixir-lang/elixir-lang.github.com/master/elixir.csv"
10+ APP_REPO_USER=" eksperimental"
11+ APP_URL=" https://github.com/${APP_REPO_USER} /${APP_NAME} "
12+ APP_GIT_URL=" ${APP_URL} .git"
13+ APP_SCRIPT_URL=" ${APP_URL} /raw/master/get-elixir.sh"
14+ APP_SCRIPT=$( basename " ${APP_SCRIPT_URL} " )
15+ APP_RELEASES_URL=" https://github.com/${APP_REPO_USER} /${APP_NAME} /releases"
16+ ELIXIR_CSV_URL=" https://github.com/elixir-lang/elixir-lang.github.com/raw/master/elixir.csv"
717ELIXIR_RELEASES_URL=" https://github.com/elixir-lang/elixir/releases"
818ELIXIR_RELEASE_TAG_URL=" "
919ELIXIR_TREE_URL=" "
20+ SCRIPT_PATH=" $( dirname " $0 " ) /${APP_SCRIPT} "
1021
1122# DEFAULT
1223DEFAULT_DEST_DIR=" elixir"
@@ -47,7 +58,7 @@ help() {
4758
4859 Package Types:
4960 source Source files
50- precompiled Precompiled files
61+ precompiled Precompiled binaries
5162
5263 Version Number:
5364 'latest' is the default option, and it's not required to specify it
@@ -58,8 +69,9 @@ help() {
5869 Where you want to unpack Elixir. Default value: '${DEFAULT_DEST_DIR} '.
5970
6071 Options:
61- --version Prints version
62- --help Prints help menu
72+ --help Prints help menu
73+ --update-script Replace this script by downloading the latest version
74+ --version Prints version
6375
6476 Usage Examples:
6577
@@ -77,8 +89,15 @@ help() {
7789 ${ELIXIR_RELEASES_URL} "
7890}
7991
92+ exit_script () {
93+ # http://stackoverflow.com/questions/9893667/is-there-a-way-to-write-a-bash-function-which-aborts-the-whole-execution-no-mat
94+ kill -s TERM $TOP_PID
95+ }
96+
8097sanitize_version () {
81- printf ' %s' " $1 " | sed -e ' s/^v//g'
98+ # remove v from version,
99+ # and after that, any "../"
100+ printf ' %s' " $1 " | sed -e ' s/^v//g;s@^\.\./@@g;'
82101}
83102
84103sanitize_dest_dir () {
@@ -87,42 +106,54 @@ sanitize_dest_dir() {
87106
88107get_latest_version () {
89108 local version
90- version=$( curl -s -fL " ${ELIXIR_CSV_URL} " | sed ' 2q;d' | cut -d , -f1)
109+ version=$( curl -sfL " ${ELIXIR_CSV_URL} " | sed ' 2q;d' | cut -d , -f1)
91110 if [ " ${version} " = " " ]; then
92111 echo " * [ERROR] Latest Elixir version number couldn't be retrieved from ${ELIXIR_CSV_URL} " >&2
93- exit 1
112+ exit_script
94113 else
95114 echo " ${version} "
96115 fi
97116}
98117
118+ get_latest_script_version () {
119+ # version=$(curl -s -fL "${APP_RELEASES_URL}" | grep browser_download_url | head -n 1 | cut -d '"' -f 4)
120+ local version=$( curl -sfI " ${APP_RELEASES_URL} /latest" | grep " Location: " | tr ' \r' ' \0' | tr ' \n' ' \0' | rev | cut -d" /" -f1 | rev)
121+ if [ " ${version} " = " " ]; then
122+ echo " * [ERROR] Latest ${APP_NAME} version number couldn't be retrieved from ${APP_RELEASES_URL} " >&2
123+ exit_script
124+ else
125+ echo " $( sanitize_version " ${version} " ) "
126+ fi
127+ }
128+
129+
99130download_source () {
100131 local version=" $1 "
101132 local url=" https://github.com/elixir-lang/elixir/archive/v${version} .tar.gz"
102133 echo " * Downloading ${url} "
103- curl -fL -o " v ${version} .tar.gz " " ${url} "
134+ curl -fL -O " ${url} "
104135 if [ ! -f " v${version} .tar.gz" ]; then
105136 echo " * [ERROR] Elixir v${VERSION} could not be downloaded from ${url} " >&2
106137 if [ " ${VERSION} " != " ${DEFAULT_VERSION} " ]; then
107138 echo " Please make sure version number is a valid one, by checking:" >&2
108139 echo " ${ELIXIR_RELEASES_URL} " >&2
109140 fi
110- exit 1
141+ exit_script
111142 fi
112143}
113144
114145download_precompiled () {
115146 local version=" $1 "
116147 local url=" https://github.com/elixir-lang/elixir/releases/download/v${version} /Precompiled.zip"
117148 echo " * Downloading ${url} "
118- curl -fL -o Precompiled.zip " ${url} "
149+ curl -fL -O " ${url} "
119150 if [ ! -f Precompiled.zip ]; then
120151 echo " * [ERROR] Elixir v${VERSION} could not be downloaded from ${url} " >&2
121152 if [ " ${VERSION} " != " ${DEFAULT_VERSION} " ]; then
122153 echo " Please make sure version number is a valid one, by finding it in:" >&2
123154 echo " ${ELIXIR_RELEASES_URL} " >&2
124155 fi
125- exit 1
156+ exit_script
126157 fi
127158}
128159
@@ -135,7 +166,7 @@ unpack_source() {
135166 rm -rf elixir-${version} / || (
136167 echo " * [ERROR] \" v${version} .tar.gz\" could not be unpacked to ${dest_dir} " >&2
137168 echo " Check the file permissions." >&2
138- exit 1
169+ exit_script
139170 )
140171}
141172
@@ -145,28 +176,76 @@ unpack_precompiled() {
145176 unzip -o -q -d ${dest_dir} Precompiled.zip || (
146177 echo " * [ERROR] \" Precompiled.zip\" could not be unpacked to ${dest_dir} " >&2
147178 echo " Check the file permissions." >&2
148- exit 1
179+ exit_script
149180 )
150181}
151182
183+ update_script () {
184+ if [ " ${DEV_MODE} " = " true" ]; then
185+ echo " * [ERROR] ${APP_NAME} cannot be updated when \` DEV_MODE=\" true\"\` " >&2
186+ exit_script
187+ fi
188+
189+ echo " * Retrieving version number of latest ${APP_NAME} release..."
190+ local latest_script_version=$( get_latest_script_version)
191+ local remote_script_url=" ${APP_URL} /raw/v${latest_script_version} /get-elixir.sh"
192+
193+ if [ " ${latest_script_version} " != " ${APP_VERSION} " ]; then
194+ confirm " * You are about to replace '${SCRIPT_PATH} '.
195+ Do you confirm?" && (
196+ curl -fL -o " ${SCRIPT_PATH} " " ${remote_script_url} " && (
197+ chmod +x " ${SCRIPT_PATH} "
198+ echo " * [OK] ${APP_NAME} succesfully updated"
199+ return 0
200+ ) || (
201+ echo " * [ERROR] ${APP_NAME} could not be downloaded from" >&2
202+ echo " ${remote_script_url} " >&2
203+ exit_script
204+ )
205+ ) || (
206+ echo " * Updating script has been cancelled."
207+ return 1
208+ )
209+ else
210+ echo " * [OK] ${APP_COMMAND} is already the newest version."
211+ return 0
212+ fi
213+ }
214+
215+ confirm () {
216+ local response=" "
217+ read -p " ${1} [Y/N]: " response
218+ # echo ${response}
219+ if printf " %s\n" " ${response} " | grep -Eq " ^[yY].*" ; then
220+ return 0
221+ else
222+ return 1
223+ fi
224+ }
225+
152226do_main () {
153227 # check for help and version
154228 case " $1 " in
155229 " help" | " --help" | " -h" )
156230 help
157- exit 0
231+ return 0
232+ ;;
233+
234+ " update-script" | " --update-script" )
235+ update_script
236+ return 0
158237 ;;
159238
160239 " version" | " --version" | " -v" )
161240 echo " ${APP_NAME} – version ${APP_VERSION} "
162- exit 0
241+ return 0
163242 ;;
164243 esac
165244
166245 # check for minimun number of args
167246 if [ " $# " -lt 2 ]; then
168247 short_help >&2
169- exit 1
248+ exit_script
170249 fi
171250
172251 # Get Variables from ARGS
@@ -188,12 +267,12 @@ do_main() {
188267 # Check for unrecognized options
189268 if [ " ${COMMAND} " != " unpack" ] && [ " ${COMMAND} " != " download" ]; then
190269 echo " * [ERROR] Unrecognized <action> \" ${COMMAND} \" . Try 'unpack' or 'download'." >&2
191- exit 1
270+ exit_script
192271 fi
193272
194273 if [ " ${PACKAGE_TYPE} " != " source" ] && [ " ${PACKAGE_TYPE} " != " precompiled" ]; then
195274 echo " * [ERROR] Unrecognized <package_type> \" ${PACKAGE_TYPE} \" . Try 'source' or 'precompiled'." >&2
196- exit 1
275+ exit_script
197276 fi
198277
199278 # Define variables based on $VERSION
0 commit comments