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 
   8   echo "Usage: ./$(basename "${0}") [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" 
  16 # Print errors to STDERR 
  17 function printerror
() { 
  22 if [ ${UID} -ne 0 ] ; then 
  23   echo "This script must be run as root, relaunching with sudo:"  
  28 # Set default options that may be overridden by passed options below 
  32 # Set other default options 
  33 PrintServer
="followprint.win.ntnu.no" 
  35 Workgroup
="WIN-NTNU-NO" 
  37 while [[ $# -gt 0 ]]; do 
  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" 
  47       shift # Jump to next argument 
  51       shift # Jump to next argument 
  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" 
  59       shift # Jump to next option 
  67       echo "Unknown option: ${Key}" 
  72   shift # past argument or value 
  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." 
  82 # Set Uname to linux if we are forcing running the script 
  83 if [ "${Force}" = "YES" ]; then 
  87 # Set printer share based on model (generic goes to ntnuprint-xerox) 
  88 if [ "${Model}" = "ricoh" ]; then 
  89   PrintShare
="ntnuprint-ricoh" 
  91   PrintShare
="ntnuprint-xerox" 
  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" 
  98   DriverPath
="drv:///sample.drv/generpcl.ppd" 
 102 if ! command -v lpadmin 
> /dev
/null 
2>&1; then 
 103   printerror 
"You must have CUPS installed to add printers. Please install CUPS." 
 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." 
 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." 
 125         if [ "${Model}" = "ricoh" ]; then 
 126           DriverPath
="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz" 
 128           DriverPath
="OpenPrintingPPDs/postscript/Generic-PostScript_Printer.Postscript.ppd.gz" 
 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." 
 138         if [ "${Model}" = "ricoh" ]; then 
 139           DriverPath
="OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz" 
 141           DriverPath
="OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz" 
 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." 
 156         if [ "${Driver}" = "postscript" ]; then 
 157           DriverPath
="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz" 
 159           DriverPath
="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz" 
 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." 
 171         if [ "${Driver}" = "postscript" ]; then 
 172           DriverPath
="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz" 
 174           DriverPath
="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz" 
 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." 
 186         if [ "${Driver}" = "postscript" ]; then 
 187           DriverPath
="openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd" 
 189           DriverPath
="openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd" 
 196 echo "This script will add a new printer called ${QueueName}, connecting to the 
 197 print server ${PrintServer} using your normal user name and 
 200 # Get username and password 
 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}." 
 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.
" 
 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}.
" 
 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." 
 245 # Finally we can add the printer 
 248 if [ "${Uname}" = "linux" ]; then 
 249   lpadmin  
-p ${QueueName} \
 
 250    -D "FollowMe print queue at NTNU" \
 
 251    -v "smb://${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \
 
 257 if [ "${Uname}" = "darwin" ]; then 
 258   lpadmin 
-p ${QueueName} \
 
 259    -D "FollowMe print queue at NTNU" \
 
 260    -v "smb://${PrintServer}/${PrintShare}" \
 
 262    -o printer-is-shared
=false 
-o printer-op-policy
=authenticated \
 
 265   cupsenable 
"${QueueName}" 
 266   cupsaccept 
"${QueueName}" 
 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 
 278 if [ $? -ne 0 ]; then 
 279   printerror "Could not connect to printer share. See above error 
for details.
" 
 283 # Set correct paper size and enable the duplexer option 
 284 lpadmin -p ${QueueName} -o PageSize=A4 -o Option1=True 
 286 if [ $? -ne 0 ]; then 
 287   printerror "Could not 
set default options on the print queue 
${QueueName}. See above error 
for details.
" 
 292 lpadmin -d ${QueueName} 
 294 if [ $? -ne 0 ]; then 
 295   printerror "Could not 
set the print queue 
${QueueName} as default printer. See above error 
for details.
" 
 299 echo "Printer successfully installed.
"