#!/bin/sh
# airprint.sh
# 2020-06-27
# by Gernot WALZL
# This script enables AirPrint for CUPS printers.
# avahi-daemon uses UDP port 5353 and UDP ports between 32768 and 61000.
# Install avahi
groupadd -g 214 avahi
useradd -u 214 -g 214 -c Avahi -d /dev/null -s /bin/false avahi
sbopkg -i "libdaemon avahi nss-mdns" -k
# Start avahi at boot
if ! grep avahi /etc/rc.d/rc.local ; then
cat >> /etc/rc.d/rc.local <<EOF
if [ -x /etc/rc.d/rc.avahidaemon ]; then
/etc/rc.d/rc.avahidaemon start
fi
if [ -x /etc/rc.d/rc.avahidnsconfd ]; then
/etc/rc.d/rc.avahidnsconfd start
fi
EOF
fi
# Stop avahi at shutdown
if ! grep avahi /etc/rc.d/rc.local_shutdown ; then
cat >> /etc/rc.d/rc.local_shutdown <<EOF
if [ -x /etc/rc.d/rc.avahidnsconfd ]; then
/etc/rc.d/rc.avahidnsconfd stop
fi
if [ -x /etc/rc.d/rc.avahidaemon ]; then
/etc/rc.d/rc.avahidaemon stop
fi
EOF
fi
# Generate AirPrint service
wget -O master.zip https://github.com/tjfontaine/airprint-generate/archive/master.zip
unzip master.zip
cd airprint-generate-master || exit 1
./airprint-generate.py
mv Air*.service /etc/avahi/services/
# https://www.linux-community.de/ausgaben/linuxuser/2013/08/vom-ipad-oder-iphone-via-airprint-und-cups-drucken/
echo "image/urf urf string(0,UNIRAST<00>)" > /usr/share/cups/mime/airprint.types
echo "image/urf application/pdf 100 pdftoraster" > /usr/share/cups/mime/airprint.convs
# Print jobs are sent to $(hostname).local:631
# /etc/nsswitch.conf can be set to use mdns to resolve *.local names.
# I prefer to use a simple entry in /etc/hosts to resolve the local hostname.
# Advantage: Not every name resolution involves mdns.
CWD=$(pwd)
TMP=$(mktemp -d)
cd "$TMP" || exit 1
cat > hosts.hints <<EOF
#...
127.0.0.1 localhost
127.0.0.1 myhostname.localdomain myhostname.local myhostname
#...
EOF
emacs hosts.hints /etc/hosts
cd "$CWD"
rm -rf "$TMP"
# Troubleshooting:
# avahi-daemon logs errors to /var/log/messages
# cups logs errors to /var/log/cups/error_log
# If no error is logged, rebooting the apple device often helps.