PATH and path bugfixes
[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
26 ##
27 ## Set up tmp, download, and install directories
28 ##
29
30 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-go.XXXXXXXX)"}
31
32 mkdir -p "$HOME/Downloads"
33 mkdir -p "$HOME/.local/bin"
34 mkdir -p "$HOME/.local/opt"
35
36 ##
37 ## Detect http client
38 ##
39 set +e
40 export WEBI_CURL="$(command -v curl)"
41 export WEBI_WGET="$(command -v wget)"
42 set -e
43
44 webi_download() {
45     if [ -n "${1:-}" ]; then
46         my_url="$1"
47     else
48         my_url="$WEBI_PKG_URL"
49         echo "Downloading $WEBI_NAME v$WEBI_VERSION"
50     fi
51     if [ -n "${2:-}" ]; then
52         my_dl="$2"
53     else
54         my_dl="$HOME/Downloads/$WEBI_PKG_FILE"
55     fi
56
57     if [ -n "$WEBI_WGET" ]; then
58         # wget has resumable downloads
59         # TODO wget -c --content-disposition "$my_url"
60         wget -q --show-progress -c "$my_url" --user-agent="wget $WEBI_UA" -O "$my_dl"
61     else
62         # BSD curl is non-resumable, hence we don't bother
63         # TODO curl -fsSL --remote-name --remote-header-name --write-out "$my_url"
64         curl -fSL "$my_url" -H "User-Agent: curl $WEBI_UA" -o "$my_dl"
65     fi
66 }
67
68 webi_extract() {
69     pushd "$WEBI_TMP" 2>&1 >/dev/null
70         if [ "tar" == "$WEBI_EXT" ]; then
71             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
72             tar xf "$HOME/Downloads/$WEBI_PKG_FILE"
73         elif [ "zip" == "$WEBI_EXT" ]; then
74             echo "Extracting $HOME/Downloads/$WEBI_PKG_FILE"
75             unzip "$HOME/Downloads/$WEBI_PKG_FILE"
76         else
77             # do nothing
78             echo "Failed to extract $HOME/Downloads/$WEBI_PKG_FILE"
79             exit 1
80         fi
81     popd 2>&1 >/dev/null
82 }
83
84 webi_path_add() {
85     # make sure that we don't recursively install pathman with webi
86     my_path="$PATH"
87     export PATH="$HOME/.local/bin:$PATH"
88     set +e
89     my_pathman=$(command -v pathman)
90     set -e
91     export PATH="$my_path"
92
93     # install pathman if not already installed
94     if [ -z "$my_pathman" ]; then
95         "$HOME/.local/bin/webi" pathman
96         "$HOME/.local/bin/pathman" add "$HOME/.local/bin"
97         export PATH="$HOME/.local/bin:$PATH"
98     fi
99
100     # in case pathman was recently installed and the PATH not updated
101     "$HOME/.local/bin/pathman" add "$1"
102 }
103
104 ##
105 ##
106 ## BEGIN user-submited script
107 ##
108 ##
109
110 {{ installer }}
111
112 ##
113 ##
114 ## END user-submitted script
115 ##
116 ##
117
118 }