repo/puke

242 lines
5.3 KiB
Plaintext
Raw Normal View History

2019-05-09 00:46:50 -06:00
#!/bin/sh
2019-05-10 08:10:54 -06:00
# shellcheck disable=2094,2103,SC2181 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-09 08:34:17 -06:00
rm -rf -- "$mak_dir" "$pkg_dir"
[ -n "$name" ] &&
2019-05-10 00:59:06 -06:00
rm "$old_pwd/repo/$name/.checksums" 2>/dev/null
2019-05-09 00:46:50 -06:00
}
pkg_info() {
[ -z "$1" ] && die "No package specified."
2019-05-09 08:34:17 -06:00
cd "./repo/$1" || die "Package '$1' not in repository."
2019-05-09 00:46:50 -06:00
[ -f version ] || die "Version file not found."
[ -f sources ] || die "Sources file not found."
2019-05-10 09:16:22 -06:00
[ -f build ] || die "Build file not found."
2019-05-09 00:46:50 -06:00
2019-05-09 02:52:34 -06:00
read -r version release < version
2019-05-09 02:30:43 -06:00
name=$1
2019-05-10 09:16:22 -06:00
pkg=$name\#$version-$release
2019-05-09 00:46:50 -06:00
}
pkg_depends() {
2019-05-09 08:41:55 -06:00
[ -f depends ] && while read -r dependency; do
2019-05-10 05:40:19 -06:00
pkg_list "$dependency" || missing="$missing $dependency"
2019-05-09 00:46:50 -06:00
done < 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
src_name=${src##*/}
2019-05-09 00:46:50 -06:00
2019-05-10 00:59:06 -06:00
if [ -f "$src" ] || [ -f "$src_dir/$src_name" ]; then
log "Found local $src_name."
2019-05-09 00:46:50 -06:00
2019-05-09 08:34:17 -06:00
elif [ -z "${src##git:*}" ]; then
git clone "${src##git:}" "$mak_dir"
2019-05-09 06:42:57 -06:00
elif [ -z "${src##*://*}" ]; then
log "Downloading '$src'."
wget -P "$src_dir" "$src" || die "Failed to download $src."
2019-05-09 00:46:50 -06:00
else
2019-05-09 06:42:57 -06:00
die "Source file '$src' not found."
2019-05-09 00:46:50 -06:00
fi
done < sources
}
2019-05-09 06:42:57 -06:00
pkg_checksum() {
while read -r src; do
src_name=${src##*/}
2019-05-09 08:34:17 -06:00
if [ -z "${src##git:*}" ]; then
continue
elif [ -f "$src" ]; then
2019-05-09 06:42:57 -06:00
src_path=$src
elif [ -f "$src_dir/$src_name" ]; then
src_path=$src_dir/$src_name
fi
cd "${src_path%/*}"
sha256sum -- "$src_name" || die "Failed to generate checksums."
cd - >/dev/null
done < sources > "${1-checksums}"
}
pkg_verify() {
pkg_checksum .checksums
diff .checksums checksums ||
die "checksum of sources does not match checksum of package" \
"run '$0 checksum $name' to update checksums"
log "Checksums verified."
2019-05-10 00:59:06 -06:00
rm .checksums
2019-05-09 06:42:57 -06:00
}
2019-05-09 00:46:50 -06:00
pkg_extract() {
2019-05-09 06:42:57 -06:00
while read -r src; do
src_name=${src##*/}
2019-05-09 08:34:17 -06:00
if [ -z "${src##git:*}" ]; then
continue
elif [ -f "$src" ]; then
2019-05-09 06:42:57 -06:00
cp -f "$src" "$mak_dir"
2019-05-09 00:46:50 -06:00
2019-05-09 06:42:57 -06:00
elif [ ! -f "$src_dir/$src_name" ]; then
2019-05-09 08:34:17 -06:00
die "$src_name not found."
2019-05-09 00:46:50 -06:00
2019-05-09 06:42:57 -06:00
else
case $src_dir/$src_name in
*.tar|*.tar.??|*.tar.???|*.tar.????|*.tgz)
tar xf "$src_dir/$src_name" -C "$mak_dir" \
--strip-components 1 ||
die "Couldn't extract $src_name"
;;
esac
2019-05-09 00:46:50 -06:00
fi
done < sources
}
pkg_build() {
2019-05-09 06:42:57 -06:00
log "Building $pkg."
2019-05-09 00:46:50 -06:00
2019-05-09 06:42:57 -06:00
cd "$mak_dir"
2019-05-09 00:46:50 -06:00
set -e
. "$OLDPWD/build"
set +e
2019-05-10 00:59:06 -06:00
cd -
2019-05-09 02:30:43 -06:00
2019-05-10 00:59:06 -06:00
cp -R "$old_pwd/repo/$name" "$old_pwd/pkg/var/db/puke"
2019-05-09 06:42:57 -06:00
log "Sucessfully built $pkg."
2019-05-09 02:30:43 -06:00
}
pkg_manifest() {
cd "$pkg_dir"
_() { find . -mindepth 1 "$@" | sed 's/^\.//'; }
_ -not -type d > "$OLDPWD/manifest"
_ -type d | sort -r >> "$OLDPWD/manifest"
2019-05-09 08:34:17 -06:00
cd - >/dev/null
2019-05-09 00:46:50 -06:00
}
2019-05-10 00:59:06 -06:00
pkg_tar() {
cd "$bin_dir"
tar pcvf "$pkg.tar.gz" -C "$pkg_dir" . ||
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:16:22 -06:00
pkg_remove "$name"
tar pxvf "$bin_dir/$pkg.tar.gz" -k -C "$sys_dir/" ||
2019-05-10 08:10:54 -06:00
die "Failed to install $pkg"
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
[ "${file%/*}" = "/etc" ] && continue
path="${sys_dir}${file}"
if [ -d "$path" ]; then
rmdir "$path" 2>/dev/null
2019-05-10 09:16:22 -06:00
else
2019-05-10 08:10:54 -06:00
rm -- "$path" || log "Failed to remove $file."
fi
[ "$?" = 0 ] && log "Removed $file"
2019-05-10 09:16:22 -06:00
done < "$sys_dir/var/db/puke/$name/manifest"
return 0
2019-05-10 08:10:54 -06:00
}
2019-05-10 05:40:19 -06:00
pkg_list() {
[ "$1" ] && {
[ -d "$sys_dir/var/db/puke/$1" ] || return 1 && return 0
}
for pkg in "$sys_dir/var/db/puke/"*; do
2019-05-10 09:20:02 -06:00
[ -d "$pkg" ] || continue
2019-05-10 05:40:19 -06:00
read -r version release < "$pkg/version"
log "${pkg##*/} $version-$release"
done
}
2019-05-09 00:46:50 -06:00
args() {
2019-05-10 05:40:19 -06:00
[ -n "${1#l*}" ] && {
mkdir -p sources build pkg/var/db/puke sys bin ||
die "Couldn't create directories at '$PWD'".
pkg_info "$2"
}
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
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 03:58:24 -06:00
2019-05-10 08:10:54 -06:00
*) log "$0 [build|checksum|install|list|remove] [pkg]" ;;
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
clean
2019-05-09 06:42:57 -06:00
old_pwd=$PWD
2019-05-10 08:10:54 -06:00
src_dir=$PWD/sources # Contains the downloaded sources.
mak_dir=$PWD/build # Contains the current build.
pkg_dir=$PWD/pkg # Contains the built package's files.
sys_dir=$PWD/sys # Fake root (testing, temporary).
bin_dir=$PWD/bin # Contains the final tarballed packages.
2019-05-09 00:46:50 -06:00
args "$@"
}
main "$@"