#!/bin/bash
CWD="$PWD"
clean=true
#OVERWRITE_TARGET=yes
OVERWRITE_MODE=overwrite #overwrite, keep-original, or move
USE_NON_FULL_VERSIONS=yes
USE_BB_FOR_nonFULL=yes
FULL_UTILS=(tar realpath)
#HARDLINKED_SFS_unextracted="$(realpath ../puppy_upupgg+d_20.10.sfs)"
HARDLINKED_SFS_unextracted="$(realpath ../spup-443.sfs)"
EXTRACTED_SFS="$(basename "$HARDLINKED_SFS_unextracted")".extracted
BUILTIN_FILE_LIST_PATCH="$CWD/patches/builtin_files"
NEW_SFS=2headed_groovyKal.sfs
FR_SOURCE_ARCHIVE="$(realpath ../kalipup_CLI_0.0003.sfs)"
FR_SOURCE_EXTRACTED=$CWD/"$(basename ${FR_SOURCE_ARCHIVE})".extracted
FR_SOURCE_ORIG="${FR_SOURCE_EXTRACTED}"
[ "$clean" = true ] && rm -rf "$EXTRACTED_SFS"
EXTRACTED_SFS_FP="$CWD"/"$EXTRACTED_SFS"
FR_SOURCE="$CWD"/"$EXTRACTED_SFS"/kali/cli
if [ ! -d $EXTRACTED_SFS ] || [ ! "$(ls -A $EXTRACTED_SFS)" ] ; then
#mkdir -p "$EXTRACTED_SFS"
uextract -o . $HARDLINKED_SFS_unextracted || { echo "failed to extract $HARDLINKED_SFS_unextracted" && exit; }
if [[ "$(basename ${HARDLINKED_SFS_unextracted}.extracted)" != "$EXTRACTED_SFS" ]]; then
mv ${HARDLINKED_SFS_unextracted}.extracted $EXTRACTED_SFS
fi
fi
EXTRACTED_SFS="$(realpath "$EXTRACTED_SFS")" || { echo "failed realpath \"$HARDLINKED_SFS\"" && exit; }
TARGET="$EXTRACTED_SFS"/kali; mkdir -p "$TARGET"
#See: https://forum.puppylinux.com/viewtopic.php?f=53&t=1397&p=15016&hilit=hardlinked#p15016
HL_LOC="$EXTRACTED_SFS"; mkdir -p "$HL_LOC"
USE_MULTI_ARCH_DIRS=yes
WARN_MV_ARCH=true
[ ! -d "$TARGET" ] && mkdir -p "$TARGET"
[ ! "$(ls -A "$TARGET")" ] && rsync -rltDv --exclude /kali --link-dest="${HL_LOC}/" "${HL_LOC}/" "${TARGET}/"
MV_LOC="$TARGET/GG-moved"; mkdir -p $MV_LOC
#set +x
#read -p "press enter to continue"
#set -x
if [ ! -d "$FR_SOURCE_ORIG" ]; then
uextract -o . "$FR_SOURCE_ARCHIVE" || { echo "failed to extract $HARDLINKED_SFS_unextracted" && exit; }
fi
if [ ! -d "$FR_SOURCE_ORIG" ]; then
#cp -Ra "$FR_SOURCE_ORIG" "$FR_SOURCE"
mv "$FR_SOURCE_EXTRACTED" "$FR_SOURCE_ORIG"
fi
if [ ! -d "$FR_SOURCE" ]; then
cp -Ra "$FR_SOURCE_ORIG" "$FR_SOURCE"
fi
set -x
cd "$TARGET"
if [ -d ./root/.packages ]; then
#mkdir -p ./var/packages
mv ./root/.packages ./var/packages
cd ./root
ln -s ../var/packages .packages
cd "$CWD"
fi
[ ! -z "$BUILTIN_FILE_LIST_PATCH" ] && cp -arfv --remove-destination "${BUILTIN_FILE_LIST_PATCH}"/* "$TARGET"
CHROOT_DIR="$TARGET" bash -x ./remove_builtin.sh --pkg-list uninstallsfs "$TARGET"
set +x
#FR_SOURCE=/mnt/+mnt+sda5+test_save+ext2/firstRIB_lego/01firstrib_rootfs.sfs.extracted
#Move no longer needed core libs
if [ "$USE_MULTI_ARCH_DIRS" = yes ]; then
while read arch_dir_rel; do
target_arch_dir="$TARGET/$arch_dir_rel"
arch_dir="$(basename "$arch_dir_rel")"
parent_dir_rel="$(dirname "${arch_dir_rel}")"
target_arch_parent="$TARGET/$parent_dir_rel"
parent_of_arch_dir="$TARGET/$parent_dir_rel"
fp_arch_dir="$parrent_dir/arch_dir"
#if [ ! -d "$target_arch_dir" ]; then
#source_dir="$FR_SOURCE/$a_dir"
source_arch_dir="$FR_SOURCE/$arch_dir_rel"
if [ ! -d "$source_arch_dir" ]; then
[ -e "$target_arch_dir" ] && rm "$target_arch_dir" ;
mkdir -p "$target_arch_dir"
else
#the read in this warn section causes the outer loop to be skipped so comment out until fixed.
#if [ "$WARN_MV_ARCH" = true ]; then #Debugging code
#set +x
#echo "about to:"
#echo "
#cd \"$FR_SOURCE\"
#echo ./\"${arch_dir_rel}\" | \
#cpio -pdu \"$TARGET\" )"
#read -p "Press enter to continue"
#set -x
#fi
( cd "$FR_SOURCE"
echo "./${arch_dir_rel}" | \
cpio -pdu "$TARGET" )
fi
#fi
while read b_name; do
#the read in this warn section causes the outer loop to be skipped so comment out until fixed.
#if [ "$WARN_MV_ARCH" = true ]; then #Debugging code
#set +x
#echo "about to:"
#echo "
#mv \"$target_arch_parent/$b_name\" \"$target_arch_dir/$b_name\"
#( cd \"$target_arch_parent\"; ln -s \"$b_name\" \"$arch_dir/$b_name\" ) "
#read -p "Press enter to continue"
#set -x
#fi
rel_path="$(dirname "$arch_dir_rel")/$b_name"
case "$rel_path" in
lib/lsb|lib/systemd|lib/terminfo) continue; ;;
usr/lib/apt|usr/lib/debootstrap|usr/lib/dpkg) continue; ;;
usr/lib/locale|usr/lib/mime|usr/lib/NetworkManager) continue; ;;
usr/lib/pm-utils|usr/lib/ssl|usr/lib/tmpfiles.d) continue; ;;
esac
[[ "$target_arch_parent/$b_name" = "$target_arch_dir" ]] && continue
mv "$target_arch_parent/$b_name" "$target_arch_dir/$b_name"
( cd "$target_arch_parent"; ln -s "$arch_dir/$b_name" "$b_name" )
done < <(find $target_arch_parent -mindepth 1 -maxdepth 1 -type d | xargs -n 1 basename)
done <<EOF
usr/local/lib/x86_64-linux-gnu
lib/x86_64-linux-gnu
usr/lib/x86_64-linux-gnu
EOF
fi
#if [ "$WARN_MV_ARCH" = true ]; then #Debugging code
#set +x
#echo "finished moving files to multi-arch dirs"
#read -p "Press enter to continue"
#set -x
#fi
#mkdir -p "$TARGET"/lib/glib23
#while read line; do
#mv "$TARGET"/lib/$line "$TARGET"/lib/glib23/$line
#done <<EOF
#ld-2.23.so
#ld-linux-x86-64.so.2
#libanl-2.23.so
#libanl.so.1
#libaudit.so.1
#libaudit.so.1.0.0
#libBrokenLocale-2.23.so
#libBrokenLocale.so.1
#libbz2.so.1
#libbz2.so.1.0
#libbz2.so.1.0.4
#libc-2.23.so
#libcrypt-2.23.so
#libcrypt.so.1
#libc.so.6
#libdbus-1.so.3
#libdbus-1.so.3.14.6
#libdl-2.23.so
#libdl.so.2
#libgpg-error.so.0
#libgpg-error.so.0.17.0
#libhistory.so.5
#libhistory.so.6
#libhistory.so.6.3
#liblzma.so.0
#liblzma.so.5
#liblzma.so.5.0.0
#libm-2.23.so
#libmemusage.so
#libm.so.6
#libmvec-2.23.so
#libmvec.so.1
#libnl-3.so
#libnl-3.so.200
#libnl-3.so.200.22.0
#libnl-genl-3.so
#libnl-genl-3.so.200
#libnl-genl-3.so.200.22.0
#libnsl-2.23.so
#libnsl.so.1
#libnss_compat-2.23.so
#libnss_compat.so.2
#libnss_dns-2.23.so
#libnss_dns.so.2
#libnss_files-2.23.so
#libnss_files.so.2
#libnss_hesiod-2.23.so
#libnss_hesiod.so.2
#libnss_nis-2.23.so
#libnss_nisplus-2.23.so
#libnss_nisplus.so.2
#libnss_nis.so.2
#libpamc.so.0
#libpamc.so.0.82.1
#libpam_misc.so.0
#libpam_misc.so.0.82.0
#libpam.so.0
#libpam.so.0.83.1
#libpcprofile.so
#libpthread-2.23.so
#libpthread.so.0
#libreadline.so.5
#libreadline.so.6
#libreadline.so.6.3
#libresolv-2.23.so
#libresolv.so.2
#librt-2.23.so
#librt.so.1
#libSegFault.so
#libselinux.so.1
#libsepol.so.1
#libsystemd.so.0
#libsystemd.so.0.14.0
#libthread_db-1.0.so
#libthread_db.so.1
#libudev.so.0
#libudev.so.1
#libudev.so.1.6.3
#libutil-2.23.so
#libutil.so.1
#libz.so.1
#libz.so.1.2.8
#EOF
if [ 1 -ne 1 ]; then #TODO: We likely need to do something like this.
cd "$TARGET"/lib
rm ld-linux.so.2
ln -s ../lib32/ld-linux.so.2 ld-linux.so.2
fi
#CWD="$PWD"
#HARDLINKED_SFS=puli7.3_x86_64-ungoogled-hardlinked.sfs.extracted
#FR_SOURCE=/mnt/+mnt+sda5+test_save+ext2/firstRIB_lego/01firstrib_rootfs.sfs.extracted
#TARGET="$CWD"/"$HARDLINKED_SFS"/cont
#for folder in bin sbin usr/bin usr/sbin usr/local/bin usr/local/sbin
#for a_file in $(ls -1 $FR_SOURCE/"$folder"); do
#mkdir -p $TARGET/${folder}2
#if [[ "$(readlink "$TARGET/$folder/$a_file")" = */busybox ]]; then
#if [ ! -h "$FR_SOURCE/$folder/$a_file" ]; then
#mv "$TARGET/$folder/$a_file" "$TARGET/${folder}2/$a_file"
#fi
#done
#done
#done
while read a_folder; do
for a_file in $(ls -1A $FR_SOURCE/"$a_folder"); do
#if [[ "$(readlink "$TARGET/$folder/$a_file")" = */busybox ]]; then
# if [ ! -h "$FR_SOURCE/$folder/$a_file" ]; then
source_file="$FR_SOURCE/$a_folder/$a_file"
target_file="$TARGET/$a_folder/$a_file"
if [ -d "$source_file" ] && [ ! -e "$target_file" ]; then
( cd "$(dirname "$source_file")";
echo "$(basename "$source_file")" | \
cpio -pdu "$(dirname "$target_file")" )
else
if [ ! -e "$target_file" ] || [ $(stat -c %s "$source_file") -gt $(stat -c %s "$target_file") ] || ! cmp --silent "$source_file" "$target_file"; then
if [ -d "$source_file" ]; then
if [ ! -e "$target_file" ]; then
( cd "$FR_SOURCE";
echo "./${a_folder}" | \
cpio -pdu "$TARGET" )
#mv "$target_file" "$MV_LOC/${a_folder}/$a_file"
fi
elif [ -e "$source_file" ]; then
if [ -e "$target_file" ] && [ "$OVERWRITE_MODE" != move ]; then
mkdir -p "$MV_LOC/${a_folder}"
mv "$target_file" "$MV_LOC/${a_folder}/$a_file"
fi
if [ ! -d "$TARGET/${a_folder}" ]; then
( cd "$FR_SOURCE";
echo "./${a_folder}" | \
cpio -pdu "$TARGET" )
fi
[ "$OVERWRITE_MODE" = overwrite ] && [ -e "$target_file" ] && rm "$target_file"
ln "$source_file" "$target_file"
fi
fi
# fi
#done
fi
done
done <<EOF
$(cd "$FR_SOURCE"; find ./lib64 -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./opt -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./var -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./opt -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./usr -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./home -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./tmp -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./opt -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./sbin -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./lib -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./bin -type d | sed -r s#^[.]/##)
$(cd "$FR_SOURCE"; find ./etc -mindepth 2 -type d | sed -r s#^[.]/##)
EOF
#We don't need to copy these directories from firstribrootfs
#dev
#dev/pts
#boot
#root
#sys
#proc
#mnt
#media
#run
#etc
echo "" >> "$TARGET/etc/ld.so.conf"
echo "/lib/x86_64-linux-gnu" >> "$TARGET/etc/ld.so.conf"
echo "/usr/local/lib/x86_64-linux-gnu" >> "$TARGET/etc/ld.so.conf"
echo "/usr/lib/x86_64-linux-gnu" >> "$TARGET/etc/ld.so.conf"
echo "include /etc/ld.so.conf.d/*.conf" >> "$TARGET/etc/ld.so.conf"
sed -i '/^$/d' "$TARGET/etc/ld.so.conf"
echo "export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:/usr/local/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH" > "$TARGET/etc/profile"
echo "export $PATH=$PATH:/usr/local/firstrib/bin" >> "$TARGET/etc/profile" #maybe do this in /root/.profile
while read a_file; do
cp -af "$FR_SOURCE/etc/$a_file" "$TARGET/etc/$a_file"
done <<EOF
xattr.conf
environment
deluser.conf
ca-certificates.conf
pam.conf
adduser.conf
debconf.conf
gai.conf
bindresvport.blacklist
EOF
#Don't need from /etc:
#.pwd.lock
#group-
#Duplicate in /etc
#gshadow
#inittab
#passwd
#passwd-
#gshadow-
#inputrc
#ld.so.cache
#libaudit.conf
#nsswitch.conf
#resolv.conf
#shells
#shadow
set -x
mkdir -p "$TARGET"/full-utils
if [ "$USE_NON_FULL_VERSIONS" = yes ]; then
while read a_file_rel_path; do #EXTRACTED_SFS_FP
if [[ $(file "$TARGET/${a_file_rel_path}") != *ASCII* ]]; then
#mv "$TARGET/${a_file_rel_path}-FULL" "$MV_LOC/${a_file_rel_path}-FULL"
#mv "$TARGET/${a_file_rel_path}" "$TARGET/${a_file_rel_path}-FULL"
#mv "$MV_LOC/${a_file_rel_path}" "$TARGET/${a_file_rel_path}"
#mv "$TARGET/${a_file_rel_path}-FULL" "$MV_LOC/${a_file_rel_path}-FULL"
[[ ! -e "$EXTRACTED_SFS_FP/${a_file_rel_path}-FULL" ]] && continue
if [[ $(file "$EXTRACTED_SFS_FP/${a_file_rel_path}") != *ASCII* ]]; then
if [[ $(readlink "$EXTRACTED_SFS_FP/${a_file_rel_path}") = *busybox* ]] || [[ $(realpath "$EXTRACTED_SFS_FP/${a_file_rel_path}") = *busybox* ]]; then
[ ! "$USE_BB_FOR_nonFULL" = yes ] && continue
else
continue #TODO think of a case where we might not want to continue here.
fi
fi
[ -e "$TARGET/${a_file_rel_path}-FULL" ] && rm "$TARGET/${a_file_rel_path}-FULL"
mv "$TARGET/${a_file_rel_path}" "$TARGET/${a_file_rel_path}-FULL"
cp -aP "$EXTRACTED_SFS_FP/${a_file_rel_path}" "$TARGET/${a_file_rel_path}"
util_name="$(basename "$TARGET/${a_file_rel_path}")"
for a_util in "${FULL_UTILS[@]}"; do
if [ "$util_name" = "$a_util" ]; then
( cd "${TARGET}/full-utils"; ln -s ../"${a_file_rel_path}" "$a_util" )
#else
# ( cd "${TARGET}/full-utils"; ln -s ../"${a_file_rel_path}-FULL" "${a_util}-NOTUSED" )
fi
done
[ ! -e "${TARGET}/full-utils/$util_name" ] && \
[ ! -e "${TARGET}/full-utils/${util_name}-NOTUSED" ] && \
( cd "${TARGET}/full-utils"; ln -s ../"${a_file_rel_path}-FULL" "${util_name}-NOTUSED" )
fi
done <<EOF
sbin/losetup
sbin/pivot_root
sbin/sulogin
sbin/switch_root
bin/df
bin/echo
bin/false
bin/more
bin/mount
bin/ps
bin/sleep
bin/su
bin/sync
bin/true
bin/umount
usr/bin/basename
usr/bin/env
usr/bin/expr
usr/bin/[
usr/bin/logger
usr/bin/mesg
usr/bin/realpath
usr/bin/sha1sum
usr/bin/sha256sum
usr/bin/tee
usr/bin/tr
usr/bin/unlink
usr/bin/whoami
usr/bin/who
usr/bin/yes
usr/sbin/chroot
usr/sbin/fdformat
usr/sbin/readprofile
usr/sbin/rtcwake
EOF
fi
if [ 1 -ne 1 ]; then
cd "$TARGET"/lib
rm ld-linux-x86-64.so.2
ln -s ./x86_64-linux-gnu/ld-linux-x86-64.so.2 ld-linux-x86-64.so.2
ln -s ./x86_64-linux-gnu/ld-linux-x86-64.so.2 ld-linux-x86-64.so.1
fi
echo "export PATH=/full-utils:$PATH" > /etc/profile
cd "$CWD"
./sync_file_lists.sh
./sync_dpkg_fm_builtin.sh
mksquashfs "$EXTRACTED_SFS" "$NEW_SFS"