Skip to content

Instantly share code, notes, and snippets.

@maalos
Last active November 29, 2023 13:19
Show Gist options
  • Save maalos/e21bf607c36741f38795a01d9a4840ca to your computer and use it in GitHub Desktop.
Save maalos/e21bf607c36741f38795a01d9a4840ca to your computer and use it in GitHub Desktop.
A Linux From Scratch Package Manager that's just a few kilobytes in size and works quite well, with support for make, cmake, perl and meson.
#!/usr/bin/env bash
echo -e "\nLinux From Scratch Package Manager"
[ -z "$1" ] && echo " _ ______ ______________ ___
| | | ___/ ___| ___ \ \/ |
| | | |_ \ \`--.| |_/ / . . |
| | | _| \`--. \ __/| |\/| |
| |____| | /\__/ / | | | | |
\_____/\_| \____/\_| \_| |_/
" && echo -e "Usage: lfspm <add/del/download/install> <package name>\n" && exit
[ -z "$2" ] && echo "Please specify a package name!" && exit
[ -z "$(command -v wget)" ] && echo "wget not found!" && exit
[ -z "$(command -v curl)" ] && echo "curl not found!" && exit
[ -z "$(command -v tar)" ] && echo "tar not found!" && exit
[ -z "$(command -v make)" ] && echo "make not found!"
[ -z "$(command -v meson)" ] && echo "meson not found!"
[ -z "$(command -v perl)" ] && echo "perl not found!"
[ "$EUID" -ne 0 ] && echo "LFSPM needs to be ran as root!" && exit
TMPDIR=$(pwd)
mkdir -pv /var/
mkdir -pv /var/cache/
mkdir -pv /var/cache/lfspm/
mkdir -pv /var/cache/lfspm/package_sources
mkdir -pv /var/cache/lfspm/installed_files/
cd /var/cache/lfspm/
[ -f lfspm.conf ] || cat > lfspm.conf << "EOF"
MAKE_ARGS="-j`nproc`"
CONF_ARGS="--prefix=/usr"
REPO="https://packages.debian.org/sid/"
EOF
source lfspm.conf
[ -z "$REPO" ] && echo "No repository availible, exiting..." && exit
lfspm_del() {
FILENAME=$(find /var/cache/lfspm/installed_files/ -name files.list | grep "$2")
if [ -z $FILENAME ]; then echo "Unable to find package, exiting..." && exit; fi
read -p "Package $2 found, are you sure you want to delete it? (Y/N) " yn
case $yn in
y|Y|YES|yes )
echo "Deleting package files..." && for file in $(cat $FILENAME); do rm -rf $file; done && echo "Deleting package file directory..." && rm -rf $(dirname $FILENAME);;
* )
echo "Leaving as is.";;
esac
}
lfspm_download() {
echo "Trying $REPO$2"
urls="$(curl -Lks $REPO$2 | grep ".tar.*" | sed -E 's/.*href="([^"]+)".*/\1/')"
SAVEIFS=$IFS
IFS=$'\n'
urls=($urls)
IFS=$SAVEIFS
[ -z "$urls" ] && echo "Package index not found or error loading page!" && exit || echo "Package index found..."
for (( i=0; i<${#urls[@]}; i++ )) do
echo -n "$i: ${urls[$i]}"
[ $(echo ${urls[$i]} | grep debian.tar) ] && echo " (might be incompatible)" || echo
done
echo "Number of the package you want to install (0): "
read -n 1 k <&1
echo
[ -z $k ] && k=0
[ -z ${urls[$k]} ] && echo "Package doesn't exist in URL list, exiting..." && exit
cd package_sources
wget -nc ${urls[$k]} -t0
}
lfspm_install () {
FILENAME=$(find . -name "*$(echo $2 | sed 's/-dev//' | sed 's/lib//')*.tar*" | head -n 1)
[ -z $FILENAME ] && echo "Error, cannot find the archive \"$2\", exiting..." && exit
FILE=$(basename $FILENAME)
[ -z $FILE ] && echo "Error, cannot find the archive \"$2\", exiting..." && exit
DIRNAME=$(tar -tf $FILE | head -n 1)
tar -xf $FILE # || echo "Error, cannot find the archive \"$2*\", trying to find it..." && DIRNAME=$(find . -name "$2*" | grep -v "tar.*" | head -n 1)
mkdir /var/cache/lfspm/installed_files/$DIRNAME
find /usr > /var/cache/lfspm/installed_files/$DIRNAME/usr_before_install
cd $DIRNAME
echo "Looking for any build system..."
[ -f autogen.sh ] && echo "Found, trying to build..." && echo "Running \"./autogen.sh\" inside $(pwd)" && ./autogen.sh
[ -f configure ] && echo "Found, trying to build..." && echo "Running \"./configure $CONF_ARGS\" inside $(pwd)" && ./configure $CONF_ARGS
[ -f Makefile.PL ] && echo "Found, trying to build..." && echo "Running \"perl Makefile.PL\" inside $(pwd)" && perl Makefile.PL
[ -f Makefile ] && echo "Found, trying to build..." && echo "Running \"make $MAKE_ARGS && make $MAKE_ARGS install\" inside $(pwd)" && make $MAKE_ARGS && make $MAKE_ARGS install
[ -f CMakeLists.txt ] && echo "Found, trying to build..." && echo "Running \"mkdir -p build && cd build && cmake ..\" inside $(pwd)" && mkdir -p build && cd build && cmake .. && echo "Running \"make $MAKE_ARGS && make $MAKE_ARGS install\" inside $(pwd)" && make $MAKE_ARGS && make $MAKE_ARGS install
[ -f meson.build ] && echo "Found, trying to build..." && echo "Running \"meson --prefix=/usr build/\" inside $(pwd)" && meson $CONF_ARGS build/ && echo "Running \"ninja -C build/ && ninja -C build/ install\" inside $(pwd)" && ninja -C build/ && ninja -C build/ install
if [ -z /var/cache/lfspm/installed_files/$DIRNAME/files.list ]; then
find /usr > /var/cache/lfspm/installed_files/$DIRNAME/usr_after_install
diff /var/cache/lfspm/installed_files/$DIRNAME/usr_before_install /var/cache/lfspm/installed_files/$DIRNAME/usr_after_install | grep "> " | sed "s/> //" > /var/cache/lfspm/installed_files/$DIRNAME/files.list
rm /var/cache/lfspm/installed_files/$DIRNAME/usr_before_install
rm /var/cache/lfspm/installed_files/$DIRNAME/usr_after_install
fi
}
lfspm_add() {
lfspm_download "$@"
lfspm_install "$@"
}
[ "$1" == "add" ] && lfspm_add "$@"
[ "$1" == "del" ] && lfspm_del "$@"
[ "$1" == "download" ] && lfspm_download "$@"
[ "$1" == "install" ] && lfspm_install "$@"
echo "Done."
cd $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment