Skip to content

Instantly share code, notes, and snippets.

@xi
Created May 23, 2024 12:42
Show Gist options
  • Save xi/6bc37c57498ec649b2775647b63bd9e0 to your computer and use it in GitHub Desktop.
Save xi/6bc37c57498ec649b2775647b63bd9e0 to your computer and use it in GitHub Desktop.
makepkg for debian
#!/bin/bash -e
# This script is a wrapper around dpkg-deb that supports a subset of arch
# linux' PKGBUILD format. This means that it can be used to build debian
# (meta-)packages from a single file.
#
# PKGBUILD files are written in bash and can define the following variables:
#
# - pkgname [required]
# - pkgver
# - pkgdesc
# - maintainer
# - arch [required, array] (NOTE: only the first value will be used)
# - url
# - depends [array]
# - optdepends [array]
# - provides [array]
# - conflicts [array]
# - makedepends [array]
#
# In addition to that, the file can define the functions `package` for
# preparing and installing the files. It has access to the variables
# `$srcdir` which is the path to a temporary directory and `$pkgdir`
# which is the path to which the code must be installed.
#
# Inspired by the following tools:
#
# - http://checkinstall.izto.org/
# - https://github.com/hoffa/debpack
# - https://wiki.archlinux.org/index.php/Pkgbuild
if [ "$(id -u)" -eq 0 ]; then
echo "This script should not be run as root!"
exit 1
fi
install=
builddeps=
PKGBUILD='PKGBUILD'
while [ $# -gt 0 ]; do
case "$1" in
-i) install=true; shift;;
-m) builddeps=true; shift;;
-h) echo "Usage: makepkg [-i] [-m] [PKGBUILD]"; exit 1;;
*) PKGBUILD="$1"; shift;;
esac
done
join() {
local IFS=",";
echo "$*";
}
# define defaults
pkgver='0.0.0'
arch='all'
maintainer="$LOGNAME <$LOGNAME@$HOSTNAME>"
package() {
true
}
. "$PKGBUILD"
if [ "$builddeps" = true ]; then
if [ -z "$makedepends" ]; then
echo "no build dependencies necessary" >&2
exit 1
fi
unset depends
unset optdepends
unset provides
unset conflicts
pkgname="$pkgname-deps"
arch='all'
depends=${makedepends[@]}
package() {
true
}
fi
srcdir="$(mktemp -d)"
pkgdir="$(mktemp -d)"
trap "rm -rf '$srcdir' '$pkgdir'; exit" EXIT
(package)
# https://www.debian.org/doc/debian-policy/ch-controlfields.html
mkdir -p "$pkgdir/DEBIAN"
cat << EOF | sed 's/ *$//' | grep ': ' > "$pkgdir/DEBIAN/control"
Package: $pkgname
Priority: optional
Section: checkinstall
Installed-Size: $(du -s "$pkgdir" | awk '{print $1}')
Maintainer: $maintainer
Architecture: $arch
Version: $pkgver
Depends: $(join ${depends[@]})
Recommends: $(join ${optdepends[@]})
Provides: $(join ${provides[@]})
Conflicts: $(join ${conflicts[@]})
Description: $pkgdesc
Homepage: $url
EOF
if ! find "$pkgdir" -type f | sed "s|^$pkgdir||" | grep '^/etc/' > "$pkgdir/DEBIAN/conffiles"; then
rm "$pkgdir/DEBIAN/conffiles"
fi
cat "$pkgdir/DEBIAN/control"
fakeroot dpkg-deb --build "$pkgdir" "${pkgname}_${pkgver}_${arch}.deb"
if [ "$install" = true ]; then
sudo dpkg -i "${pkgname}_${pkgver}_${arch}.deb"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment