summaryrefslogtreecommitdiff
path: root/static/thesiah-mac.sh
blob: b793c9a21f7fdf38250cbfb5850bf52d0e70ea41 (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
#!/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/thesiah/.dotfiles.git"
progsfile="https://raw.githubusercontent.com/thesiah/THESIAH/main/static/macprogs.csv"
repobranch="main"
name=$(whoami) # Use the current user's name


### 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
}

# Install Homebrew packages
installpkg() {
	if brew list "$1" &>/dev/null; then
		echo "$1 is already installed."
	else
		echo "Installing $1..."
		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..."
		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"
    brew install "$pkg" || error "Failed to install $pkg from tap."
}

# Install mac apps with mas
masinstall() {
    echo "Installing the mac app: $1"
    if ! command -v mas &>/dev/null; then
        installpkg mas # This installs mas
    fi

    # Search for the app by name and get its ID using awk to parse the output
    id=$(mas search "$1" | awk -F ' ' '{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
        echo "Installing the app with ID $id..."
        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"
}

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
}

### Main Installation Process ###

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

# Start the installation loop for software from the CSV
installationloop

# Setup Dotfiles
putgitrepo "$dotfilesrepo" "$HOME" "$repobranch"

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

ln -s ~/.dotfiles/mac/.zprofile ~/.zprofile

# Final
finalize