#!/bin/bash # TODO: CHANGE KLIPPER PORTS SOURCE=${BASH_SOURCE[0]} while [ -L "$SOURCE" ]; do TARGET=$(readlink "$SOURCE") if [[ $TARGET == /* ]]; then SOURCE=$TARGET else DIR=$( dirname "$SOURCE" ) SOURCE=$DIR/$TARGET fi done RDIR=$( dirname "$SOURCE" ) ROOT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) cd $ROOT_DIR source ./ColorEchoForShell/dist/ColorEcho.bash # Pull Klipper git submodule update --init --recursive # Name Printer # Todo: avoid duplications 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 cp -r $ROOT_DIR/dirs $ROOT_DIR/$PRINTER_NAME # Select Klipper Config cd $ROOT_DIR/$PRINTER_NAME/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 # Build Klipper cp $CONFIG_FILE $ROOT_DIR/$PRINTER_NAME/klipper/printer.cfg sed -i -e "s/^serial: .\+$/serial: \/dev\/serial\/by-id\/${DEVICE}/g" "${ROOT_DIR}/$PRINTER_NAME/klipper/printer.cfg" make -j$(nproc) # Flash Firmware make flash FLASH_DEVICE=/dev/serial/by-id/$DEVICE # Copy over printer config file echo.Cyan "Setup printer.cfg" cd $ROOT_DIR/$PRINTER_NAME 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 echo.cyan "Setup Moonraker" cd $ROOT_DIR/$PRINTER_NAME/printer_data/config sed -i -e "s/PRINTERNAME/$PRINTER_NAME/g" "moonraker.conf" sed -i -e "s/pi/$USER/g" "moonraker.conf" echo.Cyan "Setup Mainsail Config" cd $ROOT_DIR/$PRINTER_NAME/mainsail-config sed -i -e "s/$HOME\/printer_data/$HOME\/$PRINTER_NAME\/printer_data/g" "client.cfg" sed -i -e "s/pi/$USER/g" "client.cfg" cd $ROOT_DIR/config/services # TODO: Add Crowsnest Support FILES=(moonraker klipper) for f in ${FILES[@]}; do echo.ICyan "Configuring ${f}" sed -i -e "s/PRINTERNAME/$PRINTER_NAME/g" "${f}.service" sed -i -e "s/pi/$USER/g" "${f}.service" done # Download Mainsail cd $ROOT_DIR echo.Cyan "Downloading Mainsail" wget -q -O mainsail.zip https://github.com/mainsail-crew/mainsail/releases/latest/download/mainsail.zip unzip -o mainsail.zip -d $PRINTER_NAME/mainsail rm mainsail.zip # Copy To Final Location echo.Cyan "Run Copy" mkdir -p $HOME/$PRINTER_NAME cp -r $PRINTER_NAME/* $HOME/$PRINTER_NAME # Create Empty $PRINTER_NAME $PRINTER_NAME=(logs gcodes certs backup mainsail-config) for d in ${$PRINTER_NAME[@]}; do mkdir -p "$HOME/$PRINTER_NAME/$d" done # Configure System Services cd $ROOT_DIR/config/services SERVICES=() for f in *.service; do echo.Cyan "Setup ${f}" SERVICES+=($f) cp "${f}" "${ROOT_DIR}/${PRINTER_NAME}/${PRINTER_NAME}-${f}" sed -i -e "s/PRINTERNAME/$PRINTER_NAME/g" "${ROOT_DIR}/${PRINTER_NAME}/${PRINTER_NAME}-${f}" done # Install System Services echo.Cyan "Install Services" sudo cp $ROOT_DIR/$PRINTER_NAME/*.service /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 # Cleanup