Repository navigation
Expand file tree
/
Copy pathpostinstall
More file actions
executable file
·86 lines (81 loc) · 4.45 KB
/
Copy pathpostinstall
File metadata and controls
executable file
·86 lines (81 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/sh
# Apple Silicon Printer Drivers — post-install
# 1. retire vendor Intel-only driver bits that block Apple's generic USB class driver
# 2. refresh queues left by older packages with the bundled PPDs
# 3. auto-detect USB printers and create queues for supported models
set -u
GP=/Library/Printers/Gutenprint
B="/Users/Shared/printer-driver-backup"
NEED_REBOOT=0
log() { echo "[printer-drivers] $*"; }
chown -R root:wheel "$GP"; chmod -R u=rwX,go=rX "$GP"
mkdir -p /usr/local/bin && ln -sf "$GP/bin/gutenprint-add" /usr/local/bin/gutenprint-add
# ---- 1) Canon IJ driver (Intel-only). Its codeless kext pins Canon's x86_64 USB class-driver
# plugin to the printer, which stops macOS from using its own generic driver.
if [ -d /Library/Extensions/BJUSBLoad.kext ]; then
mkdir -p "$B"
kmutil unload -b jp.co.canon.bj.print.BJUSBLoad >/dev/null 2>&1 || true
mv /Library/Extensions/BJUSBLoad.kext "$B/" && log "moved Canon BJUSBLoad.kext -> $B"
kmutil install --update-all >/dev/null 2>&1 || true
NEED_REBOOT=1
fi
if [ -d /Library/Printers/Canon/BJPrinter ]; then
mkdir -p "$B"; mv /Library/Printers/Canon "$B/Canon" && log "moved /Library/Printers/Canon -> $B"
fi
for p in /Library/Printers/PPDs/Contents/Resources/CanonIJ*.ppd.gz; do
[ -f "$p" ] && mkdir -p "$B" && mv "$p" "$B/" && log "moved $(basename "$p") -> $B"
done
rm -rf /Library/Caches/Canon
# queues that still point at the Canon IJ filters can never print again
for f in /etc/cups/ppd/*.ppd; do
[ -f "$f" ] || continue; q=$(basename "$f" .ppd)
if grep -qs 'Raster2CanonIJ' "$f"; then
lpadmin -x "$q" && log "removed dead Canon queue: $q"
fi
done
# ---- 2) queues created by an older package keep its PPD, which the new filters reject or misread:
# rastertogutenprint refuses a PPD from another Gutenprint version, and brlaser 6.2.8 reads
# options brlaser v6 PPDs never set (toner density would go out as -100). Reinstall the
# bundled PPD on the same device; lpadmin keeps the chosen defaults.
GPVER=$("$GP/libexec/cups-genppd.5.3" -V 2>/dev/null | sed -n 's/^cups-genppd version \([0-9.]*\),.*/\1/p')
for f in /etc/cups/ppd/*.ppd; do
[ -f "$f" ] || continue
if grep -qs "$GP/libexec/rastertobrlaser" "$f"; then
grep -qs 'brlaserDensityAdjust' "$f" && continue
ID=brlaser-$(sed -n 's/^\*ShortNickName: "Brother \(.*\)"/\1/p' "$f" | sed 's/ series$//' | tr 'A-Z' 'a-z' | tr -c 'a-z0-9\n' '-')
elif grep -qs "$GP/libexec/rastertogutenprint" "$f"; then
# also redo PPDs generated under a comma-decimal locale ("PageSize[612,000 792,000]"), which CUPS rejects
{ [ -z "$GPVER" ] || grep -qs "CUPS+Gutenprint v$GPVER\"" "$f"; } && ! grep -qsE 'PageSize\[[0-9]+,[0-9]' "$f" && continue
ID=$(sed -n 's/^\*StpDriverName:[[:space:]]*"\(.*\)"/\1/p' "$f")
else
continue
fi
q=$(basename "$f" .ppd)
URI=$(awk -v q="$q" '$1 ~ /^<(Default)?Printer$/ { sub(/>$/, "", $2); cur = $2 } cur == q && $1 == "DeviceURI" { print $2; exit }' /etc/cups/printers.conf 2>/dev/null)
if [ -n "$ID" ] && [ -n "$URI" ] && "$GP/bin/gutenprint-add" "$ID" "$q" "$URI" >/dev/null 2>&1; then
log "queue '$q' updated to the current $ID driver"
else
log "could not update queue '$q' — remove it in Printers & Scanners and run: sudo gutenprint-add ${ID:-<id>}"
fi
done
# ---- 3) auto-detect USB printers
# lpinfo -v prints: direct usb://Vendor/Model%20name?serial=XXX
# Match through the helper so Gutenprint and brlaser use the same catalog.
lpinfo -v 2>/dev/null | awk '$2 ~ /^usb:\/\//{print $2}' | while read -r URI; do
DEV=$(printf '%s' "$URI" | sed -E 's#^usb://##; s#\?.*##; s#%20# #g; s#/# #')
ID=$("$GP/bin/gutenprint-add" --match-usb "$URI")
if [ -n "$ID" ]; then
QUEUE=$(printf '%s' "$DEV" | tr -c 'A-Za-z0-9\n' '_' | sed -E 's/_+/_/g; s/^_//; s/_$//')
if "$GP/bin/gutenprint-add" "$ID" "$QUEUE" "$URI" >/dev/null 2>&1; then
lpadmin -d "$QUEUE" 2>/dev/null || true
log "queue '$QUEUE' created for '$DEV' ($ID) and set as default"
else
log "could not create queue for '$DEV'"
fi
else
log "USB printer '$DEV' not matched automatically — run: gutenprint-add --list \"<model>\" then: sudo gutenprint-add <id>"
fi
done
[ "$(lpinfo -v 2>/dev/null | grep -c '^direct usb://')" = 0 ] && log "no USB printer connected — connect one and run: sudo gutenprint-add <id> (find <id> with: gutenprint-add --list \"<model>\")"
[ "$NEED_REBOOT" = 1 ] && log "REBOOT REQUIRED (vendor kernel extension removed) — then unplug/replug the printer once."
exit 0