Last active
March 6, 2024 09:41
-
-
Save knu/18ecb0c63d54cea5bdbc7b21dd6488da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
Verbose= | |
DryRun= | |
run() { | |
if [ -n "$DryRun" ]; then | |
echo "$*" | |
elif [ -n "$Verbose" ]; then | |
echo "$*" | |
"$@" | |
else | |
"$@" | |
fi | |
} | |
xrun() { | |
if [ -n "$DryRun" ]; then | |
xargs echo "$@" | |
elif [ -n "$Verbose" ]; then | |
xargs sh -c 'echo "$*"; "$@"' . "$@" | |
else | |
xargs "$@" | |
fi | |
} | |
main() { | |
local opt file destdir filename dest tmpfile ppa | |
set -e | |
if [ "$(id -u)" -ne 0 ]; then | |
exec sudo "$0" "$@" | |
fi | |
while getopts "nv" opt; do | |
case $opt in | |
n) | |
DryRun=t | |
;; | |
v) | |
Verbose=t | |
;; | |
*) | |
exit 64 | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ $# -eq 0 ]; then | |
echo "usage: $0 [-n] [-v] <Debfile>..." >&2 | |
exit 64 | |
fi | |
initialize | |
cat -- "$@" > "$TmpDir/Debfile" | |
. "$TmpDir/Debfile" | |
destdir=$HOME/.cache/$(basename "$0") | |
while read url; do | |
file=$(basename "$url") | |
if [ -f "$destdir/$file" ]; then | |
curl -fsSLR -z "$destdir/$file" -o "$destdir/$file" "$url" | |
else | |
curl -fsSLR -o "$destdir/$file" "$url" | |
fi | |
echo installing "$file" | |
run dpkg -i "$file" | |
done < "$TmpDir/debs" | |
destdir=/usr/share/keyrings | |
find "$TmpDir/keyring_scripts" -type f | while read -r file; do | |
filename=$(basename "$file" .sh).gpg | |
dest=$destdir/$filename | |
tmpfile=$TmpDir/keyrings/$filename | |
[ -f "$dest" ] || ( | |
set -e | |
dir=$TmpDir/tmp | |
rm -rf "$dir" | |
mkdir "$dir" | |
cd "$dir" | |
sh "$file" > "$tmpfile" | |
if grep -qe '^-----BEGIN PGP PUBLIC KEY BLOCK-----' "$tmpfile"; then | |
gpg --dearmor < "$tmpfile" > keyring.gpg | |
mv keyring.gpg "$tmpfile" | |
fi | |
run install -o 0 -g 0 -m 644 "$tmpfile" "$dest" | |
) | |
done | |
destdir=/etc/apt/sources.list.d | |
find "$TmpDir/sources" -type f -name '*.list' | while read -r file; do | |
cmp -s "$file" "$destdir/$(basename "$file")" 2>/dev/null || | |
run install -o 0 -g 0 -m 644 "$file" "$destdir/" | |
done | |
while read ppa; do | |
if [ -z "$(find /etc/apt/sources.list.d -name '*.list' -type f | xargs -r awk -v repo="http://ppa.launchpad.net/$ppa/ubuntu" '$1 == "deb" && $2 == repo')" ]; then | |
run add-apt-repository --no-update -y "ppa:$ppa" | |
fi | |
done < $TmpDir/sources/ppas | |
run apt update | |
xrun apt install -y < "$TmpDir/packages" | |
} | |
initialize() { | |
TmpDir=$(mktemp -d "${TMPDIR-/tmp}/${0##*/}.XXXXXX") || exit 1 | |
trap 'finalize' 0 1 2 3 15 | |
mkdir -p "$TmpDir/keyring_scripts" "$TmpDir/keyrings" "$TmpDir/sources" | |
touch "$TmpDir/debs" "$TmpDir/ppas" | |
} | |
finalize() { | |
rm -rf "$TmpDir" | |
} | |
die() { | |
printf "%s: %s\n" "$0" "$*" >&2 | |
exit 1 | |
} | |
package() { | |
[ $# -eq 1 ] || die "usage: package <name>" | |
local pkg="$1" | |
case "$pkg" in | |
https://*|http://*) | |
printf "%s\n" "$pkg" >> "$TmpDir/debs" | |
esac | |
printf "%s\n" "$pkg" >> "$TmpDir/packages" | |
} | |
keyring() { | |
[ $# -eq 1 ] || die "usage: keyring <name> <<COMMAND...COMMAND" | |
local name="$1" | |
cat > "$TmpDir/keyring_scripts/$name.sh" | |
} | |
source() { | |
[ $# -eq 1 ] || die "usage: source <name> <<LIST...LIST" | |
local name="$1" | |
cat > "$TmpDir/sources/$name.list" | |
} | |
ppa() { | |
[ $# -eq 1 ] || die "usage: ppa <user>/<ppa-name>" | |
local name="$1" | |
printf "%s\n" "$name" >> "$TmpDir/sources/ppas" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment