summaryrefslogtreecommitdiff
path: root/install/util.fish
blob: 9b258e0e6bbf01f6801b1a23670a72bf1419228c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
. (dirname (status filename))/../util.fish

function confirm-overwrite -a path
    if test -e $path -o -L $path
        read -l -p "input '$(realpath $path 2> /dev/null || echo $path) already exists. Overwrite? [y/N] ' -n" confirm
        if test "$confirm" = 'y' -o "$confirm" = 'Y'
            log 'Continuing.'
            test -z "$argv[2]" && rm -rf $path  # If a second arg is provided, don't delete
        else
            log 'Exiting.'
            exit
        end
    end
end

function install-deps
    # All dependencies already installed
    pacman -Q $argv &> /dev/null && return

    for dep in $argv
        # Skip if already installed
        if ! pacman -Q $dep &> /dev/null
            # If pacman can install it, use it, otherwise use an AUR helper
            if pacman -Si $dep &> /dev/null
                log "Installing dependency '$dep'"
                sudo pacman -S --noconfirm $dep
            else
                # Get AUR helper or install if none
                which yay &> /dev/null && set -l helper yay || set -l helper paru
                if ! which $helper &> /dev/null
                    warn 'No AUR helper found'
                    read -l -p "input 'Install yay? [Y/n] ' -n" confirm
                    if test "$confirm" = 'n' -o "$confirm" = 'N'
                        warn "Manually install yay or paru and try again."
                        warn "Alternatively, install the dependencies '$argv' manually and try again."
                        exit
                    else
                        sudo pacman -S --needed git base-devel
                        git clone https://aur.archlinux.org/yay.git
                        cd yay
                        makepkg -si
                        cd ..
                        rm -rf yay

                        # First use, see https://github.com/Jguer/yay?tab=readme-ov-file#first-use
                        yay -Y --gendb
                        yay -Y --devel --save
                    end
                end

                log "Installing dependency '$dep'"
                $helper -S --noconfirm $dep
            end
        end
    end
end

function install-optional-deps
    for dep in $argv
        set -l dep_name (string split -f 1 ' ' $dep)
        if ! pacman -Q $dep_name &> /dev/null
            read -l -p "input 'Install $dep? [Y/n] ' -n" confirm
            test "$confirm" != 'n' -a "$confirm" != 'N' && install-deps $dep_name
        end
    end
end

function update-repo -a module dir
    set -l remote https://github.com/caelestia-dots/$module.git
    if test -d $dir
        cd $dir || exit

        # Delete and clone if it's a different git repo
        if test "$(git config --get remote.origin.url)" != $remote
            cd .. || exit
            confirm-overwrite $dir
            git clone $remote $dir
        else
            # Check for uncommitted changes
            if test -n "$(git status --porcelain)"
                read -l -p "input 'You have uncommitted changes in $dir. Stash, reset or exit? [S/r/e] ' -n" confirm

                if test "$confirm" = 'e' -o "$confirm" = 'E'
                    log 'Exiting...'
                    exit
                end

                if test "$confirm" = 'r' -o "$confirm" = 'R'
                    log 'Resetting to HEAD...'
                    git reset --hard
                else
                    log 'Stashing changes...'
                    git stash
                end
            end

            git pull
        end
    else
        git clone $remote $dir
    end
end

function setup-systemd-monitor -a module dir
    set -l systemd $CONFIG/systemd/user
    if which systemctl &> /dev/null
        log 'Installing systemd service...'

        mkdir -p $systemd
        echo "[Unit]
Description=Sync $module and caelestia schemes

[Service]
Type=oneshot
ExecStart=$dir/monitor/update.fish" > $systemd/$module-monitor-scheme.service
        echo "[Unit]
Description=Sync $module and caelestia schemes (monitor)

[Path]
PathModified=%S/caelestia/scheme/current.txt
Unit=$module-monitor-scheme.service

[Install]
WantedBy=default.target" > $systemd/$module-monitor-scheme.path

        systemctl --user daemon-reload
        systemctl --user enable --now $module-monitor-scheme.path
        systemctl --user start $module-monitor-scheme.service
    end
end

function install-link -a from to
    if ! test -L $to -a "$(realpath $to 2> /dev/null)" = $from
        mkdir -p (dirname $to)
        confirm-overwrite $to
        ln -s $from $to
    end
end

function confirm-copy -a from to
    test -L $to -a "$(realpath $to 2> /dev/null)" = (realpath $from) && return  # Return if symlink
    cmp $from $to &> /dev/null && return  # Return if files are the same
    if test -e $to
        read -l -p "input '$(realpath $to) already exists. Overwrite? [y/N] ' -n" confirm
        test "$confirm" = 'y' -o "$confirm" = 'Y' && log 'Continuing.' || return
    end
    cp $from $to
end