322 lines
11 KiB
Bash
322 lines
11 KiB
Bash
#!/bin/bash
|
|
# =============================================================================
|
|
# setup-printer.sh - Configuration CUPS + Canon Selphy CP1300
|
|
# =============================================================================
|
|
# Usage: sudo bash setup-printer.sh
|
|
# Prerequis: imprimante branchee en USB et allumee
|
|
# =============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m'
|
|
|
|
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
|
|
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
|
|
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
log_error "Ce script doit etre execute en root (sudo)"
|
|
exit 1
|
|
fi
|
|
|
|
echo "============================================="
|
|
echo " Configuration imprimante Canon Selphy CP1300"
|
|
echo "============================================="
|
|
echo ""
|
|
|
|
# -------------------------------------------------------------------
|
|
# 1. INSTALLATION DES PAQUETS
|
|
# -------------------------------------------------------------------
|
|
log_info "Installation des paquets necessaires..."
|
|
|
|
apt-get update -qq
|
|
apt-get install -y -qq \
|
|
cups \
|
|
printer-driver-gutenprint \
|
|
cups-client \
|
|
system-config-printer \
|
|
2>/dev/null || apt-get install -y -qq cups printer-driver-gutenprint cups-client
|
|
|
|
# Ajouter l'utilisateur au groupe lpadmin pour administrer CUPS
|
|
REAL_USER="${SUDO_USER:-$(logname 2>/dev/null || echo pi)}"
|
|
usermod -aG lpadmin "$REAL_USER" 2>/dev/null || true
|
|
log_info "Utilisateur $REAL_USER ajoute au groupe lpadmin"
|
|
|
|
# -------------------------------------------------------------------
|
|
# 2. CONFIGURATION CUPS
|
|
# -------------------------------------------------------------------
|
|
log_info "Configuration de CUPS..."
|
|
|
|
# Backup de la config originale
|
|
cp /etc/cups/cupsd.conf /etc/cups/cupsd.conf.backup.$(date +%Y%m%d) 2>/dev/null || true
|
|
|
|
# Autoriser l'acces local au panneau CUPS
|
|
# (utile pour debug mais pas necessaire en production)
|
|
cat > /etc/cups/cupsd.conf << 'EOF'
|
|
# Configuration CUPS optimisee pour photomaton
|
|
LogLevel warn
|
|
MaxLogSize 0
|
|
PageLogFormat
|
|
|
|
# Ecouter sur localhost et le reseau local
|
|
Listen localhost:631
|
|
Listen /run/cups/cups.sock
|
|
|
|
# Partage d'imprimante desactive (pas besoin)
|
|
Browsing Off
|
|
BrowseLocalProtocols none
|
|
DefaultAuthType Basic
|
|
WebInterface Yes
|
|
|
|
# Politique par defaut
|
|
<Location />
|
|
Order allow,deny
|
|
Allow localhost
|
|
Allow 10.0.0.*
|
|
Allow 192.168.*
|
|
</Location>
|
|
|
|
<Location /admin>
|
|
Order allow,deny
|
|
Allow localhost
|
|
</Location>
|
|
|
|
<Location /admin/conf>
|
|
AuthType Default
|
|
Require user @SYSTEM
|
|
Order allow,deny
|
|
Allow localhost
|
|
</Location>
|
|
|
|
<Policy default>
|
|
JobPrivateAccess default
|
|
JobPrivateValues default
|
|
SubscriptionPrivateAccess default
|
|
SubscriptionPrivateValues default
|
|
|
|
<Limit Create-Job Print-Job Print-URI Validate-Job>
|
|
Order deny,allow
|
|
</Limit>
|
|
|
|
<Limit Send-Document Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs Set-Job-Attributes Create-Job-Subscription Renew-Subscription Cancel-Subscription Get-Notifications Reprocess-Job Cancel-Current-Job Suspend-Current-Job Resume-Job Cancel-My-Jobs Close-Job CUPS-Move-Job CUPS-Get-Document>
|
|
Require user @OWNER @SYSTEM
|
|
Order deny,allow
|
|
</Limit>
|
|
|
|
<Limit CUPS-Add-Modify-Printer CUPS-Delete-Printer CUPS-Add-Modify-Class CUPS-Delete-Class CUPS-Set-Default CUPS-Get-Devices>
|
|
AuthType Default
|
|
Require user @SYSTEM
|
|
Order deny,allow
|
|
</Limit>
|
|
|
|
<Limit Pause-Printer Resume-Printer Enable-Printer Disable-Printer Pause-Printer-After-Current-Job Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer Activate-Printer Restart-Printer Shutdown-Printer Startup-Printer Promote-Job Schedule-Job-After Cancel-Jobs CUPS-Accept-Jobs CUPS-Reject-Jobs>
|
|
AuthType Default
|
|
Require user @SYSTEM
|
|
Order deny,allow
|
|
</Limit>
|
|
|
|
<Limit All>
|
|
Order deny,allow
|
|
</Limit>
|
|
</Policy>
|
|
EOF
|
|
|
|
systemctl restart cups
|
|
systemctl enable cups
|
|
log_info "CUPS configure et redemarre"
|
|
|
|
# -------------------------------------------------------------------
|
|
# 3. DETECTION DE L'IMPRIMANTE
|
|
# -------------------------------------------------------------------
|
|
log_info "Recherche des imprimantes Canon Selphy..."
|
|
|
|
sleep 3 # Laisser CUPS detecter les USB
|
|
|
|
# Chercher les imprimantes USB Canon
|
|
USB_PRINTERS=$(lpinfo -v 2>/dev/null | grep -i "canon\|selphy\|usb" || true)
|
|
|
|
if [ -z "$USB_PRINTERS" ]; then
|
|
log_warn "Aucune imprimante Canon detectee via USB."
|
|
log_warn "Verifiez que l'imprimante est branchee, allumee, et prete."
|
|
echo ""
|
|
echo "Imprimantes detectees:"
|
|
lpinfo -v 2>/dev/null || true
|
|
echo ""
|
|
echo "Vous pouvez relancer ce script une fois l'imprimante connectee,"
|
|
echo "ou ajouter manuellement via: http://localhost:631"
|
|
echo ""
|
|
else
|
|
echo ""
|
|
echo -e "${CYAN}Imprimantes detectees:${NC}"
|
|
echo "$USB_PRINTERS"
|
|
echo ""
|
|
fi
|
|
|
|
# Chercher le driver Gutenprint pour Canon Selphy
|
|
log_info "Recherche du driver Canon Selphy CP1300..."
|
|
DRIVER=$(lpinfo -m 2>/dev/null | grep -i "selphy\|cp1300\|CP1300" | head -1 || true)
|
|
|
|
if [ -z "$DRIVER" ]; then
|
|
# Essayer avec un pattern plus large
|
|
DRIVER=$(lpinfo -m 2>/dev/null | grep -i "canon.*cp" | head -1 || true)
|
|
fi
|
|
|
|
if [ -n "$DRIVER" ]; then
|
|
DRIVER_URI=$(echo "$DRIVER" | awk '{print $1}')
|
|
log_info "Driver trouve: $DRIVER"
|
|
else
|
|
log_warn "Driver Selphy CP1300 non trouve dans Gutenprint."
|
|
log_warn "Essayez: sudo apt install printer-driver-gutenprint && sudo systemctl restart cups"
|
|
DRIVER_URI=""
|
|
fi
|
|
|
|
# -------------------------------------------------------------------
|
|
# 4. AJOUT AUTOMATIQUE DE L'IMPRIMANTE (si detectee)
|
|
# -------------------------------------------------------------------
|
|
USB_URI=$(echo "$USB_PRINTERS" | grep "usb://" | head -1 | awk '{print $2}' || true)
|
|
|
|
if [ -n "$USB_URI" ] && [ -n "$DRIVER_URI" ]; then
|
|
log_info "Ajout automatique de l'imprimante..."
|
|
|
|
# Supprimer les anciennes configs si elles existent
|
|
lpadmin -x Canon_SELPHY_CP1300_usb_blanche 2>/dev/null || true
|
|
lpadmin -x Canon_SELPHY_CP1300_usb_noire 2>/dev/null || true
|
|
|
|
# Ajouter la premiere imprimante (blanche)
|
|
lpadmin -p Canon_SELPHY_CP1300_usb_blanche \
|
|
-v "$USB_URI" \
|
|
-m "$DRIVER_URI" \
|
|
-L "Photomaton" \
|
|
-D "Canon Selphy CP1300 Blanche" \
|
|
-E
|
|
|
|
# Configurer comme imprimante par defaut
|
|
lpadmin -d Canon_SELPHY_CP1300_usb_blanche
|
|
|
|
log_info "Imprimante 'Canon_SELPHY_CP1300_usb_blanche' ajoutee et definie par defaut"
|
|
|
|
# Detecter une deuxieme imprimante si presente
|
|
USB_URI_2=$(echo "$USB_PRINTERS" | grep "usb://" | sed -n '2p' | awk '{print $2}' || true)
|
|
if [ -n "$USB_URI_2" ]; then
|
|
lpadmin -p Canon_SELPHY_CP1300_usb_noire \
|
|
-v "$USB_URI_2" \
|
|
-m "$DRIVER_URI" \
|
|
-L "Photomaton" \
|
|
-D "Canon Selphy CP1300 Noire" \
|
|
-E
|
|
log_info "Deuxieme imprimante 'Canon_SELPHY_CP1300_usb_noire' ajoutee"
|
|
fi
|
|
else
|
|
log_warn "Ajout automatique impossible. Ajoutez manuellement via http://localhost:631"
|
|
fi
|
|
|
|
# -------------------------------------------------------------------
|
|
# 5. OPTIONS D'IMPRESSION OPTIMALES POUR SELPHY CP1300
|
|
# -------------------------------------------------------------------
|
|
log_info "Configuration des options d'impression..."
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cat > "${SCRIPT_DIR}/../config/cups-selphy.conf" << 'EOF'
|
|
# =============================================================================
|
|
# Options d'impression pour Canon Selphy CP1300
|
|
# =============================================================================
|
|
# Format: lpoptions -p PRINTER_NAME -o OPTION=VALUE
|
|
#
|
|
# La Selphy CP1300 supporte:
|
|
# - Format carte postale : 100x148mm (4x6 pouces)
|
|
# - Format carte : 54x86mm
|
|
# - Format carre : 89x89mm (si media disponible)
|
|
#
|
|
# Noms CUPS des imprimantes:
|
|
# Canon_SELPHY_CP1300_usb_blanche
|
|
# Canon_SELPHY_CP1300_usb_noire
|
|
#
|
|
# Commande d'impression pour photobooth-app:
|
|
# lp -d Canon_SELPHY_CP1300_usb_blanche -o landscape -o fit-to-page {filename}
|
|
#
|
|
# Options utilisees:
|
|
# - fit-to-page : Ajuster l'image a la page sans crop
|
|
# - landscape : Orientation paysage
|
|
# - -n X : Nombre de copies
|
|
#
|
|
# Pour le load balancing entre 2 imprimantes:
|
|
# Utiliser script_print.sh (gere automatiquement)
|
|
# =============================================================================
|
|
EOF
|
|
|
|
# Appliquer les options par defaut si les imprimantes sont configurees
|
|
if lpstat -p Canon_SELPHY_CP1300_usb_blanche &>/dev/null; then
|
|
lpoptions -p Canon_SELPHY_CP1300_usb_blanche -o fit-to-page -o landscape 2>/dev/null || true
|
|
log_info "Options par defaut appliquees a Canon_SELPHY_CP1300_usb_blanche"
|
|
fi
|
|
|
|
if lpstat -p Canon_SELPHY_CP1300_usb_noire &>/dev/null; then
|
|
lpoptions -p Canon_SELPHY_CP1300_usb_noire -o fit-to-page -o landscape 2>/dev/null || true
|
|
log_info "Options par defaut appliquees a Canon_SELPHY_CP1300_usb_noire"
|
|
fi
|
|
|
|
# -------------------------------------------------------------------
|
|
# 6. COPIER LE SCRIPT D'IMPRESSION OPTIMISE
|
|
# -------------------------------------------------------------------
|
|
log_info "Verification du script d'impression..."
|
|
|
|
if [ -f "${SCRIPT_DIR}/script_print.sh" ]; then
|
|
log_info "script_print.sh present dans le projet"
|
|
log_info "Pour deployer: cp ${SCRIPT_DIR}/script_print.sh /home/pi/photobooth-data/script/"
|
|
else
|
|
log_warn "script_print.sh non trouve dans ${SCRIPT_DIR}/"
|
|
fi
|
|
|
|
# -------------------------------------------------------------------
|
|
# 7. SCRIPT DE NETTOYAGE AUTOMATIQUE DES JOBS BLOQUES
|
|
# -------------------------------------------------------------------
|
|
log_info "Creation du script de nettoyage jobs CUPS..."
|
|
|
|
cat > "${SCRIPT_DIR}/cups-cleanup.sh" << 'CLEANUP_EOF'
|
|
#!/bin/bash
|
|
# Nettoyer les jobs d'impression bloques/en erreur
|
|
# A appeler periodiquement via cron ou le watchdog
|
|
|
|
# Annuler tous les jobs en erreur
|
|
cancel -a 2>/dev/null || true
|
|
|
|
# Reactiver les imprimantes si elles sont en pause/erreur
|
|
for printer in $(lpstat -p 2>/dev/null | grep -E "disabled|stopped" | awk '{print $2}'); do
|
|
cupsenable "$printer" 2>/dev/null || true
|
|
cupsdisable --release "$printer" 2>/dev/null || true
|
|
cupsenable "$printer" 2>/dev/null || true
|
|
echo "Imprimante reactivee: $printer"
|
|
done
|
|
CLEANUP_EOF
|
|
|
|
chmod +x "${SCRIPT_DIR}/cups-cleanup.sh"
|
|
log_info "Script cups-cleanup.sh cree"
|
|
|
|
# -------------------------------------------------------------------
|
|
# RESUME
|
|
# -------------------------------------------------------------------
|
|
echo ""
|
|
echo "============================================="
|
|
echo " Configuration imprimante terminee !"
|
|
echo "============================================="
|
|
echo ""
|
|
echo "Imprimantes configurees:"
|
|
lpstat -p 2>/dev/null || echo " (aucune)"
|
|
echo ""
|
|
echo "Commande d'impression pour photobooth-app:"
|
|
echo " script_print.sh \"{filename}\" \"{media_type}\" \"{action_config_name}\" \"{copies}\""
|
|
echo ""
|
|
echo "Imprimantes CUPS:"
|
|
echo " Canon_SELPHY_CP1300_usb_blanche"
|
|
echo " Canon_SELPHY_CP1300_usb_noire"
|
|
echo ""
|
|
echo "Administration CUPS: http://localhost:631"
|
|
echo ""
|
|
echo "Pour tester l'impression:"
|
|
echo " lp -d Canon_SELPHY_CP1300_usb_blanche -o landscape -o fit-to-page /chemin/vers/une/photo.jpg"
|
|
echo ""
|