2
0
mirror of https://codeberg.org/kiss-community/repo synced 2025-01-03 00:20:09 -07:00
repo/puke

227 lines
5.6 KiB
Plaintext
Raw Normal View History

2019-05-09 00:46:50 -06:00
#!/bin/sh
2019-05-13 07:29:20 -06:00
# shellcheck disable=2094,2103,2181,2188 source=/dev/null
2019-05-09 00:46:50 -06:00
#
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' "$@"
}
clean() {
2019-05-13 06:27:02 -06:00
rm -rf -- "$mak_dir" "$pkg_dir"
2019-05-09 00:46:50 -06:00
}
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 06:58:33 -06:00
read -r version release < version || die "Version file not found."
2019-05-13 01:32:16 -06:00
pkg=${name:=$1}\#$version-$release
2019-05-12 09:05:06 -06:00
2019-05-13 06:56:55 -06:00
mkdir -p "$src_dir" "$mak_dir" "$pkg_dir/$dbs_dir" "$sys_dir" "$bin_dir" ||
die "Couldn't create directories at $PWD."
2019-05-09 00:46:50 -06:00
}
pkg_depends() {
2019-05-13 06:21:39 -06:00
while read -r dependency; do
2019-05-10 05:40:19 -06:00
pkg_list "$dependency" || missing="$missing $dependency"
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-12 07:50:52 -06:00
source_type "$src"
2019-05-09 00:46:50 -06:00
2019-05-12 07:50:52 -06:00
case $? in
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-12 07:50:52 -06:00
source_type "$src"
2019-05-09 06:42:57 -06:00
2019-05-12 07:50:52 -06:00
case $? in
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-13 01:43:49 -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}"
2019-05-13 08:35:34 -06:00
log "Generated checksums."
2019-05-09 06:42:57 -06:00
}
pkg_verify() {
2019-05-13 06:21:39 -06:00
pkg_checksum /dev/stdout | diff checksums - ||
2019-05-13 01:48:58 -06:00
die "Checksums of sources do not match checksums of package." \
"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-12 07:50:52 -06:00
source_type "$src"
case $? in
2) cp -f "$src" "$mak_dir" ;;
2019-05-13 01:48:58 -06:00
4) ;;
2019-05-12 07:50:52 -06:00
3) case $src in
*.tar|*.tar.??|*.tar.???|*.tar.????|*.tgz)
tar xf "$src_dir/${src##*/}" -C "$mak_dir" \
--strip-components 1 ||
die "Couldn't extract ${src##*/}"
esac ;;
*) die "${src##*/} not found."
esac
2019-05-09 00:46:50 -06:00
done < sources
}
pkg_build() {
2019-05-09 06:42:57 -06:00
log "Building $pkg."
2019-05-09 00:46:50 -06:00
(set -e errexit; cd "$mak_dir"; "$OLDPWD/build" "$pkg_dir" ) ||
2019-05-12 09:13:02 -06:00
die "Build failed."
2019-05-09 02:30:43 -06:00
cp -R "$rep_dir/$name" "$pkg_dir/$dbs_dir"
2019-05-12 01:31:18 -06:00
2019-05-13 08:28:07 -06:00
log "Sucessfully built $pkg." 2> "$pkg_dir/$dbs_dir/$name/manifest"
2019-05-09 02:30:43 -06:00
}
2019-05-13 07:42:19 -06:00
pkg_manifest() {
find "$pkg_dir" -mindepth 1 | sed "s|^$pkg_dir||" | sort -r > manifest
cp manifest "$pkg_dir/$dbs_dir/$name"
}
2019-05-09 00:46:50 -06:00
2019-05-10 00:59:06 -06:00
pkg_tar() {
2019-05-13 07:46:19 -06:00
tar pcvf "$bin_dir/$pkg.tar.gz" -C "$pkg_dir" . ||
2019-05-10 00:59:06 -06:00
die "Failed to create package."
log "Package is at $bin_dir/$pkg.tar.gz."
}
2019-05-10 08:10:54 -06:00
pkg_install() {
2019-05-10 09:23:30 -06:00
[ -f "$bin_dir/$pkg.tar.gz" ] || die "Package must be built first."
2019-05-10 09:16:22 -06:00
pkg_remove "$name"
2019-05-10 09:21:19 -06:00
tar pxvf "$bin_dir/$pkg.tar.gz" -k -C "$sys_dir/" 2>/dev/null
[ -f "$sys_dir/$dbs_dir/$name/post-install" ] &&
2019-05-13 08:32:56 -06:00
"$sys_dir/$dbs_dir/$name/post-install"
2019-05-10 08:10:54 -06:00
log "Installed $pkg"
}
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-13 08:08:29 -06:00
if [ "${file%/*}" = "/etc" ]; then
continue
elif [ -d "$sys_dir$file" ]; then
2019-05-13 08:10:33 -06:00
rmdir "$sys_dir$file" 2>/dev/null ||:
2019-05-13 08:08:29 -06:00
else
rm -- "$sys_dir$file" || log "Failed to remove $file."
fi && log "Removed $file"
2019-05-13 06:26:21 -06:00
done < "$sys_dir/$dbs_dir/$name/manifest"
2019-05-10 08:10:54 -06:00
}
2019-05-10 11:21:41 -06:00
pkg_updates() {
2019-05-13 07:49:47 -06:00
for pkg in "$sys_dir/$dbs_dir/"*; do
read -r db_ver db_rel < "$pkg/version"
read -r re_ver re_rel < "$rep_dir/${pkg##*/}/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' "${pkg##*/} $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-13 08:14:34 -06:00
[ "$1" ] && { [ -d "$sys_dir/$dbs_dir/$1" ]; return "$?"; }
2019-05-10 05:40:19 -06:00
for pkg in "$sys_dir/$dbs_dir/"*; do
2019-05-10 05:40:19 -06:00
read -r version release < "$pkg/version"
2019-05-13 01:35:39 -06:00
printf '%s\n' "${pkg##*/} $version-$release"
2019-05-10 05:40:19 -06:00
done
}
2019-05-09 00:46:50 -06:00
args() {
2019-05-13 06:31:38 -06:00
case $1 in b*|c*|d*|i*|r*) pkg_setup "${2-null}"; esac
2019-05-09 00:46:50 -06:00
case $1 in
b*)
2019-05-09 06:42:57 -06:00
[ -f checksums ] || die "Checksums missing, run '$0 checksum $name'"
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-10 00:59:06 -06:00
pkg_tar
2019-05-09 00:46:50 -06:00
;;
2019-05-09 06:42:57 -06:00
c*)
pkg_sources
pkg_checksum
;;
2019-05-10 03:28:03 -06:00
2019-05-10 11:02:47 -06:00
d*) [ -f depends ] && cat depends ;;
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-12 09:05:06 -06:00
*) log "$0 [b|c|d|i|l|r|u] [pkg]" \
"build: Build a package." \
"checksum: Generate checksums." \
"depends: See package dependencies." \
"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-09 06:42:57 -06:00
trap clean EXIT INT
2019-05-09 00:46:50 -06:00
src_dir=$PWD/sources
mak_dir=$PWD/build
pkg_dir=$PWD/pkg
2019-05-13 07:57:08 -06:00
sys_dir=$PUKE_ROOT
bin_dir=$PWD/bin
rep_dir=$PWD/repo
dbs_dir=var/db/puke
2019-05-09 00:46:50 -06:00
args "$@"
}
main "$@"