2
0
mirror of https://codeberg.org/kiss-community/repo synced 2024-12-22 07:10:16 -07:00
repo/puke

193 lines
5.0 KiB
Plaintext
Raw Normal View History

2019-05-09 00:46:50 -06:00
#!/bin/sh
#
2019-05-10 05:41:55 -06:00
# puke - package manager for kiss linux.
2019-05-09 00:46:50 -06:00
die() {
printf '\033[31mERROR>\033[m %s\n' "$@" >&2
exit 1
}
log() {
printf '\033[32m=>\033[m %s\n' "$@"
}
2019-05-13 02:42:11 -06:00
source_type() {
[ -f "$1" ] && return 2
[ -f "$src_dir/${1##*/}" ] && return 3
[ -z "${1##git:*}" ] && return 4
[ -z "${1##*://*}" ] && return 5
}
2019-05-12 09:05:06 -06:00
pkg_setup() {
2019-05-12 01:31:18 -06:00
cd "$rep_dir/$1" || die "Package '$1' not in repository."
[ -f sources ] || die "Sources file not found."
[ -x build ] || die "Build file not found or not executable."
2019-05-09 00:46:50 -06:00
2019-05-13 10:00:10 -06:00
read -r ver rel < version || die "Version file not found."
2019-05-15 02:43:23 -06:00
pkg=${name:=$1}\#$ver-$rel.tar.gz
2019-05-09 00:46:50 -06:00
}
pkg_depends() {
2019-05-13 09:34:28 -06:00
while read -r dep; do
pkg_list "$dep" || missing="$missing $dep"
2019-05-13 06:21:39 -06:00
done 2>/dev/null < depends
2019-05-10 05:40:19 -06:00
[ -n "$missing" ] && die "Missing dependencies:$missing"
2019-05-09 00:46:50 -06:00
}
pkg_sources() {
2019-05-09 06:42:57 -06:00
while read -r src; do
2019-05-16 01:35:11 -06:00
case $(source_type "$src"; echo $?) in
2019-05-12 07:50:52 -06:00
2|3) log "Found local ${src##*/}." ;;
4) git clone "${src##git:}" "$mak_dir" ;;
2019-05-09 08:34:17 -06:00
2019-05-12 07:50:52 -06:00
5) log "Downloading '$src'."
wget -P "$src_dir" "$src" || die "Failed to download $src." ;;
2019-05-09 00:46:50 -06:00
2019-05-12 07:50:52 -06:00
*) die "Source file '$src' not found." ;;
esac
2019-05-09 00:46:50 -06:00
done < sources
}
2019-05-09 06:42:57 -06:00
pkg_checksum() {
while read -r src; do
2019-05-16 01:35:11 -06:00
case $(source_type "$src"; echo $?) in
2019-05-12 07:50:52 -06:00
2) src_path=$src ;;
3) src_path=$src_dir/${src##*/} ;;
2019-05-13 06:55:11 -06:00
4) continue
2019-05-12 07:50:52 -06:00
esac
2019-05-09 06:42:57 -06:00
2019-05-14 14:09:18 -06:00
(cd "${src_path%/*}" >/dev/null; sha256sum "${src##*/}") ||
die "Failed to generate checksums."
2019-05-09 06:42:57 -06:00
done < sources > "${1-checksums}"
}
pkg_verify() {
2019-05-13 06:21:39 -06:00
pkg_checksum /dev/stdout | diff checksums - ||
2019-05-16 07:48:06 -06:00
die "Checksum mismatch, run '$0 checksum $name' to update checksums."
2019-05-09 06:42:57 -06:00
log "Checksums verified."
}
2019-05-09 00:46:50 -06:00
pkg_extract() {
2019-05-09 06:42:57 -06:00
while read -r src; do
2019-05-16 01:35:11 -06:00
case $(source_type "$src"; echo $?)-$src in
2019-05-14 13:49:42 -06:00
2-*) cp -f "$src" "$mak_dir" ;;
3-*.tar*)
tar xf "$src_dir/${src##*/}" -C "$mak_dir" --strip-components 1 ||
die "Couldn't extract ${src##*/}" ;;
2019-05-12 07:50:52 -06:00
2019-05-14 13:59:25 -06:00
[01]-*) die "${src##*/} not found."
2019-05-12 07:50:52 -06:00
esac
2019-05-09 00:46:50 -06:00
done < sources
}
pkg_build() {
2019-05-14 14:05:50 -06:00
(cd "$mak_dir"; "$OLDPWD/build" "$pkg_dir") || die "Build failed."
2019-05-16 07:45:08 -06:00
cp -Rf "$rep_dir/$name" "$pkg_db"
2019-05-15 02:43:23 -06:00
log "Sucessfully built $pkg." 2> "$pkg_db/$name/manifest"
2019-05-09 02:30:43 -06:00
}
2019-05-16 07:45:08 -06:00
pkg_manifest() {
(cd "$pkg_dir" && find ./*) | sed ss.ss | tac |
2019-05-15 02:43:23 -06:00
tee manifest > "$pkg_db/$name/manifest"
2019-05-16 07:45:08 -06:00
}
2019-05-09 00:46:50 -06:00
2019-05-10 00:59:06 -06:00
pkg_tar() {
2019-05-15 02:43:23 -06:00
tar pcvf "$bin_dir/$pkg" -C "$pkg_dir" . || die "Failed to create package."
log "Package is at $bin_dir/$pkg."
2019-05-10 00:59:06 -06:00
}
2019-05-10 08:10:54 -06:00
pkg_install() {
2019-05-15 02:43:23 -06:00
[ -f "$bin_dir/$pkg" ] || die "Package must be built first."
2019-05-10 09:23:30 -06:00
2019-05-10 09:16:22 -06:00
pkg_remove "$name"
2019-05-15 02:43:23 -06:00
tar pxvf "$bin_dir/$pkg" -k -C "$sys_dir/" 2>/dev/null
2019-05-15 02:43:23 -06:00
"$sys_db/$name/post-install" 2>/dev/null
2019-05-15 02:43:23 -06:00
log "Installed ${pkg%.tar.gz}"
2019-05-10 08:10:54 -06:00
}
pkg_remove() {
2019-05-10 09:16:22 -06:00
pkg_list "$name" || return 1
2019-05-10 08:10:54 -06:00
while read -r file; do
2019-05-14 13:31:44 -06:00
[ "${file%/*}" = "/etc" ] && continue
if [ -d "$sys_dir$file" ]; then
2019-05-14 09:58:03 -06:00
rmdir "$sys_dir$file" 2>/dev/null || continue
2019-05-13 08:08:29 -06:00
else
rm -- "$sys_dir$file" || log "Failed to remove $file."
2019-05-14 09:58:03 -06:00
fi && log "Removed $file"
2019-05-15 02:43:23 -06:00
done < "$sys_db/$name/manifest"
2019-05-10 08:10:54 -06:00
}
2019-05-10 11:21:41 -06:00
pkg_updates() {
2019-05-15 02:43:23 -06:00
for item in "$sys_db/"*; do
read -r db_ver db_rel < "$item/version"
read -r re_ver re_rel < "$rep_dir/${item##*/}/version"
2019-05-10 11:21:41 -06:00
2019-05-13 07:49:47 -06:00
[ "$db_ver-$db_rel" != "$re_ver-$re_rel" ] &&
printf '%s\n' "${item##*/} $re_ver-$re_rel"
2019-05-10 11:21:41 -06:00
done
}
2019-05-10 05:40:19 -06:00
pkg_list() {
2019-05-15 02:43:23 -06:00
[ "$1" ] && { [ -d "$sys_db/$1" ]; return "$?"; }
2019-05-10 05:40:19 -06:00
2019-05-15 02:43:23 -06:00
for item in "$sys_db/"*; do
2019-05-14 14:27:19 -06:00
read -r version release 2>/dev/null < "$item/version" &&
printf '%s\n' "${item##*/} $version-$release"
2019-05-10 05:40:19 -06:00
done
}
2019-05-09 00:46:50 -06:00
args() {
2019-05-16 01:04:34 -06:00
case $1 in b*|c*|i*|r*) pkg_setup "${2-null}"; esac
2019-05-09 00:46:50 -06:00
case $1 in
2019-05-14 07:10:12 -06:00
b*) [ -f checksums ] || die "Checksums missing, run '$0 checksum $name'"
2019-05-09 06:42:57 -06:00
2019-05-09 00:46:50 -06:00
pkg_depends
pkg_sources
2019-05-09 06:42:57 -06:00
pkg_verify
2019-05-09 00:46:50 -06:00
pkg_extract
2019-05-09 02:30:43 -06:00
pkg_build
pkg_manifest
2019-05-14 07:10:12 -06:00
pkg_tar ;;
2019-05-09 06:42:57 -06:00
2019-05-14 07:10:12 -06:00
c*) pkg_sources
2019-05-09 06:42:57 -06:00
pkg_checksum
2019-05-14 07:10:12 -06:00
log "Generated checksums." ;;
2019-05-10 03:28:03 -06:00
2019-05-10 08:10:54 -06:00
i*) pkg_install ;;
l*) pkg_list "$2" ;;
2019-05-10 09:16:22 -06:00
r*) pkg_remove || die "Package '$name' not installed" ;;
2019-05-10 11:21:41 -06:00
u*) pkg_updates ;;
2019-05-10 03:58:24 -06:00
2019-05-14 14:34:51 -06:00
*) log "$0 [b|c|i|l|r|u] [pkg]" \
2019-05-12 09:05:06 -06:00
"build: Build a package." \
"checksum: Generate checksums." \
"install: Install a package." \
"list: List packages." \
"remove: Remove a package." \
"update: Check for updates."
2019-05-09 00:46:50 -06:00
esac
}
main() {
2019-05-14 14:39:28 -06:00
trap 'rm -rf -- "$mak_dir" "$pkg_dir"' EXIT INT
2019-05-09 00:46:50 -06:00
2019-05-16 01:25:08 -06:00
mkdir -p "${src_dir:=$PWD/sources}" \
"${mak_dir:=$PWD/build}" \
"${pkg_db:=${pkg_dir:=$PWD/pkg}/var/db/puke}" \
"${sys_db:=${sys_dir:=$PUKE_ROOT}/var/db/puke}" \
"${bin_dir:=$PWD/bin}" \
"${rep_dir:=$PWD/repo}" ||
die "Couldn't create directories at $PWD."
2019-05-09 00:46:50 -06:00
args "$@"
}
main "$@"