refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / fish / install.sh
1 #!/bin/bash
2 set -e
3 set -u
4
5 if ! (uname -a | grep -i "darwin" > /dev/null); then
6     echo "No fish installer for Linux yet. Try this instead:"
7     echo "    sudo apt install -y fish"
8     exit 1
9 fi
10
11 ################
12 # Install fish #
13 ################
14
15 # Every package should define these 6 variables
16 # shellcheck disable=2034
17 pkg_cmd_name="fish"
18
19 pkg_dst_cmd="$HOME/.local/bin/fish"
20 # shellcheck disable=2034
21 pkg_dst="$pkg_dst_cmd"
22
23 pkg_src_cmd="$HOME/.local/opt/fish-v$WEBI_VERSION/bin/fish"
24 # shellcheck disable=2034
25 pkg_src_dir="$HOME/.local/opt/fish-v$WEBI_VERSION"
26 # shellcheck disable=2034
27 pkg_src="$pkg_src_cmd"
28
29 # pkg_install must be defined by every package
30
31 function _macos_post_install() {
32     if ! [ -e "$HOME/.local/bin/fish" ]; then
33         return 0
34     fi
35
36     echo ""
37     echo "Trying to set fish as the default shell..."
38     echo ""
39     # stop the caching of preferences
40     killall cfprefsd
41
42     # Set default Terminal.app shell to fish
43     defaults write com.apple.Terminal "Shell" -string "$HOME/.local/bin/fish"
44     echo "To set 'fish' as the default Terminal.app shell:"
45     echo "    Terminal > Preferences > General > Shells open with:"
46     echo "    $HOME/.local/bin/fish"
47     echo ""
48
49     # Set default iTerm2 shell to fish
50     if [ -e "$HOME/Library/Preferences/com.googlecode.iterm2.plist" ]; then
51         /usr/libexec/PlistBuddy \
52             -c "SET ':New Bookmarks:0:Custom Command' 'Custom Shell'" \
53             "$HOME/Library/Preferences/com.googlecode.iterm2.plist"
54         /usr/libexec/PlistBuddy \
55             -c "SET ':New Bookmarks:0:Command' $HOME/.local/bin/fish" \
56             "$HOME/Library/Preferences/com.googlecode.iterm2.plist"
57         echo "To set 'fish' as the default iTerm2 shell:"
58         echo "    iTerm2 > Preferences > Profiles > General > Command >"
59         echo "    Custom Shell: $HOME/.local/bin/fish"
60         echo ""
61     fi
62
63     killall cfprefsd
64 }
65
66 # always try to reset the default shells
67 _macos_post_install
68
69 function pkg_install() {
70     mv fish.app/Contents/Resources/base/usr/local "$HOME/.local/opt/fish-v${WEBI_VERSION}"
71
72 }
73
74 function pkg_post_install() {
75     # don't skip what webi would do automatically
76     webi_post_install
77
78     # try again to update default shells, now that all files should exist
79     _macos_post_install
80 }
81
82 # pkg_get_current_version is recommended, but (soon) not required
83 function pkg_get_current_version() {
84     # 'fish --version' has output in this format:
85     #       fish, version 3.1.2
86     # This trims it down to just the version number:
87     #       3.1.2
88     fish --version 2> /dev/null | head -n 1 | cut -d ' ' -f 3
89 }