Merge branch 'master' of ssh://wowbagger.slaskete.net/srv/git/einar-bin
[einar-bin] / addfollowmeprint.sh
1 #!/bin/bash
2 # This script installs the FollowMe print queue at NTNU on Linux (and possibly Mac) systems.
3 # The targeted and tested distros are: Debian, Ubuntu (and derivates), Fedora, CentOS, OpenSUSE and Mint
4 # Copyright © 2017 einar.haraldseid@ntnu.no
5
6 # Documentation
7 function usage {
8 echo "Usage: ./$(basename "${0}") [OPTIONS]"
9 echo "Options:"
10 echo " -m, --model {ricoh|generic} Printer model to install (default: generic)"
11 echo " -d, --driver {pcl,postscript} Printer driver to use (default: postscript)"
12 echo " -f, --force Force running script as if on Linux systems"
13 echo " -h, --help Display this help text"
14 }
15
16 # Print errors to STDERR
17 function printerror() {
18 echo "${*}" 1>&2
19 }
20
21 # Test for root
22 if [ ${UID} -ne 0 ] ; then
23 echo "This script must be run as root, relaunching with sudo:"
24 sudo bash "$0" "$@"
25 exit $?
26 fi
27
28 # Set default options that may be overridden by passed options below
29 Model="generic"
30 Driver="postscript"
31
32 # Set other default options
33 PrintServer="followprint.win.ntnu.no"
34 QueueName="FollowMe"
35 Workgroup="WIN-NTNU-NO"
36
37 while [[ $# -gt 0 ]]; do
38 Key="${1}"
39
40 case ${Key} in
41 -m|--model)
42 Model=$(echo "${2}" | tr "[:upper:]" "[:lower:]")
43 if [ "${Model}" != "ricoh" ] && [ "${Model}" != "generic" ]; then
44 printerror "Unknown model ${Model}, please choose one of ricoh or generic"
45 exit 1
46 fi
47 shift # Jump to next argument
48 ;;
49 -f|--force)
50 Force="YES"
51 shift # Jump to next argument
52 ;;
53 -d|--driver)
54 Driver=$(echo "${2}" | tr "[:upper:]" "[:lower:]")
55 if [ "${Driver}" != "postscript" ] && [ "${Driver}" != "pcl" ]; then
56 printerror "Unknown driver ${Driver}, plase choose one of postscript or pcl"
57 exit 1
58 fi
59 shift # Jump to next option
60 ;;
61 -h|--help)
62 usage
63 exit 0
64 ;;
65 *)
66 # unknown option
67 echo "Unknown option: ${Key}"
68 exit 1
69 ;;
70 esac
71
72 shift # past argument or value
73 done
74
75 # Test for supported OS
76 Uname=$(uname | tr "[:upper:]" "[:lower:]")
77 if [ "${Uname}" != "darwin" ] && [ "${Uname}" != "linux" ] && [ "${Force}" != "YES" ]; then
78 printerror "I don't think I can run on ${Uname}, but if you insist, please use -f to force Linux detection. This may not work, though."
79 exit 1
80 fi
81
82 # Set Uname to linux if we are forcing running the script
83 if [ "${Force}" = "YES" ]; then
84 Uname="linux"
85 fi
86
87 # Set printer share based on model (generic goes to ntnuprint-xerox)
88 if [ "${Model}" = "ricoh" ]; then
89 PrintShare="ntnuprint-ricoh"
90 else
91 PrintShare="ntnuprint-xerox"
92 fi
93
94 # Set printer driver path based on driver (we will override these if we're specific distros
95 if [ "${Driver}" = "postscript" ]; then
96 DriverPath="drv:///sample.drv/generic.ppd"
97 else
98 DriverPath="drv:///sample.drv/generpcl.ppd"
99 fi
100
101 # Test for CUPS
102 if ! command -v lpadmin > /dev/null 2>&1; then
103 printerror "You must have CUPS installed to add printers. Please install CUPS."
104 exit 1
105 fi
106
107 # Tests for Linux systems
108 if [ "${Uname}" = "linux" ]; then
109 # Test if smbclient is installed
110 if ! command -v smbclient >/dev/null 2>&1; then
111 printerror "You must have smbclient installed to print to NTNU followprint. Please install smbclient."
112 exit 1
113 fi
114
115 # Try to determine if the necessary printer drivers are installed, and set alternate driver paths
116 # This should match OpenSUSE, and probably SUSE Enterprise, and other derivates that use yast2 as the primary package
117 if command -v yast2 >/dev/null 2>&1; then
118 if [ "${Driver}" = "postscript" ]; then
119 if ! rpm -q OpenPrintingPPDs-postscript >/dev/null 2>&1; then
120 printerror "You don't seem to have the correct printer drivers installed, please run:"
121 printerror " sudo zypper install OpenPrintingPPDs-postscript"
122 printerror "first, or use the generic driver instead."
123 exit 1
124 else
125 if [ "${Model}" = "ricoh" ]; then
126 DriverPath="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz"
127 else
128 DriverPath="OpenPrintingPPDs/postscript/Generic-PostScript_Printer.Postscript.ppd.gz"
129 fi
130 fi
131 else
132 if ! rpm -q OpenPrintingPPDs-ghostscript >/dev/null 2>&1; then
133 printerror "You don't seem to have the correct printer drivers installed, please run:"
134 printerror " sudo zypper install OpenPrintingPPDs-ghostscript"
135 printerror "first, or use the generic driver instead."
136 exit 1
137 else
138 if [ "${Model}" = "ricoh" ]; then
139 DriverPath="OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz"
140 else
141 DriverPath="OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz"
142 fi
143 fi
144 fi
145 fi
146 # The rest of the tests ignore the model test (all other distros support drv:///)
147 if [ "${Model}" != "generic" ]; then
148 # This should match Fedora and other modern rpm based systems that have dnf as the primary package manager
149 if command -v dnf >/dev/null 2>&1; then
150 if ! rpm -q foomatic-db-ppds 2>&1; then
151 printerror "You don't seem to have the correct printer drivers installed, please run:"
152 printerror " sudo dnf install foomatic-db-ppds"
153 printerror "first, or use the generic driver instead."
154 exit 1
155 else
156 if [ "${Driver}" = "postscript" ]; then
157 DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
158 else
159 DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
160 fi
161 fi
162 fi
163 # This should match CentOS, RHEL and other RHEL based distros that have yum as the primary package manager
164 if command -v yum >/dev/null 2>&1; then
165 if ! rpm -q foomatic-db-ppds >/dev/null 2>&1; then
166 printerror "You don't seem to have the correct printer drivers installed, please run:"
167 printerror " sudo yum install foomatic-db-ppds"
168 printerror "first, or use the generic driver instead."
169 exit 1
170 else
171 if [ "${Driver}" = "postscript" ]; then
172 DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
173 else
174 DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
175 fi
176 fi
177 fi
178 # This should match Debian, Ubuntu and most if not all derivates that use dpkg as the primary package manager
179 if command -v dpkg >/dev/null 2>&1; then
180 if ! dpkg -s openprinting-ppds > /dev/null 2>&1; then
181 printerror "You must have the correct printer drivers installed, please run:"
182 printerror " sudo apt-get install openprinting-ppds"
183 printerror "first, or use the generic driver instead."
184 exit 1
185 else
186 if [ "${Driver}" = "postscript" ]; then
187 DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd"
188 else
189 DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd"
190 fi
191 fi
192 fi
193 fi
194 fi
195
196 echo "This script will add a new printer called ${QueueName}, connecting to the
197 print server ${PrintServer} using your normal user name and
198 password from NTNU."
199
200 # Get username and password
201 printf "User name: "
202 read Username
203 printf "Password: "
204 Settings=$(stty -g)
205 stty -echo
206 read Password
207 stty "${Settings}"
208
209 echo ""
210
211 # Some further tests for Linux systems
212 if [ "${Uname}" = "linux" ]; then
213 echo -e "\nNOTE: Your credentials will be stored in plaintext in /etc/cups/printers.conf.\nThis file us usually not readable for normal users, but still:\nDo not use this script on multi user systems!"
214 # Test for valid username and password if on Linux
215 # Bonus: find out if the print share is actually available
216 PrintServerIP=$(getent ahostsv4 ${PrintServer} | head -n 1 | cut -d " " -f 1)
217 smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" > /dev/null 2>&1
218 if [ $? -ne 0 ]; then
219 printerror "User name or password incorrect, or no contact with the print server ${PrintServer}."
220 exit 1
221 fi
222 ShareFound=$(smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" -g 2>/dev/null | grep ${PrintShare} | cut -d "|" -f 1)
223 if [ "${ShareFound}" != "Printer" ]; then
224 printerror "Could not find printer share called ${PrintShare} on the server"
225 printerror "This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
226 exit 1
227 fi
228 fi
229
230 # Similar tests for OSX
231 if [ "${Uname}" = "darwin" ]; then
232 smbutil view -A "//${Workgroup};${Username}:${Password}@${PrintServer}" > /dev/null 2>&1
233 if [ $? -ne 0 ]; then
234 printerror "User name or password incorrect, or no contact with the print server ${PrintServer}."
235 exit 1
236 fi
237 ShareFound=$(smbutil view "//${Workgroup};${Username}:${Password}@${PrintServer}" 2>/dev/null | grep ${PrintShare} | cut -d " " -f 1)
238 if [ "${ShareFound}" != "${PrintShare}" ]; then
239 printerror "Could not find printer share called ${PrintShare} on the server"
240 printerror "This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
241 exit 1
242 fi
243 fi
244
245 # Finally we can add the printer
246
247 # The Linux way
248 if [ "${Uname}" = "linux" ]; then
249 lpadmin -p ${QueueName} \
250 -D "FollowMe print queue at NTNU" \
251 -v "smb://${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \
252 -m "${DriverPath}" \
253 -u allow:all -E
254 fi
255
256 # The OSX way
257 if [ "${Uname}" = "darwin" ]; then
258 lpadmin -p ${QueueName} \
259 -D "FollowMe print queue at NTNU" \
260 -v "smb://${PrintServer}/${PrintShare}" \
261 -m "${DriverPath}" \
262 -o printer-is-shared=false -o printer-op-policy=authenticated \
263 -u allow:all -E
264
265 cupsenable "${QueueName}"
266 cupsaccept "${QueueName}"
267
268 # Add credentials to the keychain if they are missing
269 # Shamelessly stolen from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh
270 if ! security find-internet-password -s ${PrintServer} >/dev/null 2>&1; then
271 security -v add-internet-password -a "${Workgroup}\\${Username}" -s ${PrintServer} \
272 -w "${Password}" -D "Network Password" -r "smb " -l "${QueueName}" \
273 -T /System/Library/CoreServices/NetAuthAgent.app -T 'group://NetAuth' \
274 -T /System/Library/CoreServices/NetAuthAgent.app/Contents/MacOS/NetAuthSysAgent >/dev/null 2>&1
275 fi
276 fi
277
278 if [ $? -ne 0 ]; then
279 printerror "Could not connect to printer share. See above error for details."
280 exit 1
281 fi
282
283 # Set correct paper size and enable the duplexer option
284 lpadmin -p ${QueueName} -o PageSize=A4 -o Option1=True
285
286 if [ $? -ne 0 ]; then
287 printerror "Could not set default options on the print queue ${QueueName}. See above error for details."
288 exit 1
289 fi
290
291 # Set as default
292 lpadmin -d ${QueueName}
293
294 if [ $? -ne 0 ]; then
295 printerror "Could not set the print queue ${QueueName} as default printer. See above error for details."
296 exit 1
297 fi
298
299 echo "Printer successfully installed."