-
Notifications
You must be signed in to change notification settings - Fork 87
/
install_optional.sh
119 lines (100 loc) · 2.92 KB
/
install_optional.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
#!/bin/bash
# Function to download the selected package
download_package() {
local url="$1"
local package_name="$2"
wget "$url" -O "./optional/$package_name"
}
unpack() {
cd optional && $1 && cd ..
}
# Function to check and install jq if it's missing
check_and_install_jq() {
if ! command -v jq &>/dev/null; then
echo "jq is not installed. Installing jq..."
# Check the package manager and install jq accordingly
if command -v apt-get &>/dev/null; then
sudo apt-get update
sudo apt-get install -y jq
elif command -v yum &>/dev/null; then
sudo yum install -y jq
else
echo "Unsupported package manager. Please install jq manually (https://stedolan.github.io/jq/download/)."
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "Failed to install jq. Please install jq manually (https://stedolan.github.io/jq/download/)."
exit 1
fi
echo "jq has been installed successfully!"
fi
}
# Read packages from the JSON file
read_packages() {
local json_file="$1"
jq -r '.[] | "\(.title)\n\(.description)\n\(.package_url)"' "$json_file"
}
# Main script starts here
json_file="repository/optional.json"
# Check and install jq if it's missing
check_and_install_jq
# Create the "optional" directory if it doesn't exist
mkdir -p "./optional"
# Read packages and generate the menu using jq
menu=$(jq -r '.[] | "\(.title)\n\(.description)\n\(.package_url)\n\(.package)\n\(.unpack)\n\(.install)"' "$json_file")
# Create an array of menu options
IFS=$'\n' read -d '' -ra menu_options <<< "$menu"
t=0
while true; do
# Clear the screen and display the menu
clear
echo -e "\033[1m\033[4mAvailable Packages:\033[0m"
echo ""
for (( i = 0; i < ${#menu_options[@]}; i+=6 )); do
title="${menu_options[i]}"
description="${menu_options[i+1]}"
installed=""
if [ -f "optional/${menu_options[i+3]}" ]; then
installed=" (Installed)"
fi
echo -e "\e[1m$((i/6 + 1))) $title$installed\e[0m"
echo " $description"
done
# Prompt user for selection
echo ""
read -p "Enter the number to download the corresponding package (type 'quit' to exit): " choice
# Check if the user wants to quit
if [[ "$choice" == "quit" ]]; then
echo ""
echo "Goodbye!"
echo ""
break
fi
# Validate user input
if [[ "$choice" =~ ^[0-9]+$ ]]; then
for (( i = 0; i < ${#menu_options[@]}; i+=6 )); do
t=$((i/6+1))
if [[ "$t" == "$choice" ]]; then
selected_package="${menu_options[i+2]}"
selected_title="${menu_options[i+3]}"
selected_unpack="${menu_options[i+4]}"
selected_install="${menu_options[i+5]}"
break;
fi
done
if [[ -z "$selected_package" ]]; then
echo "Invalid selection. Please try again."
sleep 2
continue
fi
# Download the selected package
download_package "$selected_package" "$selected_title"
echo "Package '$selected_title' downloaded to ./optional/"
unpack "$selected_unpack"
rsync -ravl $selected_install
exit
else
echo "Invalid input. Please try again."
sleep 2
fi
done