-
Notifications
You must be signed in to change notification settings - Fork 0
/
mccopy
157 lines (107 loc) · 3.58 KB
/
mccopy
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
#!/bin/sh
set -e
PREREQ=""
prereqs () {
echo "$PREREQ"
}
case $1 in
prereqs)
prereqs
exit 0
;;
esac
echo "DESTDIR=\"${DESTDIR}\""
# Function to recursively find dependencies using ldd
recursive_ldd() {
binary="$1"
visited="$2"
dependencies=""
# Check if the binary has already been visited
case "$visited" in
*"$binary"*) return ;;
esac
# Add binary to visited list
visited="$visited $binary"
# Find dependencies using ldd and awk (first approach)
libs=$(ldd "$binary" 2>/dev/null | awk '/^\s*\// || /^\// {print $1}')
# Find dependencies using ldd and awk (second approach)
libs2=$(ldd "$binary" | awk '/=>/{print $(NF-1)}')
# Merge results, remove empty and whitespace lines, and sort
all_libs=$(printf "%s\n%s\n" "$libs" "$libs2" | grep -v '^[[:space:]]*$' | sort -u)
# Iterate over dependencies
for lib in $all_libs; do
# Skip empty lines
[ -z "$lib" ] && continue
# Add dependency to the list
dependencies="$dependencies $lib"
# Recursively find dependencies of this dependency
dependencies="$dependencies $(recursive_ldd "$lib" "$visited")"
done
# Output the list of dependencies
[ -n "$dependencies" ] && echo "$dependencies" | tr ' ' '\n' | sort -u | grep -v '^[[:space:]]*$'
}
copy_binary_and_dependencies() {
if [ -n "${1}" ]; then
binary="${1}"
else
echo "ERROR: No parameter specified at copy_binary_and_dependencies()."
exit 1
fi
if [ -e "${binary}" ]; then
echo "Copying $binary..."
cp --parents "${binary}" "${DESTDIR}"
# there are some non-zero exit statuses in the recursive_ldd but it works
set +e
dependencies=$(recursive_ldd "$binary" "")
set -e
echo "Copying dependencies of $binary..."
for file in ${dependencies}; do
# Copy the file recursively to the destination directory
echo "Copying '${file}'..."
cp --parents "${file}" "${DESTDIR}"
done
else
echo "The program $binary is not found, we will not include it in the inintrd image."
fi
}
#DESTDIR="/tmp/DESTDIR_TEST" # DEBUG
# Run recursive ldd
binary="/usr/bin/mc"
if [ -e "${binary}" ]; then
echo "We include mc and mcedit..."
copy_binary_and_dependencies "${binary}"
others="
/lib/terminfo/l/linux
/usr/bin/mcedit
/usr/bin/mcdiff
/usr/bin/mcview
/lib/terminfo/l/linux
/usr/share/mc/mc.charsets
/etc/mc/mcedit.menu
/usr/share/mc/skins/default.ini
"
for file in ${others}; do
echo "Copying '${file}'..."
cp -a --parents "${file}" "${DESTDIR}"
done
echo "Copying /usr/lib/mc directory..."
cp -r -a "/usr/lib/mc" "${DESTDIR}/usr/lib/"
echo "Copying /usr/share/mc directory..."
cp -r -a "/usr/share/mc" "${DESTDIR}/usr/share/"
echo "Copying /etc/mc directory..."
cp -r -a "/etc/mc" "${DESTDIR}/etc"
# mcdiff does not work without diff
binary="/usr/bin/diff"
copy_binary_and_dependencies "${binary}"
else
echo "WARNING: $binary is not installed, we will not include it in the inintramfs. "
echo " The program mcedit is useful for editing config files."
fi
binary="/usr/bin/sdmem"
if [ -e "${binary}" ]; then
echo "We include sdmem..."
copy_binary_and_dependencies "${binary}"
else
echo "WARNING: $binary is not installed, we will not include it in the inintramfs. "
echo " We use sdmem to clean the ram after the encrypted keychain is closed."
fi