#!/bin/sh
# File: devuan-printer.sh
# Location: /mnt/public/Support/Platform/printer/
# Author: bgstack15
# SPDX-License-Identifier: GPL-3.0-only
# Startdate: 2024-02-08-5 08:15
# Title: Install my printer on Devuan
# Purpose: oneliner install printer
# History:
# Usage:
# Reference:
#    https://www.cups.org/doc/admin.html
#    https://serverfault.com/questions/1011996/cups-how-to-list-all-detected-printers-from-command-line-linux
#    http://localhost:631/
#    https://bgstack15.ddns.net/blog/posts/2022/11/17/use-remote-printer-driver-for-cups-shared-printer-in-cups-client/"
#    man lpadmin
# Improve:
# Dependencies:
#    run on Devuan. Sudo access.
# Documentation:
#    if you need printing with `lpr -P ml1865w /path/to/file`, then install cups-bsd

# install cups
if ! dpkg -l | awk '$2=="cups" && $1~/^i./' 1>/dev/null ;
then
   sudo apt-get update
   sudo apt-get install --no-install-recommends -y cups
fi

ensure_printer() {
   _name="${1}"
   _url="${2}"
   _location="${3}"
   status="$( sudo lpstat -v )"
   if ! echo "${status}" | grep -qiE "${_url}"  ;
   then
      sudo lpadmin -p "${_name}" -E -v "${_url}" -L "${_location}"
   fi
}

# printer list
ensure_printer "ml1865" "ipp://dns2.ipa.internal.com:631/printers/ml1865" "computer room"

# display status
sudo lpstat -v
