generalize, a lot
[webi-installers/.git] / _example / install.sh
1 #!/bin/bash
2
3 set -e
4 set -u
5
6 ## The defaults can be assumed if these are not set
7
8 ## The command name may be different from the package name
9 ## (i.e. golang => go, rustlang => cargo, ripgrep => rg)
10 ## Note: $HOME may contain special characters and should alway be quoted
11
12 pkg_cmd_name="xmpl"
13
14 ## Some of these directories may be the same, in some cases
15 #pkg_dst="$HOME/.local/opt/xmpl"
16 #pkg_dst_bin="$HOME/.local/opt/xmpl/bin"
17 #pkg_dst_cmd="$HOME/.local/opt/xmpl/bin/xmpl"
18
19 #pkg_src="$HOME/.local/opt/xmpl-v$WEBI_VERSION"
20 #pkg_src_bin="$HOME/.local/opt/xmpl-v$WEBI_VERSION/bin"
21 #pkg_src_cmd="$HOME/.local/opt/xmpl-v$WEBI_VERSION/bin/xmpl"
22
23 # Different packages represent the version in different ways
24 # ex: node v12.8.0 (leading 'v')
25 # ex: go1.14 (no space, nor trailing '.0's)
26 # ex: flutter 1.17.2 (plain)
27 pkg_format_cmd_version() {
28     my_version=$1
29     echo "$pkg_cmd_name v$my_version"
30 }
31
32 # The version info should be reduced to a sortable version, without any leading characters
33 # (i.e. v12.8.0 => 12.8.0, go1.14 => 1.14, 1.12.13+hotfix => 1.12.13+hotfix)
34 pkg_get_current_version() {
35     echo "$(xmpl --version 2>/dev/null | head -n 1 | cut -d' ' -f2)"
36 }
37
38 # For (re-)linking to the desired installed version
39 # (for example: 'go' is special and needs both $HOME/go and $HOME/.local/opt/go)
40 # (others like 'rg', 'hugo', and 'caddy' are single files that just get replaced)
41 pkg_link() {
42     rm -rf "$pkg_dst"
43     ln -s "$pkg_src" "$pkg_dst"
44 }
45
46 pkg_pre_install() {
47     # web_* are defined in webi/template.bash at https://github.com/webinstall/packages
48
49     # if selected version is installed, re-link it and quit
50     webi_check
51
52     # will save to ~/Downloads/$WEBI_PKG_FILE by default
53     webi_download
54
55     # supported formats (.xz, .tar.*, .zip) will be extracted to $WEBI_TMP
56     webi_extract
57 }
58
59 # For installing from the extracted package tmp directory
60 pkg_install() {
61     # remove the versioned folder, just in case it's there with junk
62     rm -rf "$pkg_src"
63
64     # rename the entire extracted folder to the new location
65     # (this will be "$HOME/.local/opt/xmpl-v$WEBI_VERSION" by default)
66     mv ./"$pkg_cmd_name"* "$pkg_src"
67 }
68
69 # For updating PATHs and installing companion tools
70 pkg_post_install() {
71     pkg_link
72
73     # web_path_add is defined in webi/template.bash at https://github.com/webinstall/packages
74     webi_path_add "$pkg_dst_bin"
75 }
76
77 pkg_done_message() {
78     echo "Installed 'example' as 'xmpl' at $pkg_dst_cmd"
79 }