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