hpcups-update-ppds.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/bash
  2. ## Copyright (C) 2010 Red Hat, Inc.
  3. ## Authors:
  4. ## Tim Waugh <twaugh@redhat.com>
  5. ## This program is free software; you can redistribute it and/or modify
  6. ## it under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2 of the License, or
  8. ## (at your option) any later version.
  9. ## This program is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ## GNU General Public License for more details.
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; if not, write to the Free Software
  15. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ## Purpose: Update hpcups PPDs when necessary.
  17. sock=/var/run/cups/cups.sock
  18. running=$(LC_ALL=C lpstat -h "$sock" -r 2>/dev/null)
  19. if [ "$?" -ne 0 ]
  20. then
  21. # No lpstat in path
  22. exit 0
  23. fi
  24. if [ -z "${running##*not*}" ]
  25. then
  26. # scheduler is not running
  27. exit 0
  28. fi
  29. trap 'rm -f "$tmpdir"/models; rmdir "$tmpdir"; exit 0' \
  30. 0 HUP INT QUIT ILL ABRT PIPE TERM
  31. debug=true
  32. tmpdir="$(mktemp -d)"
  33. for ppd in /etc/cups/ppd/*.ppd
  34. do
  35. [ -r "$ppd" ] || continue
  36. queue="${ppd#/etc/cups/ppd/}"
  37. queue="${queue%.ppd}"
  38. lpstat -h "$sock" -p "$queue" &>/dev/null || continue
  39. # We have PPD associated with a queue. Find out its NickName
  40. $debug && echo "Examining $queue"
  41. nickname="$(grep '^\*NickName:' "$ppd")"
  42. nickname="${nickname#*\"}" # strip text up to and incl first double quote
  43. nickname="${nickname%\"*}" # strip final double quote
  44. $debug && echo "NickName is: $nickname"
  45. # Is it an hpcups PPD?
  46. [ -z "${nickname##*, hpcups*}" ] || continue
  47. $debug && echo "hpcups: true"
  48. # No: need to regenerate the PPD.
  49. if [ ! -f "$tmpdir/models" ]
  50. then
  51. # Get list of driver URIs and NickNames
  52. lpinfo -h "$sock" --include-schemes=drv -m 2>/dev/null >"$tmpdir/models"
  53. fi
  54. # Strip hpcups version from NickName
  55. nickname="${nickname%, hpcups*}"
  56. $debug && echo "Stripped NickName: $nickname"
  57. while read line
  58. do
  59. uri=${line%% *}
  60. nn="${line#$uri }"
  61. [ -z "${nn##*, hpcups*}" ] || continue
  62. nn="${nn%, hpcups*}"
  63. if [ "$nn" == "$nickname" ]
  64. then
  65. $debug && echo "Match found, URI: $uri"
  66. # Unfortunately CUPS will reset the page size when we
  67. # change the PPD, due to the weird page size names that
  68. # HPLIP uses. Try to maintain the existing page size.
  69. size="$(grep '^\*DefaultPageSize:' "$ppd")"
  70. size="${size##* }" # strip until after first ' '
  71. size="${size%% *}" # strip after any ' '
  72. $debug && echo "PageSize is $size"
  73. if [ -z "${size#*Duplex}" ]
  74. then
  75. # Special handling for duplex sizes because HPLIP
  76. # broke backwards compatibility with *that* too!
  77. size="${size%Duplex}.Duplex"
  78. fi
  79. null=/dev/null
  80. $debug && null=/dev/stdout
  81. lpadmin -h "$sock" -p "$queue" -m "$uri" &>"$null" || :
  82. $debug && echo "PPD regenerated"
  83. lpadmin -h "$sock" -p "$queue" -o PageSize="$size" &>"$null" || :
  84. $debug && echo "PageSize restored to $size"
  85. break
  86. fi
  87. done <"$tmpdir/models"
  88. done
  89. exit 0