kiss/kiss

257 lines
6.9 KiB
Plaintext
Raw Normal View History

2019-06-13 08:48:08 -06:00
#!/bin/sh
#
# kiss - package manager for kiss linux.
die() {
2019-06-14 04:38:19 -06:00
printf '\033[31m!>\033[m %s\n' "$@" >&2
2019-06-13 08:48:08 -06:00
exit 1
}
log() {
printf '\033[32m=>\033[m %s\n' "$@"
}
source_type() {
2019-06-14 04:38:19 -06:00
[ -f "$1" ] && return 2 # Local file.
[ -f "$src_dir/${1##*/}" ] && return 3 # Cached downloaded file.
[ -z "${1##git:*}" ] && return 4 # Git repository.
[ -z "${1##*://*}" ] && return 5 # Remote file.
2019-06-13 08:48:08 -06:00
}
2019-06-13 10:40:50 -06:00
pkg_search() {
set -f
# shellcheck disable=2086,2046
set -- "$1" $(IFS=: find $KISS_PATH -maxdepth 1 -name "$1")
set +f
[ -z "$2" ] && die "Package '$1' not in any repository."
rep_dir=${2%/$1}
}
2019-06-13 08:48:08 -06:00
pkg_setup() {
2019-06-13 10:40:50 -06:00
pkg_search "$1"
cd "$rep_dir/$1" || die "'$rep_dir/$1' not accessible"
2019-06-13 08:48:08 -06:00
[ -f sources ] || die "Sources file not found."
[ -x build ] || die "Build file not found or not executable."
read -r ver rel < version || die "Version file not found."
pkg=${name:=$1}\#$ver-$rel.tar.gz
}
pkg_depends() {
2019-06-15 00:19:20 -06:00
while read -r dep opt; do
pkg_list "$dep" || {
2019-06-15 00:22:17 -06:00
[ "$1" = install ] && [ "$opt" = make ] && continue
2019-06-15 00:19:20 -06:00
2019-06-14 00:28:32 -06:00
case $missing in
2019-06-15 00:19:20 -06:00
*" $dep${opt:+ ($opt)},"*) ;;
*) missing="$missing $dep${opt:+ ($opt)},"
2019-06-14 00:28:32 -06:00
pkg_setup "$dep"
pkg_depends ;;
esac
2019-06-15 00:19:20 -06:00
}
2019-06-13 08:48:08 -06:00
done 2>/dev/null < depends
}
pkg_sources() {
while read -r src _; do
case $(source_type "$src"; echo $?) in
4) git clone "${src##git:}" "$mak_dir" ;;
5) wget -P "$src_dir" "$src" || die "Failed to download $src." ;;
0|1) die "Source file '$src' not found." ;;
esac
done < sources
}
pkg_checksum() {
while read -r src _; do
case $(source_type "$src"; echo $?) in
2) src_path=$src ;;
3) src_path=$src_dir/${src##*/} ;;
4) continue
esac
(cd "${src_path%/*}" >/dev/null; sha256sum "${src##*/}") ||
die "Failed to generate checksums."
done < sources > "${1-checksums}"
}
pkg_verify() {
2019-06-16 08:50:39 -06:00
[ "$(pkg_checksum /dev/stdout)" = "$(cat checksums)" ] ||
2019-06-13 08:48:08 -06:00
die "Checksum mismatch, run '$0 checksum $name' to update checksums."
}
pkg_extract() {
while read -r src dest; do
[ "$dest" ] && mkdir -p "$mak_dir/$dest"
case $(source_type "$src"; echo $?)-$src in
2-*) cp -f "$src" "$mak_dir/$dest" ;;
3-*.tar*)
tar xf "$src_dir/${src##*/}" -C "$mak_dir/$dest" \
--strip-components 1 || die "Couldn't extract ${src##*/}" ;;
[01]-*) die "${src##*/} not found."
esac
done < sources
}
pkg_build() {
(cd "$mak_dir"; "$OLDPWD/build" "$pkg_dir") || die "Build failed."
cp -Rf "$rep_dir/$name" "$pkg_db"
log "Sucessfully built $pkg." 2> "$pkg_db/$name/manifest"
}
pkg_strip() {
log "Stripping unneeded symbols from binaries and libraries."
find "$pkg_dir" -type f | while read -r binary; do
case $(file -bi "$binary") in
application/x-sharedlib*|application/x-pie-executable*)
strip_opts=--strip-unneeded
;;
application/x-archive*) strip_opts=--strip-debug ;;
application/x-executable*) strip_opts=--strip-all ;;
*) continue ;;
esac
strip "$strip_opts" "$binary"
done
}
pkg_manifest() {
2019-06-16 08:59:09 -06:00
(cd "$pkg_dir" && find ./*) | sed -e ss.ss -e '1!G;h;$!d' |
2019-06-13 08:48:08 -06:00
tee manifest > "$pkg_db/$name/manifest"
}
pkg_tar() {
tar zpcf "$bin_dir/$pkg" -C "$pkg_dir" . || die "Failed to create package."
log "Use '$0 install $name' to install the package."
}
pkg_install() {
[ -f "$bin_dir/$pkg" ] || args b "$name"
# Create a backup of 'tar' so it isn't removed during
# package installation.
cp "$(command -v tar)" "$cac_dir"
2019-06-13 08:48:08 -06:00
pkg_remove "$name"
"$cac_dir/tar" kpxvf "$bin_dir/$pkg" -C "$sys_dir/"
rm "$cac_dir/tar"
2019-06-13 08:48:08 -06:00
"$sys_db/$name/post-install" 2>/dev/null
log "Installed ${pkg%.tar.gz}"
}
pkg_remove() {
pkg_list "$name" || return 1
# Create a backup of 'rm' and 'rmdir' so they aren't
2019-06-16 10:59:21 -06:00
# removed during package removal.
cp "$(command -v rm)" "$cac_dir"
cp "$(command -v rmdir)" "$cac_dir"
2019-06-13 08:48:08 -06:00
while read -r file; do
2019-06-16 09:22:57 -06:00
[ "${file%/*}" = /etc ] && continue
2019-06-13 08:48:08 -06:00
if [ -d "$sys_dir$file" ]; then
"$cac_dir/rmdir" "$sys_dir$file" 2>/dev/null || continue
2019-06-13 08:48:08 -06:00
else
"$cac_dir/rm" -f -- "$sys_dir$file" || log "Failed to remove $file."
2019-06-13 08:48:08 -06:00
fi && log "Removed $file"
done < "$sys_db/$name/manifest"
# Use the backup of 'rm' to remove 'rmdir' and itself.
"$cac_dir/rm" "$cac_dir/rmdir" "$cac_dir/rm"
2019-06-13 08:48:08 -06:00
}
pkg_updates() {
for item in "$sys_db/"*; do
2019-06-13 23:58:09 -06:00
pkg_search "${item##*/}"
2019-06-13 08:48:08 -06:00
read -r db_ver db_rel < "$item/version"
read -r re_ver re_rel < "$rep_dir/${item##*/}/version"
[ "$db_ver-$db_rel" != "$re_ver-$re_rel" ] &&
printf '%s\n' "${item##*/} $re_ver-$re_rel"
done
}
pkg_list() {
[ "$1" ] && { [ -d "$sys_db/$1" ]; return "$?"; }
for item in "$sys_db/"*; do
read -r version release 2>/dev/null < "$item/version" &&
printf '%s\n' "${item##*/} $version-$release"
done
}
args() {
case $1 in b*|c*|i*|r*) pkg_setup "${2-null}"; esac
case $1 in
b*) [ -f checksums ] || die "Checksums missing, run '$0 checksum $name'"
pkg_depends
2019-06-14 00:28:32 -06:00
[ -n "$missing" ] && die "Missing dependencies:${missing%,}"
2019-06-13 08:48:08 -06:00
pkg_sources
pkg_verify
pkg_extract
pkg_build
2019-06-14 00:28:32 -06:00
2019-06-13 08:48:08 -06:00
[ -f nostrip ] || pkg_strip
2019-06-14 00:28:32 -06:00
2019-06-13 08:48:08 -06:00
pkg_manifest
pkg_tar ;;
c*) pkg_sources
pkg_checksum
log "Generated checksums." ;;
2019-06-15 00:19:20 -06:00
i*) pkg_depends install
pkg_install ;;
2019-06-13 08:48:08 -06:00
l*) pkg_list "$2" ;;
r*) pkg_remove || die "Package '$name' not installed" ;;
u*) pkg_updates ;;
2019-06-13 10:40:50 -06:00
*) log "${0##*/} [b|c|i|l|r|u] [pkg]" \
2019-06-13 08:48:08 -06:00
"build: Build a package." \
"checksum: Generate checksums." \
2019-06-13 11:26:22 -06:00
"install: Install a package (Runs build if needed)." \
2019-06-13 08:48:08 -06:00
"list: List packages." \
"remove: Remove a package." \
"update: Check for updates."
esac
}
main() {
trap 'rm -rf -- "$mak_dir" "$pkg_dir"' EXIT INT
2019-06-14 08:34:02 -06:00
[ -z "$KISS_PATH" ] &&
die "Set \$KISS_PATH to a repository location."
[ -z "$KISS_ROOT" ] && [ "$(id -u)" != 0 ] &&
2019-06-14 08:34:39 -06:00
die "\$KISS_ROOT is set to '/' so you need to be root."
2019-06-13 09:11:59 -06:00
2019-06-13 10:40:50 -06:00
mkdir -p "${cac_dir:=${XDG_CACHE_HOME:=$HOME/.cache}/${0##*/}}" \
"${src_dir:=$cac_dir/sources}" \
"${mak_dir:=$cac_dir/build}" \
"${bin_dir:=$cac_dir/bin}" \
"${pkg_db:=${pkg_dir:=$cac_dir/pkg}/var/db/${0##*/}}" \
"${sys_db:=${sys_dir:=$KISS_ROOT}/var/db/${0##*/}}" ||
die "Couldn't create directories."
2019-06-13 08:48:08 -06:00
args "$@"
}
main "$@"