aboutsummaryrefslogtreecommitdiff
path: root/install.sh
blob: f339341aa45a8b767e7773b6ba0c2071dd865190 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env bash

trap 'log WARN "Interrupted by user"; exit 130' INT

INSTALL_STATUS="none"   # none | partial | complete | failed
AUTO_YES=0

#######################
## Arguement Parsing ##
#######################
for arg in "$@"; do
    case "$arg" in
        --yes|--ci|--non-interactive)
            AUTO_YES=1
            ;;
    esac
done

log() {
    local level="$1"
    shift

    local color reset='\033[0m'
    case "$level" in
        INFO)    color='\033[0;34m' ;; # Blue
        WARN)    color='\033[0;33m' ;; # Yellow
        ERROR)   color='\033[0;31m' ;; # Red
        SUCCESS) color='\033[0;32m' ;; # Green
        *)       color='\033[0m' ;;
    esac

    printf '%b[%s] [%s]%b %s\n' \
        "$color" "$(date '+%H:%M:%S')" "$level" "$reset" "$*" >&2
}


## Prevent user from running this script as root
if [[ "$EUID" -eq 0 ]]; then
    log ERROR "This script must NOT be run as root."
    log INFO "If you need elevated privileges, the script will ask for sudo when required."
    exit 1
fi


check_install() {
    if ! command -v $1 >/dev/null 2>&1; then
        echo "$1 is required. Install it first."
        exit 1
    fi
}


choose_packages() {
    local -n arr=$1
    gum choose --no-limit \
        --selected "$(IFS=,; echo "${arr[*]}")" \
        "${arr[@]}"
}

prompt_style() {
    gum style \
        --border rounded \
        --padding "0 1" \
        "$1"
}

gum_warn(){
    gum style --bold --foreground 196 "⚠ $1"
}

gum_to_array() {
    local -n arr="$1"
    mapfile -t arr
    local cleaned=()
    for item in "${arr[@]}"; do
        [[ -n "$item" ]] && cleaned+=("$item")
    done
    arr=("${cleaned[@]}")
}

confirm() {
    local msg="$1"
    if ((AUTO_YES)); then
        log INFO "[auto] $msg → yes"
        return 0
    fi
    gum confirm --default=false "$msg"
}

#################
## GATEKEEPING ##
#################

## Add checks for prerequisites
check_install git
check_install gum
check_install stow

currentDir="$(dirname "$(readlink -f "$0")")"
cd "$currentDir"


DISCLAIMER=$(gum style \
  --border double \
  --border-foreground 196 \
  --padding "1 2" \
  --bold \
"⚠️  DISCLAIMER

This script is provided AS IS, WITHOUT ANY WARRANTY.
There is NO guarantee that it will work on your system.

It may:
• Modify system files
• Overwrite or DELETE existing configuration
• Potentially break your setup

You are STRONGLY ADVISED to back up:
• ~/.config
• ~/.local
• /etc (if modified)

Proceed at your OWN RISK."
)

confirm "$DISCLAIMER

Do you understand the risks and want to continue?" || exit 1


###########
## UTILS ##
###########
source "$currentDir/packages/pkg_utils.sh"
prompt_style "Important utilities (Most likely go with defaults)"
if ((AUTO_YES)); then
    DEV_PKGS=("${pkg_utils[@]}")
else
    gum_to_array UTILITY_PKGS < <(choose_packages pkg_utils)
fi


###############
## DEV TOOLS ##
###############
source "$currentDir/packages/pkg_dev_tools.sh"
prompt_style "Select Development Tools"
if ((AUTO_YES)); then
    DEV_PKGS=("${pkg_dev_tools[@]}")
else
    gum_to_array DEV_PKGS < <(choose_packages pkg_dev_tools)
fi



#######################
## OPTIONAL PACKAGES ##
#######################
source "$currentDir/packages/pkg_optional.sh"
prompt_style "Select Optional Desktop Packages"
if ((AUTO_YES)); then
    OPTIONAL_PKGS=("${pkg_optional[@]}")
else
    gum_to_array OPTIONAL_PKGS < <(choose_packages pkg_optional)
fi



#################
## GPU DRIVERS ##
#################
source "$currentDir/packages/pkg_gpu.sh"
prompt_style "Select GPU drivers"
if ((AUTO_YES)); then
    GPU_PKGS=("${pkg_gpu[@]}")
else
    gum_to_array GPU_PKGS < <(choose_packages pkg_gpu)
fi


##################
## AUR PACKAGES ##
##################
source "$currentDir/packages/pkg_aur.sh"
prompt_style "Select AUR packages"

if ((AUTO_YES)); then
    AUR_PKGS=("${pkg_aur[@]}")
