-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·160 lines (137 loc) · 4.75 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·160 lines (137 loc) · 4.75 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
set -e
# POS Thermal Printer — setup script
# Works on macOS and Raspberry Pi (Debian/Ubuntu)
#
# Usage:
# chmod +x setup.sh
# ./setup.sh # install deps + auto-start service
# ./setup.sh --deps-only # just install dependencies, no service
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SERVICE_NAME="pos-printer"
DEPS_ONLY=false
if [ "$1" = "--deps-only" ]; then
DEPS_ONLY=true
fi
OS="$(uname -s)"
echo "==> Detected OS: $OS"
echo "==> Project dir: $SCRIPT_DIR"
# --- Install system dependencies ---
if [ "$OS" = "Linux" ]; then
echo "==> Installing system packages..."
sudo apt-get update -qq
sudo apt-get install -y python3 python3-pip python3-venv libusb-1.0-0-dev fonts-dejavu-core locales
# Generate UTF-8 locale to avoid locale warnings
if ! locale -a 2>/dev/null | grep -q "en_US.utf8"; then
echo "==> Generating en_US.UTF-8 locale..."
sudo sed -i 's/^# *en_US.UTF-8/en_US.UTF-8/' /etc/locale.gen
sudo locale-gen en_US.UTF-8
fi
# udev rule so the printer is accessible without root
UDEV_RULE='/etc/udev/rules.d/99-thermal-printer.rules'
if [ ! -f "$UDEV_RULE" ]; then
echo "==> Adding USB permission rule for thermal printer..."
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="1fc9", ATTR{idProduct}=="2016", MODE="0666"' \
| sudo tee "$UDEV_RULE" > /dev/null
sudo udevadm control --reload-rules
sudo udevadm trigger
fi
# Blacklist usblp kernel module so it doesn't grab the printer
BLACKLIST='/etc/modprobe.d/no-usblp.conf'
if [ ! -f "$BLACKLIST" ]; then
echo "==> Blacklisting usblp kernel module..."
echo 'blacklist usblp' | sudo tee "$BLACKLIST" > /dev/null
sudo rmmod usblp 2>/dev/null || true
fi
elif [ "$OS" = "Darwin" ]; then
echo "==> macOS: checking for python3..."
if ! command -v python3 &>/dev/null; then
echo "ERROR: python3 not found. Install it via: brew install python"
exit 1
fi
fi
# --- Python virtual environment + dependencies ---
VENV="$SCRIPT_DIR/venv"
if [ ! -d "$VENV" ]; then
echo "==> Creating virtual environment..."
python3 -m venv "$VENV"
fi
echo "==> Installing Python dependencies..."
"$VENV/bin/pip" install -q -r "$SCRIPT_DIR/requirements.txt"
echo "==> Dependencies installed."
if [ "$DEPS_ONLY" = true ]; then
echo "==> Done (deps only). Start manually with:"
echo " $VENV/bin/python $SCRIPT_DIR/print_server.py"
exit 0
fi
# --- Auto-start service ---
if [ "$OS" = "Linux" ]; then
# systemd service for Raspberry Pi
echo "==> Setting up systemd service..."
sudo tee /etc/systemd/system/${SERVICE_NAME}.service > /dev/null <<EOF
[Unit]
Description=POS Thermal Printer Server
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=3
[Service]
Type=simple
User=$(whoami)
WorkingDirectory=$SCRIPT_DIR
ExecStart=$VENV/bin/python $SCRIPT_DIR/print_server.py
Restart=on-failure
RestartSec=10
MemoryMax=400M
TasksMax=100
KillMode=mixed
TimeoutStopSec=15
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable ${SERVICE_NAME}
sudo systemctl restart ${SERVICE_NAME}
echo "==> Service started and enabled on boot."
echo " Status: sudo systemctl status ${SERVICE_NAME}"
echo " Logs: journalctl -u ${SERVICE_NAME} -f"
echo " Stop: sudo systemctl stop ${SERVICE_NAME}"
elif [ "$OS" = "Darwin" ]; then
# launchd plist for macOS
PLIST="$HOME/Library/LaunchAgents/com.pos.printer.plist"
echo "==> Setting up launchd service..."
mkdir -p "$HOME/Library/LaunchAgents"
cat > "$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pos.printer</string>
<key>ProgramArguments</key>
<array>
<string>$VENV/bin/python</string>
<string>$SCRIPT_DIR/print_server.py</string>
</array>
<key>WorkingDirectory</key>
<string>$SCRIPT_DIR</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/pos-printer.log</string>
<key>StandardErrorPath</key>
<string>/tmp/pos-printer.log</string>
</dict>
</plist>
EOF
# Unload first if already loaded
launchctl bootout gui/$(id -u) "$PLIST" 2>/dev/null || true
launchctl bootstrap gui/$(id -u) "$PLIST"
echo "==> Service started and will auto-start on login."
echo " Logs: tail -f /tmp/pos-printer.log"
echo " Stop: launchctl bootout gui/\$(id -u) $PLIST"
echo " Start: launchctl bootstrap gui/\$(id -u) $PLIST"
fi
echo ""
echo "==> Done! Open http://$(hostname -s).local:9100 from any device on the network."