|
| 1 | +#!/usr/bin/env sh |
| 2 | +# |
| 3 | +# =============================================================== |
| 4 | +# |
| 5 | +# Filename: install.sh |
| 6 | +# |
| 7 | +# Author: Oxnz |
| 8 | +# Email: yunxinyi@gmail.com |
| 9 | +# Created: [2014-12-09 19:11:50 CST] |
| 10 | +# Last-update: 2014-12-09 19:11:50 CST |
| 11 | +# Description: ANCHOR |
| 12 | +# |
| 13 | +# Version: 0.0.1 |
| 14 | +# Revision: [None] |
| 15 | +# Revision history: [None] |
| 16 | +# Date Author Remarks: [None] |
| 17 | +# |
| 18 | +# License: |
| 19 | +# Copyright (c) 2013 Oxnz |
| 20 | +# |
| 21 | +# Distributed under terms of the [LICENSE] license. |
| 22 | +# [license] |
| 23 | +# |
| 24 | +# =============================================================== |
| 25 | +# |
| 26 | + |
| 27 | +set -e |
| 28 | + |
| 29 | +PROGNAME='INSTALL' |
| 30 | +# version info, ref: http://semver.org |
| 31 | +MAJORVER='0' |
| 32 | +MINORVER='1' |
| 33 | +PATCHVER='0' |
| 34 | +RELEASE='beta' |
| 35 | +VERSION="${MAJORVER}.${MINORVER}.${PATCHVER}-${RELEASE}" |
| 36 | + |
| 37 | +# output |
| 38 | +VERBOSE=0 |
| 39 | +DEBUG=0 |
| 40 | + |
| 41 | +# specify the install destination directory |
| 42 | +PREFIX="${HOME}" |
| 43 | +DESTDIR="${PREFIX}/.shell" |
| 44 | + |
| 45 | +# url for the archive |
| 46 | +URI='https://github.com/oxnz/shell-utils/xx' |
| 47 | + |
| 48 | +msgdump() { |
| 49 | + if [ $# -lt 2 ]; then |
| 50 | + msgdump "error" "msgdump: 2+ arguments expected, but $# found: '$@'" |
| 51 | + return 1 |
| 52 | + fi |
| 53 | + local level="$1" |
| 54 | + shift |
| 55 | + local msg="$*" |
| 56 | + case "${level}" in |
| 57 | + debug) |
| 58 | + if [ "${VERBOSE}" -gt 0 ]; then |
| 59 | + echo "[${PROGNAME}] ${level}: ${msg}" |
| 60 | + fi |
| 61 | + ;; |
| 62 | + info) |
| 63 | + echo "[${PROGNAME}] ${msg}" |
| 64 | + ;; |
| 65 | + warn|warning|error) |
| 66 | + echo "[${PROGNAME}] ${level}: ${msg}" >&2 |
| 67 | + ;; |
| 68 | + esac |
| 69 | +} |
| 70 | + |
| 71 | +optparse() { |
| 72 | + local OPTIND=1 |
| 73 | + local opt |
| 74 | + |
| 75 | + while getopts "d:hv" opt; do |
| 76 | + case $opt in |
| 77 | + d) |
| 78 | + DESTDIR=$OPTARG |
| 79 | + if [ -d $DESTDIR ]; then |
| 80 | + msgdump 'error' "directory already exists: [$DESTDIR]" |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + ;; |
| 84 | + D) |
| 85 | + DEBUG=1 |
| 86 | + VERBOSE=$((VERBOSE+1)) |
| 87 | + set -x |
| 88 | + ;; |
| 89 | + h) |
| 90 | + cat << EOH |
| 91 | +Usage: $0 [options]" |
| 92 | + Options: |
| 93 | + -d DIR specify the installation directory |
| 94 | + -D enable debug mode |
| 95 | + -h show this message and exit |
| 96 | + -v specify verbose level |
| 97 | +EOH |
| 98 | + return |
| 99 | + ;; |
| 100 | + v) |
| 101 | + VERBOSE=$((VERBOSE+1)) |
| 102 | + ;; |
| 103 | + esac |
| 104 | + done |
| 105 | + shift $((OPTIND-1)) |
| 106 | + |
| 107 | + if [ $# -ne 0 ]; then |
| 108 | + msgdump 'error' "unexpected argument(s): $@" |
| 109 | + return 1 |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +initialize() { |
| 114 | + msgdump 'info' " Shell-utils installer ${VERSION}" |
| 115 | + case "$0" in |
| 116 | + csh|tcsh) |
| 117 | + msgdump 'warn' "support for $0 is incomplete for now" |
| 118 | + msgdump 'warn' "please do not use in production" |
| 119 | + ;; |
| 120 | + ash) |
| 121 | + msgdump 'error' "sorry, ash is not supported for now" |
| 122 | + ;; |
| 123 | + *) |
| 124 | + msgdump 'debug' "install for shell: [$0]" |
| 125 | + ;; |
| 126 | + esac |
| 127 | +} |
| 128 | + |
| 129 | +finalize() { |
| 130 | + msgdump 'debug' 'finalize success' |
| 131 | +} |
| 132 | + |
| 133 | +populate() { |
| 134 | + msgdump 'info' "retrieve archive from uri: [${URI}]" |
| 135 | + local archive |
| 136 | + if ! archive="$(mktemp)"; then |
| 137 | + msgdump 'error' 'cannot create temporary archive' |
| 138 | + return 1 |
| 139 | + else |
| 140 | + trap "rm -f ${archive}" INT HUP QUIT TERM KILL |
| 141 | + fi |
| 142 | + if which wget > /dev/null; then |
| 143 | + if wget -nv "${URI}" -O "${archive}"; then |
| 144 | + msgdump 'debug' "download success: [file=${archive}]" |
| 145 | + else |
| 146 | + msgdump 'error' "unable to download shell-utils" |
| 147 | + return 1 |
| 148 | + fi |
| 149 | + elif which curl > /dev/null; then |
| 150 | + msgdump 'debug' "'wget' not found, fallback to curl" |
| 151 | + if curl -o "${archive}" "${URI}"; then |
| 152 | + msgdump 'debug' "download success: [file=${archive}]" |
| 153 | + else |
| 154 | + msgdump 'error' "unable to download shell-utils" |
| 155 | + return 1 |
| 156 | + fi |
| 157 | + else |
| 158 | + msgdump 'error' "cannot find 'wget' or 'curl' which is used to download the shell-utils" |
| 159 | + fi || msgdump 'error' "unable to download shell-utils" |
| 160 | + msgdump 'info' "extract archive: [${archive}] to [${DESTDIR}]" |
| 161 | + if tar pzxf "${archive}" -C "${DESTDIR}"; then |
| 162 | + msgdump 'info' 'extract failed' |
| 163 | + return 1 |
| 164 | + else |
| 165 | + msgdump 'debug' "extracted successfully" |
| 166 | + fi |
| 167 | +} |
| 168 | + |
| 169 | +main() { |
| 170 | + optparse |
| 171 | + if ! initialize; then |
| 172 | + msgdump 'error' 'installer init failed' |
| 173 | + return 1 |
| 174 | + fi |
| 175 | + if ! populate; then |
| 176 | + msgdump 'error' 'install process failed' |
| 177 | + return 1 |
| 178 | + fi |
| 179 | + if ! finalize; then |
| 180 | + msgdump 'debug' 'installer finished with non-zero status' |
| 181 | + return 1 |
| 182 | + fi |
| 183 | + msgdump 'info' 'install finished successfully, enjoy!' |
| 184 | +} |
| 185 | + |
| 186 | +main $@ |
| 187 | + |
| 188 | +# detect shell type: |
| 189 | +#http://stackoverflow.com/questions/3327013/how-to-determine-the-current-shell-im-working-on#comment17728546_3327013 |
0 commit comments