-
Notifications
You must be signed in to change notification settings - Fork 0
/
depresolver.sh
executable file
·143 lines (130 loc) · 6.21 KB
/
depresolver.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
#!/bin/bash
## This script is used to install the necessary packages for a script to run.
## Author: Rosen Vitanov
## Date: 2024-09-10
Version="0.2"
## Here are stored all the necessary packages for the script to run"
DEPENDENCIES=(
)
## This function checks if the necessary packages are installed.
## It uses the first parameter - as a list of packages to check.
## The function returns 0 if all packages are installed, otherwise it returns the number of missing packages.
## The function also prints the missing packages.
## After it has printed the missing packages, it will ask the user if he wants to install them.
## If the user types "yes" or "y" the function will install the missing packages.
## If the user types "no" or "n" the function will exit with code 1.
## If the user types anything else, the function will ask the user to type "yes" or "no".
## The function will check if the package was installed successfully and will print a message.
function solve_dependencies() {
local missing_packages=0
local missing_packages_list=()
local FLAG_SKIP_PROMPT=false
local FLAG_YES_TO_ALL=false
while getopts "hysi:" opt; do
case $opt in
i)
## Input file or list of packages to check
## Check if the input parameter is a file or a list of packages
## If a file - check if the file exists and read the packages from it
## If a list of packages - read the packages from the input
## If the input is not a file or a list of packages - exit with code 1
if [ -f "$OPTARG" ]; then
while IFS= read -r package; do
DEPENDENCIES+=("$package")
done < "$OPTARG"
elif [ -n "$OPTARG" ]; then
IFS=',' read -ra packages <<< "$OPTARG"
DEPENDENCIES+=("${packages[@]}")
else
echo "Invalid input parameter: $OPTARG" >&2
return 1
fi
;;
y)
## Automatically installs missing packages without asking for user intervention, but shows the missing packages.
FLAG_YES_TO_ALL=true
;;
s)
## Silent automated mode - installs missing packages without asking for any user intervention (exsept for sudo password)
## This automatically sets the -y flag
FLAG_SKIP_PROMPT=true
FLAG_YES_TO_ALL=true
;;
h)
echo "depresolver.sh - Dependency resolver script"
echo "Version: $Version"
echo "Usage: $0 [-i <file|packages>] [-y] [-s]"
echo "Options:"
echo " -i <file|packages> Input file or list of packages to install"
echo " -y Automatically installs missing packages without asking for user intervention, but will show the missing packages"
echo " -s Silent automated mode - installs missing packages without asking for any user intervention or giving any ourput"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
esac
done
[ "$FLAG_SKIP_PROMPT" = false ] && echo "Checking dependencies for $0"
for package in "${DEPENDENCIES[@]}"; do
[ "$FLAG_SKIP_PROMPT" = false ] && echo -n " - Checking $package... "
if ! (dpkg -l | grep -w "$package") > /dev/null; then
missing_packages=$((missing_packages+1))
missing_packages_list+=($package)
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[31m Missing \e[0m"
else
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[32m OK \e[0m"
fi
done
[ "$FLAG_SKIP_PROMPT" = false ] && echo ""
if [ $missing_packages -eq 0 ]; then
[ "$FLAG_SKIP_PROMPT" = false ] && echo "All dependencies are satisfied."
return 0
else
[ "$FLAG_SKIP_PROMPT" = false ] && echo "The following dependencies are missing:"
for package in "${missing_packages_list[@]}"; do
[ "$FLAG_SKIP_PROMPT" = false ] && echo $package
done
if [ "$skip_prompt" = false ] || [ "$install_all" = true ]; then
for package in "${missing_packages_list[@]}"; do
[ "$FLAG_SKIP_PROMPT" = false ] && echo -n " - Installing $package... "
sudo apt-get install -y $package >> /dev/null
if (dpkg -l | grep -w "$package") > /dev/null; then
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[32m OK \e[0m"
else
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[31m Failed \e[0m"
fi
done
return 0
else
[ "$FLAG_SKIP_PROMPT" = false ] && echo ""
while true; do
if [ "$FLAG_YES_TO_ALL" = true ]; then
answer="yes"
else
read -p "Do you want to install the missing dependencies? (yes/no): " answer
fi
case $answer in
[Yy]* )
for package in "${missing_packages_list[@]}"; do
[ "$FLAG_SKIP_PROMPT" = false ] && echo -n " - Installing $package... "
sudo apt-get install -y $package >> /dev/null
if (dpkg -l | grep -w "$package") > /dev/null; then
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[32m OK \e[0m"
else
[ "$FLAG_SKIP_PROMPT" = false ] && echo -e "\e[31m Failed \e[0m"
fi
done
return 0
;;
[Nn]* )
return 1
;;
* )
[ "$FLAG_SKIP_PROMPT" = false ] && echo "Please answer yes or no."
;;
esac
done
fi
fi
}