0c6f506e984f842df9611704224b5a4ec3852069
[webi-installers/.git] / webi / template.bash
1 #!/bin/bash
2
3 {
4
5 set -e
6 set -u
7
8 #WEBI_PKG=
9 #WEBI_NAME=
10 # TODO should this be BASEURL instead?
11 #WEBI_HOST=
12 #WEBI_RELEASES=
13 #WEBI_CSV=
14 #WEBI_VERSION=
15 #WEBI_MAJOR=
16 #WEBI_MINOR=
17 #WEBI_PATCH=
18 # TODO not sure if BUILD is the best name for this
19 #WEBI_BUILD=
20 #WEBI_LTS=
21 #WEBI_CHANNEL=
22 #WEBI_EXT=
23 #WEBI_PKG_URL=
24 #WEBI_PKG_FILE=
25 export WEBI_HOST
26
27 ##
28 ## Set up tmp, download, and install directories
29 ##
30
31 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-${WEBI_PKG:-}.XXXXXXXX)"}
32
33 mkdir -p "$HOME/Downloads"
34 mkdir -p "$HOME/.local/bin"
35 mkdir -p "$HOME/.local/opt"
36
37 ##
38 ## Detect http client
39 ##
40 set +e
41 export WEBI_CURL="$(command -v curl)"
42 export WEBI_WGET="$(command -v wget)"
43 set -e
44
45 webi_download() {
46     if [ -n "${1:-}" ]; then
47         my_url="$1"
48     else
49         if [ "error" == "$WEBI_CHANNEL" ]; then
50             echo "Could not find $WEBI_NAME v$WEBI_VERSION"
51             exit 1
52         fi
53         my_url="$WEBI_PKG_URL"
54         echo "Downloading $WEBI_NAME v$WEBI_VERSION"
55     fi
56     if [ -n "${2:-}" ]; then
57         my_dl="$2"
58     else
59         my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
60     fi
61
62     if [ -n "$WEBI_WGET" ]; then
63         # wget has resumable downloads
64         # TODO wget -c --content-disposition "$my_url"
65         set +e
66         wget -q --show-progress -c "$my_url" --user-agent="wget $WEBI_UA" -O "$my_dl"
67         if ! [ $? -eq 0 ]; then
68           echo "failed to download from $WEBI_PKG_URL"
69           exit 1
70         fi
71         set -e
72     else
73         # BSD curl is non-resumable, hence we don't bother
74         # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
75         curl -fSL "$my_url" -H "User-Agent: curl $WEBI_UA" -o "$my_dl"
76     fi
77 }
78
79 webi_extract() {
80     pushd "$WEBI_TMP" 2>&1 >/dev/null
81         if [ "tar" == "$WEBI_EXT" ]; then
82             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
83             tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
84         elif [ "zip" == "$WEBI_EXT" ]; then
85             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
86             unzip "$HOME/Downloads/$WEBI_PKG_FILE"
87         else
88             # do nothing
89             echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
90             exit 1
91         fi
92     popd 2>&1 >/dev/null
93 }
94
95 webi_path_add() {
96     # make sure that we don't recursively install pathman with webi
97     my_path="$PATH"
98     export PATH="$HOME/.local/bin:$PATH"
99     set +e
100     my_pathman=$(command -v pathman)
101     set -e
102     export PATH="$my_path"
103
104     # install pathman if not already installed
105     if [ -z "$my_pathman" ]; then
106         "$HOME/.local/bin/webi" pathman
107         "$HOME/.local/bin/pathman" add "$HOME/.local/bin"
108         export PATH="$HOME/.local/bin:$PATH"
109     fi
110
111     # in case pathman was recently installed and the PATH not updated
112     "$HOME/.local/bin/pathman" add "$1"
113 }
114
115 ##
116 ##
117 ## BEGIN user-submited script
118 ##
119 ##
120
121 {{ installer }}
122
123 ##
124 ##
125 ## END user-submitted script
126 ##
127 ##
128
129 }