-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_program_source
75 lines (56 loc) · 1.9 KB
/
get_program_source
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
#!/bin/bash
cd /tmp
# Handle script interruption
trap 'echo "Script interrupted."; exit 1' SIGINT
# Slackware source directories
readonly directories=(
"a" "ap" "d" "e" "f" "k" "kde" "l" "n" "t" "tcl" "x" "xap" "xfce" "y"
)
# URL for source
readonly base_url="https://slackware.osuosl.org/slackware64-current/source"
# Prompt for the program name
read -p "Enter the program name to download: " program
# Temporary file to signal if the program is found
tmpfile=$(mktemp)
# Function to search for the program in a directory
search_directory() {
dir="$1"
# Check if the program has already been found
if [ -s "$tmpfile" ]; then
exit 0
fi
echo "Searching in \"$dir/\"..."
# Check if the directory exists on the remote server and search for the program
if lftp -c "open $base_url/$dir && cls -1" 2>/dev/null | grep -qw "$program"; then
# Write the directory name to the temporary file
echo "$dir" > "$tmpfile"
echo "Found $program in $dir. Downloading..."
# Download the program and check if it succeeded
if lftp -c "open $base_url/$dir && mirror $program"; then
echo "Download completed successfully."
else
echo "Failed to download $program from $dir."
exit 1
fi
fi
}
export -f search_directory
export base_url
export program
export tmpfile
# Determine the number of CPU cores
num_cores=$(nproc)
# Adjust the number of parallel processes
# For I/O-bound tasks, you might use a higher number
parallel_processes=$((num_cores * 2))
# Run searches in parallel using xargs with dynamic -P value
printf "%s\n" "${directories[@]}" | xargs -P"$parallel_processes" -I{} bash -c 'search_directory "$@"' _ {}
# Check if the program was found
if [ -s "$tmpfile" ]; then
found_dir=$(cat "$tmpfile")
echo "Program '$program' found in directory '$found_dir'."
else
echo "Program '$program' not found in any directory."
fi
# Clean up the temporary file
rm -f "$tmpfile"