99fba3549c3faf30b9691b5f0bb7201d19c9db87
[webi-installers/.git] / _example / install.bash
1 #!/bin/bash
2
3 set -e
4 set -u
5
6 ###################
7 # Install foobar #
8 ###################
9
10 common_opt="${HOME}/.local/opt/foobar-v${WEBI_VERSION}"
11 new_opt="${HOME}/.local/opt/foobar-v${WEBI_VERSION}"
12 new_bin="${HOME}/.local/opt/foobar-v${WEBI_VERSION}/bin/foobar"
13
14 update_installed() {
15     rm -rf "$common_opt"
16     ln -s "$new_opt" "$common_opt"
17
18     # TODO get better output from pathman / output the path to add as return to webi bootstrap
19     webi_path_add "$common_opt/bin"
20     webi_path_add "$HOME/foobar/bin"
21 }
22
23 if [ -x "$new_opt/bin/foobar" ]; then
24   update_installed
25   exit 0
26 fi
27
28 # Test for existing version
29 set +e
30 cur_go="$(command -v foobar)"
31 set -e
32 if [ -n "$cur_go" ]; then
33   cur_ver=$(foobar version | cut -d' ' -f3 | sed 's:foobar::')
34   if [ "$cur_ver" == "$(echo $WEBI_VERSION | sed 's:\.0::g')" ]; then
35     echo "foobar v$WEBI_VERSION already installed at $cur_go"
36     exit 0
37   elif [ "$cur_go" != "$new_bin" ]; then
38     echo "WARN: possible conflict with foobar v$WEBI_VERSION at $cur_go"
39   fi
40 fi
41
42
43 # Note: this file is `source`d by the true installer and hence will have the webi functions
44
45 # because we created releases.js we can use webi_download()
46 # downloads foobar to ~/Downloads
47 webi_download
48
49 # because this is tar or zip, we can webi_extract()
50 # extracts to the WEBI_TMP directory, raw (no --strip-prefix)
51 webi_extract
52
53 pushd "$WEBI_TMP" 2>&1 >/dev/null
54     echo Installing foobar v${WEBI_VERSION} as "$new_bin"
55
56     # simpler for single-binary commands
57     #mv ./example*/bin/example "$HOME/.local/bin"
58
59     # best for packages and toolchains
60     rm -rf "$new_opt"
61     if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
62       rsync -Krl ./foobar*/ "$new_opt/" 2>/dev/null
63     else
64       cp -Hr ./foobar*/* "$new_opt/" 2>/dev/null
65       cp -Hr ./foobar*/.* "$new_opt/" 2>/dev/null
66     fi
67     rm -rf ./foobar*
68
69 popd 2>&1 >/dev/null
70
71 ###################
72 #   Update PATH   #
73 ###################
74
75 update_installed
76
77 echo "Installed 'foobar'"
78 echo ""