@@ -56,7 +56,7 @@ if ! type zip > /dev/null 2>&1; then
5656 exit 1
5757fi
5858function package_libc_via_pacman {
59- if [[ $( cat /etc/os-release | sed ' /ID_LIKE/!d;s/ID_LIKE=//' ) == " archlinux" ]]; then
59+ if [[ $( sed ' /ID_LIKE/!d;s/ID_LIKE=//' < /etc/os-release ) == " archlinux" ]]; then
6060 if type pacman > /dev/null 2>&1 ; then
6161 pacman --files --list --quiet glibc | sed -E ' /\.so$|\.so\.[0-9]+$/!d'
6262 fi
@@ -65,29 +65,42 @@ function package_libc_via_pacman {
6565
6666function package_libc_via_dpkg() {
6767 if type dpkg-query > /dev/null 2>&1 ; then
68- if [ $( dpkg-query --listfiles libc6 | wc -l) -gt 0 ]; then
68+ if [[ $( dpkg-query --listfiles libc6 | wc -l) -gt 0 ] ]; then
6969 dpkg-query --listfiles libc6 | sed -E ' /\.so$|\.so\.[0-9]+$/!d'
7070 fi
7171 fi
7272}
7373
7474function package_libc_via_rpm() {
7575 if type rpm > /dev/null 2>&1 ; then
76- if [ $( rpm --query --list glibc | wc -l) -gt 1 ]; then
76+ if [[ $( rpm --query --list glibc | wc -l) -gt 1 ] ]; then
7777 rpm --query --list glibc | sed -E ' /\.so$|\.so\.[0-9]+$/!d'
7878 fi
7979 fi
8080}
8181
82- PKG_BIN_FILENAME=` basename " $PKG_BIN_PATH " `
82+ # hasElement expects an element and an array parameter
83+ # it's equivalent to array.contains(element)
84+ # e.g. hasElement "needle" ${haystack[@]}
85+ function hasElement() {
86+ local el key=$1
87+ shift
88+ for el in " $@ " :
89+ do
90+ [[ " $el " == " $key " ]] && return 0
91+ done
92+ return 1
93+ }
94+
95+ PKG_BIN_FILENAME=$( basename " $PKG_BIN_PATH " )
8396PKG_DIR=tmp
8497PKG_LD=" "
8598
8699list=$( ldd " $PKG_BIN_PATH " | awk ' {print $(NF-1)}' )
87100libc_libs=()
88- libc_libs+=$( package_libc_via_dpkg)
89- libc_libs+=$( package_libc_via_rpm)
90- libc_libs+=$( package_libc_via_pacman)
101+ libc_libs+=( $( package_libc_via_dpkg) )
102+ libc_libs+=( $( package_libc_via_rpm) )
103+ libc_libs+=( $( package_libc_via_pacman) )
91104
92105mkdir -p " $PKG_DIR /bin" " $PKG_DIR /lib"
93106
98111 fi
99112
100113 # Do not copy libc files which are directly linked unless it's the dynamic loader
101- matched=$( echo $libc_libs | grep --fixed-strings --count $i ) || true # prevent the non-zero exit status from terminating the script
102- if [ $matched -gt 0 ]; then
103- filename=` basename $i `
114+ if hasElement " $i " " ${libc_libs[@]} " ; then
115+ filename=$( basename " $i " )
104116 if [[ -z " ${filename## ld-* } " ]]; then
105117 PKG_LD=$filename # Use this file as the loader
106- cp $i $PKG_DIR /lib
118+ cp " $i " $PKG_DIR /lib
107119 fi
108120 continue
109121 fi
110122
111- cp $i $PKG_DIR /lib
123+ cp " $i " $PKG_DIR /lib
112124done
113125
114126if [[ $INCLUDE_LIBC == true ]]; then
115- for i in $ libc_libs
127+ for i in " ${ libc_libs[@]} "
116128 do
117- filename=` basename $i `
129+ filename=$( basename " $i " )
118130 if [[ -z " ${filename## ld-* } " ]]; then
119131 continue # We don't want the dynamic loader's symlink because its target is an absolute path (/lib/ld-*).
120132 fi
121- cp --no-dereference $i $PKG_DIR /lib
133+ cp --no-dereference " $i " " $PKG_DIR /lib"
122134 done
123135fi
124136
148160chmod +x " $PKG_DIR /bootstrap"
149161# some shenanigans to create the right layout in the zip file without extraneous directories
150162pushd " $PKG_DIR " > /dev/null
151- zip --symlinks --recurse-paths $PKG_BIN_FILENAME .zip *
163+ zip --symlinks --recurse-paths " $PKG_BIN_FILENAME " .zip -- *
152164ORIGIN_DIR=$( dirs -l +1)
153- mv $PKG_BIN_FILENAME .zip $ORIGIN_DIR
165+ mv " $PKG_BIN_FILENAME " .zip " $ORIGIN_DIR "
154166popd > /dev/null
155167rm -r " $PKG_DIR "
156168echo Created " $ORIGIN_DIR /$PKG_BIN_FILENAME " .zip
0 commit comments