aptpkg/aptpkg

184 lines
3.6 KiB
Plaintext
Raw Normal View History

#! /usr/bin/env bash
2021-07-26 13:41:48 -04:00
# Init variables
2021-10-16 18:33:22 -04:00
APTPKG_VERSION="1.1.0"
2021-07-26 13:41:48 -04:00
# Show help
show_help(){
cat > /dev/stdout <<EOF
aptpkg v$APTPKG_VERSION
Easily create debian packages.
For more documentation, run man aptpkg
2021-07-26 13:41:48 -04:00
Usage:
aptpkg [directory]
aptpkg --all [directory] Build all folders under directory
aptpkg --auto (commit id) Autobuild modified packages from commit
2021-07-26 13:41:48 -04:00
aptpkg -h, --help Show help
aptpkg -v, --version Show version
2021-07-26 13:41:48 -04:00
EOF
}
# Incorrect aptpkg usage: explain, show help, and exit
error_usage_die(){
echo "ERROR: " "$1"
show_help
exit 1
}
# Verify sha512sums
verify(){
if [ ! "${sha512sums:?}" = "SKIP" ]; then
echo "${sha512sums:?}" | sha512sum -c
fi
}
# Copy/ install wrapper for files
cin-bin(){
install -Dm755 "$@"
}
cin-file(){
install -Dm644 "$@"
}
2021-07-26 13:41:48 -04:00
# Check that "build" file exists in directory
check(){
if ! [ -f "$1/build" ]; then
echo "No package of that name exists."
exit 1
fi
}
2021-07-26 17:33:50 -04:00
# Generate the debian control file
gen_control_file(){
mkdir -p "$builddir"/DEBIAN
2021-09-06 21:20:51 -04:00
echo "Package: ${name:?}
Version: ${version:?}-${rev:?}
2021-07-26 17:33:50 -04:00
Architecture: amd64
Maintainer: $(grep -am 1 -hr "Maintainer: " build | sed 's/# Maintainer: //')
Section:
Priority:
2021-09-06 21:20:51 -04:00
Homepage: ${url:?}
Description: ${description:?}
${customcontrol:=}
2021-07-26 17:33:50 -04:00
" > "$builddir"/DEBIAN/control
}
2021-07-26 13:41:48 -04:00
# Download a debian package
build_deb(){
2021-09-06 21:20:51 -04:00
echo "Getting ${source:?}"
wget "$source" -qP dist/
2021-09-06 21:20:51 -04:00
( cd dist || error_usage_die "Problem with the dist/ directory" && verify )
2021-07-26 13:41:48 -04:00
}
2021-07-26 17:33:50 -04:00
# Download and build using files
build_file(){
builddir=${name}_${version}-${rev}_amd64
2021-09-06 21:20:51 -04:00
cd "$1" || error_usage_die "Problem with changing directory"
2021-07-26 17:33:50 -04:00
mkdir -p "$builddir"
for urls in $source; do
echo "Getting $source"
wget -q "$urls"
done
verify
# Run
2021-07-26 17:33:50 -04:00
steps "$@"
gen_control_file "$@"
2021-07-26 23:14:32 -04:00
for maintfile in preinst postinst prerm postrm; do
2021-07-27 18:47:30 -04:00
[ -f $maintfile ] && cin-bin $maintfile "$builddir"/DEBIAN/$maintfile
2021-07-26 23:14:32 -04:00
done
2021-07-26 17:33:50 -04:00
2021-09-06 21:20:51 -04:00
cd - || error_usage_die "Problem with changing directory"
2021-07-26 17:33:50 -04:00
dpkg-deb --build --root-owner-group "$1"/"$builddir" dist/
}
2021-07-26 13:41:48 -04:00
# Determine which function to run
load_build(){
2021-07-26 17:26:29 -04:00
mkdir -p dist
2021-07-26 13:41:48 -04:00
# shellcheck disable=SC1090
# shellcheck disable=SC1091
source "${1:?}/build"
2021-07-26 13:41:48 -04:00
2021-09-06 21:20:51 -04:00
case "${type:?}" in
2021-07-26 13:41:48 -04:00
deb)
build_deb ;;
2021-07-26 17:33:50 -04:00
file)
build_file "$@" ;;
2021-07-26 13:41:48 -04:00
esac
}
# Autobuild packages based on git list files changed in commit
# latest (HEAD) assumed if commit hash not provided
change_autobuild_pkgs(){
[ -d .git ] || error_usage_die "Current directory is not a git repository!"
commitid=$1
[ -n "$commitid" ] || commitid=HEAD
if [[ "$commitid" = HEAD ]]; then
before_run=$(git log HEAD^..HEAD | grep aptpkg-auto-run | sed 's#aptpkg-auto-run: ##' | sed 's# ##')
2021-10-06 22:09:22 -04:00
if [ -n "$before_run" ]; then
for m in $before_run; do
build_chg_pre=$(git diff-tree --no-commit-id --name-only -r "$m" | grep "build" | sed 's#/build##')
echo "Using $m with files $build_chg_pre"
for a in $build_chg_pre; do
aptpkg "$a"
done
done
fi
fi
build_chg=$(git diff-tree --no-commit-id --name-only -r $commitid | grep "build" | sed 's#/build##')
echo "Using $commitid with files $build_chg"
for f in $build_chg; do
aptpkg "$f"
done
}
# Build all in directory
build_all_in_dir(){
[ -d "$1" ] || error_usage_die "Provide a directory"
buildfile_folders=$(ls -d "$1"/*)
for ww in $buildfile_folders; do
printf "\nBuilding %s\n\n" "$ww"
aptpkg "$ww"
done
}
2021-07-26 13:41:48 -04:00
# Run!
case $1 in
-h|--help)
show_help ;;
--all)
shift
build_all_in_dir "$1" ;;
--auto)
shift
change_autobuild_pkgs "$1" ;;
-v|--version)
echo "aptpkg $APTPKG_VERSION" ;;
2021-07-26 13:41:48 -04:00
*)
2021-07-26 17:26:29 -04:00
if [ $# -ne 1 ]; then
error_usage_die "You must provide one directory"
fi
2021-07-26 13:41:48 -04:00
check "$1"
load_build "$1" ;;
esac