-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapt
executable file
·79 lines (73 loc) · 3.05 KB
/
apt
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
#!/usr/bin/env bash
while [[ "$PROJECT_DIR" != */MobilePassThrough ]]; do PROJECT_DIR="$(readlink -f "$(dirname "${PROJECT_DIR:-0}")")"; done
source "$PROJECT_DIR/scripts/utils/common/libs/helpers"
loadConfig
#####################################################################################################
# Simple script to install the packages containing the given executable names or file names
# Usage: `./apt --executables "curl git wget docker"`
# Usage: `./apt --files "/usr/include/{ree,ruby}/ruby.h /usr/share/virtio-win/virtio-win.iso"`
#####################################################################################################
alias getMissingExecutables="${COMMON_UTILS_TOOLS_DIR}/get-missing-executables"
alias getMissingFiles="${COMMON_UTILS_TOOLS_DIR}/get-missing-files"
COMMAND="$1"
SEARCH_MODE="$2"
WANTED_ITEMS="$3"
function updatePackageInfo() {
sudo apt-get update
if ! command -v apt-file &> /dev/null; then
sudo apt-get install -y apt-file
fi
sudo apt-file update
}
function installPackages() {
SEARCH_MODE="$1"
WANTED_ITEMS="$2"
if ! command -v apt-file &> /dev/null; then
sudo apt-get install -y apt-file
sudo apt-file update
fi
PACKAGES_TO_INSTALL=""
for CURRENT_ITEM in $WANTED_ITEMS; do
if [ "$SEARCH_MODE" = "--executables" ]; then
PACKAGE_TO_INSTALL="$(sudo apt-file search -l -x "^/(bin|sbin|usr/bin|usr/sbin)/${CURRENT_ITEM}$" | head -1)"
elif [ "$SEARCH_MODE" = "--files" ]; then
PACKAGE_TO_INSTALL="$(sudo apt-file search $CURRENT_ITEM | grep "${CURRENT_ITEM}$" | head -1 | cut -d':' -f1)"
fi
if [ "$?" = "0" ] && [ "$PACKAGE_TO_INSTALL" != "" ]; then
PACKAGES_TO_INSTALL+=" $PACKAGE_TO_INSTALL"
else
STILL_MISSING_ITEMS+=" $CURRENT_ITEM"
fi
done
if [ "$PACKAGES_TO_INSTALL" != "" ]; then
sudo bash -c "DEBIAN_FRONTEND=noninteractive apt-get install -yq $PACKAGES_TO_INSTALL"
fi
}
if [ "$COMMAND" = "install" ]; then
if [ "$SEARCH_MODE" = "--executables" ]; then
MISSING_EXECUTABLES="$(getMissingExecutables "$WANTED_ITEMS")"
installPackages "$SEARCH_MODE" "$MISSING_EXECUTABLES"
MISSING_EXECUTABLES="$(getMissingExecutables "$WANTED_ITEMS")"
elif [ "$SEARCH_MODE" = "--files" ]; then
MISSING_FILES="$(getMissingFiles "$WANTED_ITEMS")"
installPackages "$SEARCH_MODE" "$MISSING_FILES"
MISSING_FILES="$(getMissingFiles "$WANTED_ITEMS")"
else
echo "ERROR: Invalid mode or no mode at all was speficied!"
exit 1
fi
if [ "$MISSING_EXECUTABLES" != "" ]; then
echo "ERROR: Still missing package providing the following executables: $MISSING_EXECUTABLES"
fi
if [ "$MISSING_FILES" != "" ]; then
echo "ERROR: Still missing package providing the following files: $MISSING_FILES"
fi
if [ "$MISSING_EXECUTABLES" != "" ] && [ "$MISSING_FILES" != "" ]; then
exit 1
fi
elif [ "$COMMAND" = "update" ]; then
updatePackageInfo
else
echo "ERROR: Invalid command"
exit 1
fi