else
    gum_to_array AUR_PKGS < <(choose_packages pkg_aur)
fi



##########################
## Configure Everything ##
##########################
WARNING=$(gum style \
  --border double \
  --border-foreground 196 \
  --padding "1 2" \
  --bold \
  "⚠️  This will DELETE any conflicting files and replace them with symlinks from this repo.
Make sure you have already backed up all your existing config files (~/.config)")

if confirm "$WARNING

Proceed with system configuration (stow, shell)?"; then
    ###################
    ## Stow dotfiles ##
    ###################
    log INFO "Detecting conflicts..."
    conflicts=$(stow . --no-folding -nv 2>&1 | \
        sed -n 's/.*existing target \(.*\) since neither.*/\1/p')

    if [[ -z "$conflicts" ]]; then
        log INFO "No conflicts. Running stow normally..."
        stow . --no-folding \
            && log SUCCESS "Dotfiles stowed successfully" \
            || log ERROR "Stow failed"
    else
        log WARN "These paths conflict and will be removed:"
        printf '  %s\n' $conflicts
        read -rp "Proceed with deleting these files? (y/N): " ok
        if [[ ! "$ok" =~ ^[Yy]$ ]]; then
            log  WARN "Aborted."
            exit 1
        fi

    # Remove conflicts relative to $HOME
    for path in $conflicts; do
        [[ -n "$path" && "$path" != "/" ]] && rm -rf "$HOME/$path" && log INFO "Deleted $path"
    done

    log INFO "Running stow..."
    stow . --no-folding \
        && log INFO "Dotfiles stowed with overwrite." \
        || log ERROR "Stow failed"
    fi

    #################################
    ## Change default shell to zsh ##
    #################################
    log INFO "Changing the default shell to ZSH"
    if [[ "$SHELL" != "$(command -v zsh)" ]]; then
        chsh -s $(which zsh) \
            && log SUCCESS "Default shell successfully set to zsh" \
            || log ERROR "Default shell could not be set to zsh"
    else
        log INFO "Skipping: zsh is already the default shell"
    fi
else
    log ERROR "Aborted stow, didn't configure or install anything"
    exit 1
fi



source "$currentDir/packages/pkg_desktop.sh"
ALL_PKGS=(
    "${DEV_PKGS[@]}"
    "${OPTIONAL_PKGS[@]}"
    "${GPU_PKGS[@]}"
    "${UTILITY_PKGS[@]}"
    "${AUR_PKGS[@]}"
    "${pkg_desktop[@]}"
)

########################
## Install Everything ##
########################
if ((${#ALL_PKGS[@]})); then
    if confirm "Install all the selected packages?"; then
        log INFO "Installing selected packages..."

        ## Installing paru if not installed already
        if ! command -v paru >/dev/null 2>&1; then
            log INFO "Installing Paru (AUR package manager)"
            git clone https://aur.archlinux.org/paru.git
            (cd paru && makepkg -sri)
            rm -rf paru
        fi

        if paru -Syu --needed "${ALL_PKGS[@]}"; then
            INSTALL_STATUS="complete"
        else
            INSTALL_STATUS="failed"
        fi
    else
        log WARN "Package installation skipped by user"
        INSTALL_STATUS="partial"
    fi
else
    log WARN "No packages selected"
    INSTALL_STATUS="partial"
fi



################
## Setup Keyd ##
################
if command -v keyd >/dev/null 2>&1; then
    if confirm "Configure and enable keyd? "; then
        log INFO "Copying keyd configuration to /etc/keyd/default.conf"
        sudo cp "$currentDir/system/etc/keyd/default.conf" /etc/keyd/
        sudo systemctl enable --now keyd.service \
            && log SUCCESS "Successfully enabled keyd.service" \
            || log ERROR "Couldn't enable keyd.service"
    fi
fi


####################
## Final dialogue ##
####################
case "$INSTALL_STATUS" in
    complete)
        gum style \
          --border rounded \
          --border-foreground 42 \
          --padding "1 2" \
          --bold \
        "✔ Installation Complete!

All selected packages were installed successfully.
You may now reboot the system for all the changes to apply."
        ;;
    partial)
        gum style \
          --border rounded \
          --border-foreground 220 \
          --padding "1 2" \
          --bold \
        "⚠ Setup Finished (Partial)

Some steps were skipped by user choice.
Your system was NOT fully configured."
        ;;
    failed)
        gum style \
          --border rounded \
          --border-foreground 196 \
          --padding "1 2" \
          --bold \
        "❌ Installation Failed

One or more steps did not complete successfully.
Check the logs above."
        ;;
    *)
        gum style --bold "Finished."
        ;;
esac