-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Installer.sh
182 lines (149 loc) · 4.85 KB
/
_Installer.sh
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
#!/bin/bash
### USAGE
## INSTALL
# curl -sSL https://raw.githubusercontent.com/mi5hmash/SteamDeckBTRFS/main/_Installer.sh | bash
## UNINSTALL
# curl -sSL https://raw.githubusercontent.com/mi5hmash/SteamDeckBTRFS/main/_Installer.sh | bash -s -- -u
### GET OPTS
# Default values
UNINSTALL_OPT=false
# Get opts
while getopts "u" opt; do
case $opt in
u) UNINSTALL_OPT=true;;
\?) ;;
esac
done
# Shift to process remaining arguments
shift $((OPTIND-1))
### GLOBAL VARIABLES
## MAIN VARIABLES
ENV_DOCUMENTS_DIR=$(xdg-user-dir DOCUMENTS); readonly ENV_DOCUMENTS_DIR
## CONFIGURATION - change as per your needs
declare -r UserName="$USER"
declare -r AuthorName="mi5hmash"
declare -r ToolName="SteamDeckBTRFS"
#declare -r RepoUrl="https://raw.githubusercontent.com/$AuthorName/$ToolName/main"
declare -r TempDir="/tmp/$AuthorName.$ToolName.install"
declare -r InstallDir="$ENV_DOCUMENTS_DIR/$ToolName"
declare -r CreateShortcutScriptDir="$InstallDir/_Create a Shortcut on Desktop.sh"
### STYLES
clr0="\e[0m";
echo_E() { echo -e "\e[1;31m[ERROR] $1$clr0"; }
echo_S() { echo -e "\e[1;96m[SUCCESS] $1$clr0"; }
echo_W() { echo -e "\e[1;93m[WARNING] $1$clr0"; }
echo_I() { echo -e "\e[1;34m[INFO] $1$clr0"; }
### FUNCTIONS
## Asks question using zenity
# $1-Question
_zenityQuestion() {
zenity --question \
--title="$ToolName Installer" \
--width="300" \
--text="$1" 2> /dev/null
case $? in
0) echo 1;;
*) exit;;
esac
}
## Gets info about releases from GitHub's REST API
# $1-API path after 'https://api.github.com/'
# USAGE: _GIT_getInfo "repos/author/tool/releases/latest"
_GIT_getInfo() { curl -sL "https://api.github.com/$1"; }
## Gets version value from json formatted input
# $1-Json formatted input
# USAGE: _GIT_getLatestVersion "$(cat "./test.json")"
_GIT_getLatestVersion() { echo "$1" | jq -r ".tag_name"; }
## Gets download link from json formatted input
# $1-Json formatted input; $2-Filename
# USAGE: _GIT_getDownloadLink "$(cat "./test.json")"
_GIT_getDownloadLink() { echo "$1" | jq -r ".assets[] | select(.name | contains(\"$2\")) | .browser_download_url"; }
## Looks for the latest release tag on GitHub and creates a download link
_GIT_GetURL() {
local _i; _i="$(_GIT_getInfo "repos/$AuthorName/$ToolName/releases/latest")"
local _v; _v="$(_GIT_getLatestVersion "$_i")"
local _f; _f=$ToolName\_$_v.zip
_GIT_getDownloadLink "$_i" "$_f"
}
## Greetings
_sayHello() {
local _i; _i="$(date +%H)" # Get the current hour
local _a=", $UserName! \(^-^)/"
if [ "$_i" -ge 5 ] && [ "$_i" -lt 12 ]; then
echo "Good morning$_a"
elif [ "$_i" -ge 12 ] && [ "$_i" -lt 18 ]; then
echo "Good afternoon$_a"
else
echo "Good evening$_a"
fi
}
_sayGoodbye() {
local _i; _i="$(date +%H)" # Get the current hour
local _a=", $UserName! (^-^)"
if [ "$_i" -ge 5 ] && [ "$_i" -lt 12 ]; then
echo "Have a great day$_a"
elif [ "$_i" -ge 12 ] && [ "$_i" -lt 18 ]; then
echo "Enjoy your afternoon$_a"
else
echo "Good night$_a"
fi
}
## Exit Logic
_exit() {
PS3="#?" # Restore the default Select Prompt (PS3)
echo -e "\nExiting...\n$(_sayGoodbye)"
echo "You can safely close this window now."; exit
}
## Downloads latest release from GitHub and installs it.
# $1-Download Link; $2-Instal Directory
_downloadAndUnpack() {
_eFail() { echo -e "\e[1;31mFailure\e[0m"; }
_eSucc() { echo -e "\e[1;96mSuccess\e[0m"; }
local _dl; _dl=$1
local _id; _id=$2
local _ret;
local _fileName; _fileName="update_$(uuidgen | tr "[:lower:]" "[:upper:]").zip"
echo_I "Trying to download the latest version..."
wget -q --show-progress -O "$TempDir/$_fileName" -- "$_dl" &> /dev/null # download the file
_ret="$?"; [ "$_ret" = 0 ] && _eSucc || _eFail
echo_I "Trying to install..."
mkdir -p -- "$_id"
unzip -oq "$TempDir/$_fileName" -d "$_id" # unpack the file
_ret="$?"; [ "$_ret" = 0 ] && _eSucc || _eFail
}
## Install Logic
function installMe() {
_zenityQuestion "Do you want to INSTALL the $ToolName by $AuthorName?" &> /dev/null
echo_I "Creating a temporary folder '$TempDir'..."
mkdir -p -- "$TempDir"
_downloadAndUnpack "$(_GIT_GetURL)" "$InstallDir"
echo_I "Granting execute permission..."
chmod u+x "$CreateShortcutScriptDir"
echo_I "Creating shortcut on Desktop..."
"$CreateShortcutScriptDir" -v
echo_I "Removing the temporary folder '$TempDir'..."
rm -rf -- "$TempDir"
echo_S "The program has been installed."
}
## Uninstall Logic
function uninstallMe() {
_zenityQuestion "Do you want to UNINSTALL the $ToolName by $AuthorName?" &> /dev/null
echo_I "Removing shortcut from Desktop..."
chmod u+x "$CreateShortcutScriptDir"
"$CreateShortcutScriptDir" -rv
echo_I "Uninstalling..."
rm -rf -- "$InstallDir"
echo_S "The program has been uninstalled."
}
### MAIN (ENTRY POINT)
_sayHello
echo ""
echo "$ToolName Installer by $AuthorName"
echo "License: MIT: https://github.com/$AuthorName/$ToolName/blob/main/LICENSE"
echo "Source: https://github.com/$AuthorName/$ToolName"
echo ""
case $UNINSTALL_OPT in
true) uninstallMe;;
*) installMe;;
esac
_exit