canon_lbp_setup.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/bin/bash
  2. ##################################################
  3. #Version 3.3 updated on September 13, 2019
  4. #http://help.ubuntu.ru/wiki/canon_capt
  5. #http://forum.ubuntu.ru/index.php?topic=189049.0
  6. #Translated into English and modified by @hieplpvip
  7. ##################################################
  8. #Check if we are running as root
  9. [ $USER != 'root' ] && exec sudo "$0"
  10. #Current user
  11. LOGIN_USER=$(logname)
  12. [ -z "$LOGIN_USER" ] && LOGIN_USER=$(who | head -1 | awk '{print $1}')
  13. #Load the file containing the path to the desktop
  14. if [ -f ~/.config/user-dirs.dirs ]; then
  15. source ~/.config/user-dirs.dirs
  16. else
  17. XDG_DESKTOP_DIR="$HOME/Desktop"
  18. fi
  19. #Driver version
  20. DRIVER_VERSION='2.71-1'
  21. DRIVER_VERSION_COMMON='3.21-1'
  22. #Links to driver packages
  23. declare -A URL_DRIVER=([amd64_common]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/cndrvcups-common_3.21-1_amd64.deb' \
  24. [amd64_capt]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/cndrvcups-capt_2.71-1_amd64.deb' \
  25. [i386_common]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/cndrvcups-common_3.21-1_i386.deb' \
  26. [i386_capt]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/cndrvcups-capt_2.71-1_i386.deb')
  27. #Links to autoshutdowntool
  28. declare -A URL_ASDT=([amd64]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/autoshutdowntool_1.00-1_amd64_deb.tar.gz' \
  29. [i386]='https://github.com/hieplpvip/canon_printer/raw/master/Packages/autoshutdowntool_1.00-1_i386_deb.tar.gz')
  30. #ppd files and printer models mapping
  31. declare -A LASERSHOT=([LBP-810]=1120 [LBP1120]=1120 [LBP1210]=1210 \
  32. [LBP2900]=2900 [LBP3000]=3000 [LBP3010]=3050 [LBP3018]=3050 [LBP3050]=3050 \
  33. [LBP3100]=3150 [LBP3108]=3150 [LBP3150]=3150 [LBP3200]=3200 [LBP3210]=3210 \
  34. [LBP3250]=3250 [LBP3300]=3300 [LBP3310]=3310 [LBP3500]=3500 [LBP5000]=5000 \
  35. [LBP5050]=5050 [LBP5100]=5100 [LBP5300]=5300 [LBP6000]=6018 [LBP6018]=6018 \
  36. [LBP6020]=6020 [LBP6020B]=6020 [LBP6200]=6200 [LBP6300n]=6300n [LBP6300]=6300 \
  37. [LBP6310]=6310 [LBP7010C]=7018C [LBP7018C]=7018C [LBP7200C]=7200C [LBP7210C]=7210C \
  38. [LBP9100C]=9100C [LBP9200C]=9200C)
  39. #Sort printer names
  40. NAMESPRINTERS=$(echo "${!LASERSHOT[@]}" | tr ' ' '\n' | sort -n -k1.4)
  41. #Models supported by autoshutdowntool
  42. declare -A ASDT_SUPPORTED_MODELS=([LBP6020]='MTNA002001 MTNA999999' \
  43. [LBP6020B]='MTMA002001 MTMA999999' [LBP6200]='MTPA00001 MTPA99999' \
  44. [LBP6310]='MTLA002001 MTLA999999' [LBP7010C]='MTQA00001 MTQA99999' \
  45. [LBP7018C]='MTRA00001 MTRA99999' [LBP7210C]='MTKA002001 MTKA999999')
  46. #OS architecture
  47. if [ "$(uname -m)" == 'x86_64' ]; then
  48. ARCH='amd64'
  49. else
  50. ARCH='i386'
  51. fi
  52. #Determine the init system
  53. if [[ $(ps -p1 | grep systemd) ]]; then
  54. INIT_SYSTEM='systemd'
  55. else
  56. INIT_SYSTEM='upstart'
  57. fi
  58. #Move to the current directory
  59. cd "$(dirname "$0")"
  60. function valid_ip() {
  61. local ip=$1
  62. local stat=1
  63. if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  64. ip=($(echo "$ip" | tr '.' ' '))
  65. [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  66. stat=$?
  67. fi
  68. return $stat
  69. }
  70. function check_error() {
  71. if [ $2 -ne 0 ]; then
  72. case $1 in
  73. 'WGET') echo "Error while downloading file $3"
  74. [ -n "$3" ] && [ -f "$3" ] && rm "$3";;
  75. 'PACKAGE') echo "Error installing package $3";;
  76. *) echo 'Error';;
  77. esac
  78. echo 'Press any key to exit'
  79. read -s -n1
  80. exit 1
  81. fi
  82. }
  83. function canon_uninstall() {
  84. if [ -f /usr/sbin/ccpdadmin ]; then
  85. installed_model=$(ccpdadmin | grep LBP | awk '{print $3}')
  86. if [ -n "$installed_model" ]; then
  87. echo "Found printer $installed_model"
  88. echo "Closing captstatusui"
  89. killall captstatusui 2> /dev/null
  90. echo 'Stopping ccpd'
  91. service ccpd stop
  92. echo 'Removing the printer from the ccpd daemon configuration file'
  93. ccpdadmin -x $installed_model
  94. echo 'Removing the printer from CUPS'
  95. lpadmin -x $installed_model
  96. fi
  97. fi
  98. echo 'Removing driver packages'
  99. dpkg --purge cndrvcups-capt
  100. dpkg --purge cndrvcups-common
  101. echo 'Removing unused libraries and packages'
  102. apt-get -y autoremove
  103. echo 'Deleting settings'
  104. [ -f /etc/init/ccpd-start.conf ] && rm /etc/init/ccpd-start.conf
  105. [ -f /etc/udev/rules.d/85-canon-capt.rules ] && rm /etc/udev/rules.d/85-canon-capt.rules
  106. [ -f "${XDG_DESKTOP_DIR}/captstatusui.desktop" ] && rm "${XDG_DESKTOP_DIR}/captstatusui.desktop"
  107. [ -f /usr/bin/autoshutdowntool ] && rm /usr/bin/autoshutdowntool
  108. [ $INIT_SYSTEM == 'systemd' ] && update-rc.d -f ccpd remove
  109. echo 'Uninstall completed'
  110. echo 'Press any key to exit'
  111. read -s -n1
  112. return 0
  113. }
  114. function canon_install() {
  115. echo
  116. PS3='Please choose your printer: '
  117. select NAMEPRINTER in $NAMESPRINTERS
  118. do
  119. [ -n "$NAMEPRINTER" ] && break
  120. done
  121. echo "Selected printer: $NAMEPRINTER"
  122. echo
  123. PS3='How is the printer connected to the computer: '
  124. select CONECTION in 'Via USB' 'Through network (LAN, NET)'
  125. do
  126. if [ "$REPLY" == "1" ]; then
  127. CONECTION="usb"
  128. while true
  129. do
  130. #Looking for a device connected to the USB port
  131. NODE_DEVICE=$(ls -1t /dev/usb/lp* 2> /dev/null | head -1)
  132. if [ -n "$NODE_DEVICE" ]; then
  133. #Find the serial number of that device
  134. PRINTER_SERIAL=$(udevadm info --attribute-walk --name=$NODE_DEVICE | sed '/./{H;$!d;};x;/ATTRS{product}=="Canon CAPT USB \(Device\|Printer\)"/!d;' | awk -F'==' '/ATTRS{serial}/{print $2}')
  135. #If the serial number is found, that device is a Canon printer
  136. [ -n "$PRINTER_SERIAL" ] && break
  137. fi
  138. echo -ne "Turn on the printer and plug in USB cable\r"
  139. sleep 2
  140. done
  141. PATH_DEVICE="/dev/canon$NAMEPRINTER"
  142. break
  143. elif [ "$REPLY" == "2" ]; then
  144. CONECTION="lan"
  145. read -p 'Enter the IP address of the printer: ' IP_ADDRES
  146. until valid_ip "$IP_ADDRES"
  147. do
  148. echo 'Invalid IP address format, enter four decimal numbers'
  149. echo -n 'from 0 to 255, separated by dots: '
  150. read IP_ADDRES
  151. done
  152. PATH_DEVICE="net:$IP_ADDRES"
  153. echo 'Turn on the printer and press any key'
  154. read -s -n1
  155. sleep 5
  156. break
  157. fi
  158. done
  159. echo '************Driver Installation************'
  160. COMMON_FILE=cndrvcups-common_${DRIVER_VERSION_COMMON}_${ARCH}.deb
  161. CAPT_FILE=cndrvcups-capt_${DRIVER_VERSION}_${ARCH}.deb
  162. if [ ! -f $COMMON_FILE ]; then
  163. sudo -u $LOGIN_USER wget -O $COMMON_FILE ${URL_DRIVER[${ARCH}_common]}
  164. check_error WGET $? $COMMON_FILE
  165. fi
  166. if [ ! -f $CAPT_FILE ]; then
  167. sudo -u $LOGIN_USER wget -O $CAPT_FILE ${URL_DRIVER[${ARCH}_capt]}
  168. check_error WGET $? $CAPT_FILE
  169. fi
  170. apt-get -y update
  171. apt-get -y install libglade2-0 libcanberra-gtk-module
  172. check_error PACKAGE $?
  173. echo 'Installing common module for CUPS driver'
  174. dpkg -i $COMMON_FILE
  175. check_error PACKAGE $? $COMMON_FILE
  176. echo 'Installing CAPT Printer Driver Module'
  177. dpkg -i $CAPT_FILE
  178. check_error PACKAGE $? $CAPT_FILE
  179. #Replace /etc/init.d/ccpd
  180. echo '#!/bin/bash
  181. # startup script for Canon Printer Daemon for CUPS (ccpd)
  182. ### BEGIN INIT INFO
  183. # Provides: ccpd
  184. # Required-Start: $local_fs $remote_fs $syslog $network $named
  185. # Should-Start: $ALL
  186. # Required-Stop: $syslog $remote_fs
  187. # Default-Start: 2 3 4 5
  188. # Default-Stop: 0 1 6
  189. # Description: Start Canon Printer Daemon for CUPS
  190. ### END INIT INFO
  191. # If the CUPS print server is not running, wait until it starts
  192. if [ `ps awx | grep cupsd | grep -v grep | wc -l` -eq 0 ]; then
  193. while [ `ps awx | grep cupsd | grep -v grep | wc -l` -eq 0 ]
  194. do
  195. sleep 3
  196. done
  197. sleep 5
  198. fi
  199. ccpd_start ()
  200. {
  201. echo -n "Starting ${DAEMON}: "
  202. start-stop-daemon --start --quiet --oknodo --exec ${DAEMON}
  203. }
  204. ccpd_stop ()
  205. {
  206. echo -n "Shutting down ${DAEMON}: "
  207. start-stop-daemon --stop --quiet --oknodo --retry TERM/30/KILL/5 --exec ${DAEMON}
  208. }
  209. DAEMON=/usr/sbin/ccpd
  210. case $1 in
  211. start)
  212. ccpd_start
  213. ;;
  214. stop)
  215. ccpd_stop
  216. ;;
  217. status)
  218. echo "${DAEMON}:" $(pidof ${DAEMON})
  219. ;;
  220. restart)
  221. while true
  222. do
  223. ccpd_stop
  224. ccpd_start
  225. # if the ccpd process does not appear after 5 seconds, we restart it again
  226. for (( i = 1 ; i <= 5 ; i++ ))
  227. do
  228. sleep 1
  229. set -- $(pidof ${DAEMON})
  230. [ -n "$1" -a -n "$2" ] && exit 0
  231. done
  232. done
  233. ;;
  234. *)
  235. echo "Usage: ccpd {start|stop|status|restart}"
  236. exit 1
  237. ;;
  238. esac
  239. exit 0' > /etc/init.d/ccpd
  240. #Installation utilities for managing AppArmor
  241. apt-get -y install apparmor-utils
  242. #Set AppArmor security profile for cupsd to complain mode
  243. aa-complain /usr/sbin/cupsd
  244. echo 'Restarting CUPS'
  245. service cups restart
  246. if [ $ARCH == 'amd64' ]; then
  247. echo 'Installing 32-bit libraries required to run 64-bit printer driver'
  248. apt-get -y install libatk1.0-0:i386 libcairo2:i386 libgtk2.0-0:i386 libpango1.0-0:i386 libstdc++6:i386 libpopt0:i386 libxml2:i386 libc6:i386
  249. check_error PACKAGE $?
  250. fi
  251. echo 'Installing the printer in CUPS'
  252. /usr/sbin/lpadmin -p $NAMEPRINTER -P /usr/share/cups/model/CNCUPSLBP${LASERSHOT[$NAMEPRINTER]}CAPTK.ppd -v ccp://localhost:59687 -E
  253. echo "Setting $NAMEPRINTER as the default printer"
  254. /usr/sbin/lpadmin -d $NAMEPRINTER
  255. echo 'Registering the printer in the ccpd daemon configuration file'
  256. /usr/sbin/ccpdadmin -p $NAMEPRINTER -o $PATH_DEVICE
  257. #Verify printer installation
  258. installed_printer=$(ccpdadmin | grep $NAMEPRINTER | awk '{print $3}')
  259. if [ -n "$installed_printer" ]; then
  260. if [ "$CONECTION" == "usb" ]; then
  261. echo 'Creating a rule for the printer'
  262. #A rule is created to provides an alternative name (a symbolic link) to our printer so as not to depend on the changing values of lp0, lp1,...
  263. echo 'KERNEL=="lp[0-9]*", SUBSYSTEMS=="usb", ATTRS{serial}=='$PRINTER_SERIAL', SYMLINK+="canon'$NAMEPRINTER'"' > /etc/udev/rules.d/85-canon-capt.rules
  264. #Update the rules
  265. udevadm control --reload-rules
  266. #Check the created rule
  267. until [ -e $PATH_DEVICE ]
  268. do
  269. echo -ne "Turn off the printer, wait 2 seconds, then turn on the printer\r"
  270. sleep 2
  271. done
  272. fi
  273. echo -e "\e[2KRunning ccpd"
  274. service ccpd restart
  275. #Autoload ccpd
  276. if [ $INIT_SYSTEM == 'systemd' ]; then
  277. update-rc.d ccpd defaults
  278. else
  279. echo 'description "Canon Printer Daemon for CUPS (ccpd)"
  280. author "LinuxMania <customer@linuxmania.jp>"
  281. start on (started cups and runlevel [2345])
  282. stop on runlevel [016]
  283. expect fork
  284. respawn
  285. exec /usr/sbin/ccpd start' > /etc/init/ccpd-start.conf
  286. fi
  287. #Create captstatusui shortcut on desktop
  288. echo '#!/usr/bin/env xdg-open
  289. [Desktop Entry]
  290. Version=1.0
  291. Name='$NAMEPRINTER'
  292. GenericName=Status monitor for Canon CAPT Printer
  293. Exec=captstatusui -P '$NAMEPRINTER'
  294. Terminal=false
  295. Type=Application
  296. Icon=/usr/share/icons/Humanity/devices/48/printer.svg' > "${XDG_DESKTOP_DIR}/$NAMEPRINTER.desktop"
  297. chmod 775 "${XDG_DESKTOP_DIR}/$NAMEPRINTER.desktop"
  298. chown $LOGIN_USER:$LOGIN_USER "${XDG_DESKTOP_DIR}/$NAMEPRINTER.desktop"
  299. #Install autoshutdowntool for supported models
  300. if [[ "${!ASDT_SUPPORTED_MODELS[@]}" =~ "$NAMEPRINTER" ]]; then
  301. SERIALRANGE=(${ASDT_SUPPORTED_MODELS[$NAMEPRINTER]})
  302. SERIALMIN=${SERIALRANGE[0]}
  303. SERIALMAX=${SERIALRANGE[1]}
  304. if [[ ${#PRINTER_SERIAL} -eq ${#SERIALMIN} && $PRINTER_SERIAL > $SERIALMIN && $PRINTER_SERIAL < $SERIALMAX || $PRINTER_SERIAL == $SERIALMIN || $PRINTER_SERIAL == $SERIALMAX ]]; then
  305. echo "Installing the autoshutdowntool utility"
  306. ASDT_FILE=autoshutdowntool_1.00-1_${ARCH}_deb.tar.gz
  307. if [ ! -f $ASDT_FILE ]; then
  308. wget -O $ASDT_FILE ${URL_ASDT[$ARCH]}
  309. check_error WGET $? $ASDT_FILE
  310. fi
  311. tar --gzip --extract --file=$ASDT_FILE --totals --directory=/usr/bin
  312. fi
  313. fi
  314. #Start captstatusui
  315. if [[ -n "$DISPLAY" ]] ; then
  316. sudo -u $LOGIN_USER nohup captstatusui -P $NAMEPRINTER > /dev/null 2>&1 &
  317. sleep 5
  318. fi
  319. echo 'Installation completed. Press any key to exit'
  320. read -s -n1
  321. exit 0
  322. else
  323. echo 'Driver for $NAMEPRINTER is not installed!'
  324. echo 'Press any key to exit'
  325. read -s -n1
  326. exit 1
  327. fi
  328. }
  329. function canon_help {
  330. clear
  331. echo 'Installation Notes
  332. If you have already installed driver for this series,
  333. uninstall it before using this script.
  334. If the driver packages are not found, they will be automatically
  335. downloaded from the Internet and saved in the script folder.
  336. To update the driver, first uninstall the old version using this script,
  337. then install a new one.
  338. Notes on printing problems:
  339. If the printer stops printing, run captstatusui via the shortcut
  340. on desktop or from terminal: captstatusui -P <printer_name>
  341. The captstatusui window shows the current status of the printer.
  342. If an error occurs, its description is displayed.
  343. Here you can try pressing button "Resume Job" to continue printing
  344. or "Cancel Job" button to cancel the job.
  345. If this does not help, try running canon_restart.sh
  346. Printer configuration command: cngplp
  347. Additional settings command: captstatusui -P <printer_name>
  348. Turn on auto-off (not for all models): autoshutdowntool
  349. To log the installation process, run the script like this:
  350. logsave log.txt ./canon_lbp_setup.sh
  351. '
  352. }
  353. clear
  354. echo 'Installing the Linux CAPT Printer Driver v'${DRIVER_VERSION}' for Canon LBP printers on Ubuntu (both 32-bit and 64-bit)
  355. Supported printers:'
  356. echo "$NAMESPRINTERS" | sed ':a; /$/N; s/\n/, /; ta' | fold -s
  357. PS3='Please enter your choice: '
  358. select opt in 'Install' 'Uninstall' 'Help' 'Exit'
  359. do
  360. if [ "$opt" == 'Install' ]; then
  361. canon_install
  362. break
  363. elif [ "$opt" == 'Uninstall' ]; then
  364. canon_uninstall
  365. break
  366. elif [ "$opt" == 'Help' ]; then
  367. canon_help
  368. elif [ "$opt" == 'Exit' ]; then
  369. break
  370. fi
  371. done