hacdc-klipper/setup.sh
2024-04-29 20:37:05 -04:00

120 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# TODO: CHANGE KLIPPER PORTS
ROOT_DIR="$(dirname "$0")"
cd $ROOT_DIR
# Pull Klipper
git submodule update --init --recursive
# Select Klipper Config
cd dirs/klipper
make menuconfig
CONFIG_FILES=(`ls config`)
CONFIG_FILES+=("Enter_Manually")
let i=0
C=()
for f in ${CONFIG_FILES[@]}; do
C+=($i $f)
let i+=1
done
CONFIG_INDEX=$(dialog --backtitle "drwho@hackers.town" --title "Printer Selection" --menu "Select Printer Config" --output-fd 1 40 0 1 ${C[@]})
CONFIG_FILE=${CONFIG_FILES[$CONFIG_INDEX]}
if [[ $CONFIG_FILE == "Enter_Manually" ]]; then
MANUAL_FILE=""
while [ ! -f "${MANUAL_FILE}" ]; do
MANUAL_FILE=$(dialog --backtitle "drwho@hackers.town" --title "Manual Config Selection" --inputbox "Enter Full Config File Path" 8 40 "${MANUAL_FILE}" 3>&1 1>&2 2>&3)
done
CONFIG_FILE=$MANUAL_FILE
else
CONFIG_FILE="./config/$CONFIG_FILE"
fi
# Select Serial Device
DEVICES=(`ls /dev/serial/by-id/`)
DEVICES+=("Enter_Manually")
let i=0
C=()
for f in ${DEVICES[@]}; do
C+=($i $f)
let i+=1
done
DEVICE_INDEX=$(dialog --backtitle "drwho@hackers.town" --title "USB Device Selection" --menu "Select USB Device" --output-fd 1 40 0 1 ${C[@]})
DEVICE=${DEVICES[$DEVICE_INDEX]}
if [[ $DEVICE == "Enter_Manually" ]]; then
MANUAL_FILE=""
while [ ! -f "${MANUAL_FILE}" ]; do
MANUAL_FILE=$(dialog --backtitle "drwho@hackers.town" --title "Manual Device Selection" --inputbox "Enter Full Device File Path" 8 40 "${MANUAL_FILE}" 3>&1 1>&2 2>&3)
done
DEVICE=$MANUAL_FILE
fi
# Name Printer
NAME_NOT_VALID=true
while $NAME_NOT_VALID ; do
PRINTER_NAME="$(dialog --backtitle 'drwho@hackers.town' --inputbox 'Name This Printer' 8 40 \"${PRINTER_NAME}\" 3>&1 1>&2 2>&3)"
if [[ $PRINTER_NAME =~ ^[a-zA-Z0-9_-]+$ ]]; then
NAME_NOT_VALID=false
fi
done
# Build Klipper
cat $CONFIG_FILE | sed -e "s/^serial: .\+$/serial: \/dev\/serial\/by-id\/${DEVICE}/" > ./printer.cfg
make -j$(nproc)
# Flash Firmware
make flash FLASH_DEVICE=/dev/serial/by-id/$DEVICE
# Copy over printer config file
cd $ROOT_DIR/dirs
cat $ROOT_DIR/config/macros.gcode >> printer.cfg.tmp
cat klipper/printer.cfg >> printer.cfg.tmp
mv printer.cfg.tmp printer_data/config/printer.cfg
# Apply Edits to Configs
cd $ROOT_DIR/dirs/print_data
cat config/moonraker.conf | sed -e "s/PRINTERNAME/$PRINTER_NAME/g" > config/moonraker.conf.tmp
mv config/moonraker.conf.tmp config/moonraker.conf
cat mainsail-config/client.cfg | sed -e "s/~\/printer_data/~\/$PRINTER_NAME\/printer_data/g" > mainsail-config/client.cfg.tmp
mv mainsail-config/client.cfg.tmp mainsail-config/client.cfg
FILES=(crowsnest moonraker klipper)
for f in ${FILES[@]}; do
cat systemd/$f.service | sed -e "s/PRINTERNAME/$PRINTER_NAME/g" > systemd/$f.service.tmp
mv systemd/$f.service.tmp systemd/$f.service
done
# Download Mainsail
cd $ROOT_DIR
wget -q -O mainsail.zip https://github.com/mainsail-crew/mainsail/releases/latest/download/mainsail.zip
unzip -o mainsail.zip -d dirs/mainsail
# Copy To Final Location
mkdir -p ~/$PRINTER_NAME
cp -r dirs/* ~/$PRINTER_NAME
# Create Empty Dirs
DIRS=(logs gcodes certs backup mainsail-config)
for d in ${DIRS[@]}; do
mkdir -p "~/$PRINTER_NAME/$d"
done
# Configure System Services
cd $ROOT_DIR/config/services
SERVICES=()
for f in *.service; do
SERVICES+=($f)
cat $f | sed -e "s/PRINTERNAME/$PRINTER_NAME/g" > /etc/systemd/system/$PRINTERNAME-$f
done
# Install System Services
sudo cp $ROOT_DIR/config/services/* /etc/systemd/system/
sudo systemctl daemon-reload
for svc in ${SERVICES[@]}; do
sudo systemctl enable $PRINTER_NAME-$svc
sudo systemctl start $PRINTER_NAME-$svc
done