summaryrefslogtreecommitdiff
path: root/static/thesiah-mac.sh
blob: 608812f1907daa5275244df8d2a8b7ce5b646295 (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
#!/bin/sh

# Soomin's Auto Rice Bootstrapping Script (THESIAH) adapted for macOS
# Adaptation by: Soomin Im <si@thesiah.xyz>
# License: GNU GPLv3


### VARIABLES ###
dotfilesrepo="https://github.com/TheSiahxyz/mac.git"
progsfile="https://raw.githubusercontent.com/thesiah/THESIAH/main/static/macprogs.csv"
repobranch="main"


### FUNCTIONS ###

# Notify function using macOS's notification system
notify() {
	osascript -e "display notification \"$2\" with title \"$1\""
}

# Error function for error handling and notification
error() {
	# Log to stderr and exit with failure.
	printf "%s\n" "$1" >&2
	exit 1
}

# Get user's password
getuserandpass() {
	# Prompts user for new username an password.
	pass1=$(echo "Enter a password for that user:")
	pass2=$(echo "Retype password:")
	while ! [ "$pass1" = "$pass2" ]; do
		unset pass2
		pass1=$(echo "Passwords do not match.\\n\\nEnter password again:")
		pass2=$(echo "Retype password:")
	done
}

# Install Homebrew
brewinstall() {
    if ! command -v brew &>/dev/null; then
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    fi
    brew update >/dev/null 2>&1
    brew upgrade >/dev/null 2>&1
    eval "$(/opt/homebrew/bin/brew shellenv)"
}

# Install Homebrew packages
installpkg() {
	if brew list "$1" &>/dev/null; then
		echo "$1 is already installed."
	else
		echo "Installing $1..."
		$pass1 | brew install "$1" || error "Failed to install $1."
	fi
}

# Install Homebrew Cask applications
caskinstall() {
	if brew list --cask "$1" &>/dev/null; then
		echo "$1 is already installed."
	else
		echo "Installing $1..."
		$pass1 | brew install --cask "$1" || error "Failed to install $1."
	fi
}

# Install Python packages with pip
pipinstall() {
	echo "Installing the Python package: $1"
	if ! command -v pip3 &>/dev/null; then
		installpkg python # This installs pip3
	fi
	pip3 install "$1" || error "Failed to install Python package $1."
}

# Install Homebrew Tap packages
tapinstall() {
    # Extract the repository and package name
    local repo=$(echo "$1" | cut -d'/' -f1)
    local pkg=$(echo "$1" | cut -d'/' -f2)

    echo "Adding tap: $repo"
    brew tap "$repo" || error "Failed to tap $repo."

    echo "Installing package: $pkg from tap"
    $pass1 | brew install "$pkg" || error "Failed to install $pkg from tap."
}

# Install mac apps with mas
masinstall() {
    echo "Installing the mac app: $1"

    # Search for the app by name and get its ID using awk to parse the output
    id=$(mas search "$1" | awk -v appName="$1" '{if ($0 ~ appName) {print $1; exit}}')
    # Check if the id variable is set and not empty
    if [ -z "$id" ]; then
        error "Failed to find an ID for the app named $1."
    else
        mas install "$id" || error "Failed to install the app $1."
    fi
}

# Clone and setup dotfiles
putgitrepo() {
	notify "Installation" "Downloading and installing config files..."
	local dir=$(mktemp -d)
	if [ ! -d "$2" ]; then
		mkdir -p "$2"
	fi
	git clone --depth 1 --single-branch --no-tags -q --recursive -b "${3:-$repobranch}" "$1" "$dir" || error "Failed to clone $1."
	cp -Rf "$dir/"* "$2"
	rm -rf "$dir" # Clean up
}

# Installation loop for processing the programs.csv file
installationloop() {
    ([ -f "$progsfile" ] && cp "$progsfile" /tmp/programs.csv) || curl -Ls "$progsfile" | sed '/^#/d' >/tmp/programs.csv || error "Failed to download programs file."
    while IFS=, read -r tag program comment; do
        echo "$comment"
        case "$tag" in
            "P") pipinstall "$program" ;;
            "C") caskinstall "$program" ;;
            "M") masinstall "$program" ;;
            "T") tapinstall "$program" ;;
            *) installpkg "$program" ;;
        esac
    done </tmp/programs.csv
}

finalize() {
	# Farewell
	echo "All done!"
	echo "Congrats! Provided there were no hidden errors, the script completed successfully and all the programs and configuration files should be in place.\\n\\nTo run the new graphical environment, log out and log back in as your new user, then run the command \"startx\" to start the graphical environment (it will start automatically in tty1).\\n\\n.t Soomin"
}

### Main Installation Process ###
# Store user's password
getuserandpass

# Ensure Homebrew is installed
brewinstall || error "Failed to install Homebrew."

# Ensure mas is installed
installpkg "mas"

# Start the installation loop for software from the CSV
installationloop

# Setup Dotfiles
putgitrepo "$dotfilesrepo" "$HOME" "$repobranch" && mv "$HOME/$(echo "$dotfilesrepo" | sed 's|.*/\([^/]*\)\.git|\1|')" "$HOME/.dotfiles"

# Clean up any unwanted files from the dotfiles repo
rm -rf "$HOME/.git" "$HOME/README.md" "$HOME/LICENSE" "$HOME/FUNDING.yml" "/tmp/programs.csv"

# Sync profile
ln -sf ~/.dotfiles/shell/profile ~/.zprofile

# Final
finalize