Skip to content

Commit

Permalink
Merge mkinitcpio libalpm scripts into one
Browse files Browse the repository at this point in the history
Fixes #10

Co-authored-by: nl6720 <nl6720@gmail.com>
Signed-off-by: Hugo Osvaldo Barrera <hugo@barrera.io>
  • Loading branch information
WhyNotHugo and nl6720 committed Oct 30, 2022
1 parent 4a2367a commit aac3876
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 109 deletions.
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -73,9 +73,8 @@ install: all
install -m644 shell/zsh-completion $(DESTDIR)/usr/share/zsh/site-functions/_mkinitcpio

install -m644 libalpm/hooks/90-mkinitcpio-install.hook $(DESTDIR)/usr/share/libalpm/hooks/90-mkinitcpio-install.hook
install -m755 libalpm/scripts/mkinitcpio-install $(DESTDIR)/usr/share/libalpm/scripts/mkinitcpio-install
install -m644 libalpm/hooks/60-mkinitcpio-remove.hook $(DESTDIR)/usr/share/libalpm/hooks/60-mkinitcpio-remove.hook
install -m755 libalpm/scripts/mkinitcpio-remove $(DESTDIR)/usr/share/libalpm/scripts/mkinitcpio-remove
install -m755 libalpm/scripts/mkinitcpio $(DESTDIR)/usr/share/libalpm/scripts/mkinitcpio

doc: $(MANPAGES)
man/%: man/%.txt Makefile
Expand Down
2 changes: 1 addition & 1 deletion libalpm/hooks/60-mkinitcpio-remove.hook
Expand Up @@ -12,5 +12,5 @@ Target = mkinitcpio-git
[Action]
Description = Removing linux initcpios...
When = PreTransaction
Exec = /usr/share/libalpm/scripts/mkinitcpio-remove
Exec = /usr/share/libalpm/scripts/mkinitcpio remove
NeedsTargets
2 changes: 1 addition & 1 deletion libalpm/hooks/90-mkinitcpio-install.hook
Expand Up @@ -8,5 +8,5 @@ Target = usr/lib/initcpio/*
[Action]
Description = Updating linux initcpios...
When = PostTransaction
Exec = /usr/share/libalpm/scripts/mkinitcpio-install
Exec = /usr/share/libalpm/scripts/mkinitcpio install
NeedsTargets
95 changes: 95 additions & 0 deletions libalpm/scripts/mkinitcpio
@@ -0,0 +1,95 @@
#!/usr/bin/env bash

set -e

args=()
package=0

process_preset() {
if [[ -n "$pkgbase" && -e "$preset" ]]; then
if ! cmp "$preset" > /dev/null 2>&1 <(sed "s|%PKGBASE%|${pkgbase}|g" /usr/share/mkinitcpio/hook.preset); then
if [[ ! -e "$preset.pacsave" ]]; then
# save the preset as pacsave
mv -- "$preset" "$preset.pacsave" && return 0
fi
else
# remove the preset
rm -- "$preset" && return 0
fi
fi
}

install_kernel() {
preset="/etc/mkinitcpio.d/${1}.preset"
if [[ ! -e "$preset" ]]; then
if [[ -e "$preset.pacsave" ]]; then
# move the pacsave to the template
mv -- "${preset}.pacsave" "$preset"
else
# create the preset from the template
sed "s|%PKGBASE%|${1}|g" /usr/share/mkinitcpio/hook.preset \
| install -Dm644 /dev/stdin "$preset"
fi
fi

# always install the kernel
install -Dm644 "$line" "/boot/vmlinuz-${1}"

# compound args for each kernel
args+=(-p "$1")
}

remove_kernel() {
# remove the actual kernel and images for the package being removed
kernel="/boot/vmlinuz-${1}"
preset="/etc/mkinitcpio.d/${1}.preset"
initramfs="/boot/initramfs-${1}.img"
fallback_initramfs="/boot/initramfs-${1}-fallback.img"
# remove the installed kernel
rm -f -- "$kernel"

process_preset "$1" "$preset"

# remove images
rm -f -- "$initramfs" "$fallback_initramfs"
}

while read -r line; do
if [[ "$line" != */vmlinuz ]]; then
# triggers when it's a change to usr/lib/initcpio/*
package=1
continue
fi

if ! read -r pkgbase > /dev/null 2>&1 < "${line%/vmlinuz}/pkgbase"; then
# if the kernel has no pkgbase, we skip it
continue
fi

case "$1" in
install) install_kernel "$pkgbase";;
remove) remove_kernel "$pkgbase";;
esac
done

if (( package )) && compgen -G /etc/mkinitcpio.d/"*.preset" > /dev/null; then
case "$1" in
install)
# change to use all presets
args=(-P)
;;
remove)
shopt -s nullglob
for preset in /etc/mkinitcpio.d/*.preset; do
pkgbase=${preset##*/}
pkgbase=${pkgbase%.preset}
process_preset "$pkgbase" "$preset"
done
shopt -u nullglob
;;
esac
fi

if [[ "$1" == "install" ]] && (( ${#args[@]} )); then
mkinitcpio "${args[@]}"
fi
44 changes: 0 additions & 44 deletions libalpm/scripts/mkinitcpio-install

This file was deleted.

61 changes: 0 additions & 61 deletions libalpm/scripts/mkinitcpio-remove

This file was deleted.

0 comments on commit aac3876

Please sign in to comment.