-
Notifications
You must be signed in to change notification settings - Fork 0
/
ippes.sh
executable file
·141 lines (130 loc) · 4.37 KB
/
ippes.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
# shellcheck disable=SC2034,SC1091,SC2086
# IPP Everywhere sucks
# A small project to allow old printers to be installed without IPP Everywhere.
#
# Made by Jiab77 - 2023
#
# Version 0.0.0
# Options
[[ -r $HOME/.debug ]] && set -o xtrace || set +o xtrace
# Colors
NC="\033[0m"
NL="\n"
BLUE="\033[1;34m"
YELLOW="\033[1;33m"
GREEN="\033[1;32m"
RED="\033[1;31m"
WHITE="\033[1;37m"
PURPLE="\033[1;35m"
# Config
DEBUG_MODE=false
DRY_RUN=false
REMOVE_ALL=false
# Functions
function get_version() {
grep -i 'version' "$0" | awk '{ print $3 }' | head -n1
}
function show_version() {
echo -e "\nVersion: $(get_version)\n" ; exit
}
function show_usage() {
echo -e "\nUsage: $(basename "$0") <ACTION> [OPTIONS]\n"
echo -e "Action:\n"
echo -e "show\tShow installed printers."
echo -e "\nOptions:\n"
echo -e "-h|--help\tShow this message."
echo -e "-v|--version\tShow script version."
echo -e "-d|--debug\tEnable debug mode."
echo -e "-a|--all\tRemove all installed printers."
echo -e "-n|--dry-run\tSimulate changes, don't apply them."
echo -e "\nDisclaimer:\n\n/!\ This script is still experimental so use it with caution. /!\ \n"
exit
}
function die() {
echo -e "\nError: $*\n" >&2
exit 255
}
function init_ippes() {
case $IPPES_ACTION in
"show") ippes_show ;;
"remove") ippes_remove ;;
esac
}
function ippes_show() {
echo -e "${NL}${WHITE}Listing installed printers...${NC}${NL}"
lpstat -t
echo -e "${NL}${WHITE}Done.${NC}${NL}"
}
function ippes_remove() {
local PRINTERS_LIST=()
local PRINTERS_FOUND=0
local PRINTER_INDEX=0
local OLD_IFS=$IFS
if [[ $REMOVE_ALL == true ]]; then
echo -e "${NL}${WHITE}Removing ${YELLOW}all${WHITE} installed printers...${NC}${NL}"
for P in $(lpstat -e) ; do lpadmin -x $P ; done
echo -e "${NL}${WHITE}Done.${NC}${NL}"
else
echo ; read -rp "Define printer brand, model or name: " PRINTER
if [[ -z $PRINTER ]]; then
die "Printer not defined."
else
echo -e "${NL}${WHITE}Searching for printer '${BLUE}$PRINTER${WHITE}'...${NC}${NL}"
# PRINTERS_FOUND=$(lpinfo --make-and-model "$PRINTER" -m | sort | uniq | wc -l)
PRINTERS_FOUND=$(lpstat -p | grep -ci "$PRINTER")
[[ $PRINTERS_FOUND -eq 0 ]] && PRINTERS_FOUND=$(lpstat -lp | grep -i "$PRINTER" | grep -ci "printer")
if [[ $PRINTERS_FOUND -eq 0 ]]; then
die "Could not find any printer brands, models or names that contains '$PRINTER'."
else
if [[ $PRINTERS_FOUND -eq 1 ]]; then
echo -e "${NL}${WHITE}Removing installed printer '${BLUE}$PRINTER${WHITE}'...${NC}${NL}"
lpadmin -x $PRINTER
echo -e "${NL}${WHITE}Done.${NC}${NL}"
else
IFS=$'\n'
for P in $(lpstat -p | awk '{ print $2 }' | sed -e 's/_/ /g') ; do
((PRINTER_INDEX++))
echo "${PRINTER_INDEX}. $P"
PRINTERS_LIST+=("$P")
done
IFS=$OLD_IFS
echo ; read -rp "Select the printer you want to remove: " REMOVE_PRINTER
if [[ -z $REMOVE_PRINTER ]]; then
die "Printer not defined."
elif [[ -z ${PRINTERS_LIST[((REMOVE_PRINTER-1))]} ]]; then
die "Printer not found."
else
echo -e "${NL}${WHITE}Selected printer: ${BLUE}${PRINTERS_LIST[((REMOVE_PRINTER-1))]}${NC}${NL}"
fi
fi
fi
fi
fi
}
# Script header
echo -e "${NL}${WHITE}IPP ${YELLOW}Everywhere ${PURPLE}sucks${BLUE} - Manage old printers without ${WHITE}IPP ${YELLOW}Everywhere${BLUE}${NC}"
# Checks
[[ $# -eq 0 ]] && show_usage
# Arguments
INDEX=0
for ARG in "$@"; do
if [[ $DEBUG_MODE == true ]]; then
echo "Arg $((INDEX++)): $ARG"
fi
case $ARG in
"show"|"remove")
IPPES_ACTION="$ARG"
;;
"-h"|"--help") show_usage ;;
"-v"|"--version") show_version ;;
"-d"|"--debug") DEBUG_MODE=true ;;
"-a"|"--all") REMOVE_ALL=true ;;
"-n"|"--dry-run") DRY_RUN=true ;;
*)
die "Unsupported argument given: $ARG"
;;
esac
done
# Main
init_ippes