@@ -0,0 +1,89 @@
1+{ stdenv, fetchurl, pkgsi686Linux, dpkg, makeWrapper, coreutils, gnused, gawk, file, cups, patchelf, utillinux, vimNox
2+, ghostscript, a2ps }:
3+
4+# Why:
5+# The executable "brprintconf_mfcj6510dw" binary is looking for "/opt/brother/Printers/%s/inf/br%sfunc" and "/opt/brother/Printers/%s/inf/br%src".
6+# Whereby, %s is printf(3) string substitution for stdin's arg0 (the command's own filename) from the 10th char forwards, as a runtime dependency.
7+# e.g. Say the filename is "0123456789ABCDE", the runtime will be looking for /opt/brother/Printers/ABCDE/inf/brABCDEfunc.
8+# Presumably, the binary was designed to be deployed under the filename "printconf_mfcj6510dw", whereby it will search for "/opt/brother/Printers/mfcj6510dw/inf/brmfcj6510dwfunc".
9+# For NixOS, we want to change the string to the store path of brmfcj6510dwfunc and brmfcj6510dwrc but we're faced with two complications:
10+# 1. Too little room to specify the nix store path. We can't even take advantage of %s by renaming the file to the store path hash since the variable is too short and can't contain the whole hash.
11+# 2. The binary needs the directory it's running from to be r/w.
12+# What:
13+# As such, we strip the path and substitution altogether, leaving only "brmfcj6510dwfunc" and "brmfcj6510dwrc", while filling the leftovers with nulls.
14+# Fully null terminating the cstrings is necessary to keep the array the same size and preventing overflows.
15+# We then use a shell script to link and execute the binary, func and rc files in a temporary directory.
16+# How:
17+# In the package, we dump the raw binary as a string of search-able hex values using hexdump. We execute the substitution with sed. We then convert the hex values back to binary form using xxd.
18+# We also write a shell script that invoked "mktemp -d" to produce a r/w temporary directory and link what we need in the temporary directory.
19+# Result:
20+# The user can run brprintconf_mfcj6510dw in the shell.
21+
22+stdenv.mkDerivation rec {
23+ name = "mfcj6510dwlpr-${version}";
24+ version = "3.0.0-1";
25+
26+ src = fetchurl {
27+ url = "http://download.brother.com/welcome/dlf006614/mfcj6510dwlpr-${version}.i386.deb";
28+ sha256 = "1ccvx393pqavsgzd8igrzlin5jrsf01d3acyvwqd1d0yz5jgqy6d";
29+ };
30+
31+ nativeBuildInputs = [ makeWrapper ];
32+ buildInputs = [ cups ghostscript dpkg a2ps ];
33+
34+ unpackPhase = "true";
35+
36+ brprintconf_mfcj6510dw_script = ''
37+ #!/bin/sh
38+ cd $(mktemp -d)
39+ ln -s @out@/usr/bin/brprintconf_mfcj6510dw_patched brprintconf_mfcj6510dw_patched
40+ ln -s @out@/opt/brother/Printers/mfcj6510dw/inf/brmfcj6510dwfunc brmfcj6510dwfunc
41+ ln -s @out@/opt/brother/Printers/mfcj6510dw/inf/brmfcj6510dwrc brmfcj6510dwrc
42+ ./brprintconf_mfcj6510dw_patched "$@"
43+ '';
44+
45+ installPhase = ''
46+ dpkg-deb -x $src $out
47+ substituteInPlace $out/opt/brother/Printers/mfcj6510dw/lpd/filtermfcj6510dw \
48+ --replace /opt "$out/opt"
49+ substituteInPlace $out/opt/brother/Printers/mfcj6510dw/lpd/psconvertij2 \
50+ --replace "GHOST_SCRIPT=`which gs`" "GHOST_SCRIPT=${ghostscript}/bin/gs"
51+ substituteInPlace $out/opt/brother/Printers/mfcj6510dw/inf/setupPrintcapij \
52+ --replace "/opt/brother/Printers" "$out/opt/brother/Printers" \
53+ --replace "printcap.local" "printcap"
54+
55+ patchelf --set-interpreter ${pkgsi686Linux.stdenv.cc.libc.out}/lib/ld-linux.so.2 \
56+ --set-rpath $out/opt/brother/Printers/mfcj6510dw/inf:$out/opt/brother/Printers/mfcj6510dw/lpd \
57+ $out/opt/brother/Printers/mfcj6510dw/lpd/brmfcj6510dwfilter
58+ patchelf --set-interpreter ${pkgsi686Linux.stdenv.cc.libc.out}/lib/ld-linux.so.2 $out/usr/bin/brprintconf_mfcj6510dw
59+
60+ #stripping the hardcoded path.
61+ ${utillinux}/bin/hexdump -ve '1/1 "%.2X"' $out/usr/bin/brprintconf_mfcj6510dw | \
62+ sed 's.2F6F70742F62726F746865722F5072696E746572732F25732F696E662F6272257366756E63.62726d66636a36353130647766756e63000000000000000000000000000000000000000000.' | \
63+ sed 's.2F6F70742F62726F746865722F5072696E746572732F25732F696E662F627225737263.62726D66636A3635313064777263000000000000000000000000000000000000000000.' | \
64+ ${vimNox}/bin/xxd -r -p > $out/usr/bin/brprintconf_mfcj6510dw_patched
65+ chmod +x $out/usr/bin/brprintconf_mfcj6510dw_patched
66+ #executing from current dir. segfaults if it's not r\w.
67+ mkdir -p $out/bin
68+ echo -n "$brprintconf_mfcj6510dw_script" > $out/bin/brprintconf_mfcj6510dw
69+ chmod +x $out/bin/brprintconf_mfcj6510dw
70+ substituteInPlace $out/bin/brprintconf_mfcj6510dw --replace @out@ $out
71+
72+ mkdir -p $out/lib/cups/filter/
73+ ln -s $out/opt/brother/Printers/mfcj6510dw/lpd/filtermfcj6510dw $out/lib/cups/filter/brother_lpdwrapper_mfcj6510dw
74+
75+ wrapProgram $out/opt/brother/Printers/mfcj6510dw/lpd/psconvertij2 \
76+ --prefix PATH ":" ${ stdenv.lib.makeBinPath [ coreutils gnused gawk ] }
77+ wrapProgram $out/opt/brother/Printers/mfcj6510dw/lpd/filtermfcj6510dw \
78+ --prefix PATH ":" ${ stdenv.lib.makeBinPath [ coreutils gnused file ghostscript a2ps ] }
79+ '';
80+
81+ meta = with stdenv.lib; {
82+ homepage = http://www.brother.com/;
83+ description = "Brother MFC-J6510DW LPR driver";
84+ license = with licenses; unfree;
85+ platforms = with platforms; linux;
86+ downloadPage = http://support.brother.com/g/b/downloadlist.aspx?c=us&lang=en&prod=mfcj6510dw_all&os=128;
87+ maintainers = with maintainers; [ ramkromberg ];
88+ };
89+}