diff --git a/kiauh.sh b/kiauh.sh index e505991..3e210a5 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -20,6 +20,7 @@ KLIPPER_DIR=${HOME}/klipper KLIPPY_ENV_DIR=${HOME}/klippy-env KLIPPER_SERVICE1=/etc/init.d/klipper KLIPPER_SERVICE2=/etc/default/klipper +KLIPPER_SERVICE3=/etc/systemd/system/klipper.service #nginx NGINX_SA=/etc/nginx/sites-available NGINX_SE=/etc/nginx/sites-enabled diff --git a/scripts/functions.sh b/scripts/functions.sh index b9213fe..03b051f 100755 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -13,17 +13,24 @@ check_euid(){ locate_printer_cfg(){ unset PRINTER_CFG - status_msg "Locating printer.cfg via /etc/default/klipper ..." - if [ -f $KLIPPER_SERVICE2 ]; then + if [ -e $KLIPPER_SERVICE2 ]; then + status_msg "Locating printer.cfg via $KLIPPER_SERVICE2 ..." #reads /etc/default/klipper and gets the default printer.cfg location KLIPPY_ARGS_LINE="$(grep "KLIPPY_ARGS=" /etc/default/klipper)" KLIPPY_ARGS_COUNT="$(grep -o " " <<< "$KLIPPY_ARGS_LINE" | wc -l)" i=1 PRINTER_CFG=$(while [ "$i" != "$KLIPPY_ARGS_COUNT" ]; do grep -E "(\/[A-Za-z0-9\_-]+)+\/printer\.cfg" /etc/default/klipper | cut -d" " -f"$i"; i=$(( $i + 1 )); done | grep "printer.cfg") ok_msg "printer.cfg location: '$PRINTER_CFG'" + elif [ -e $KLIPPER_SERVICE3 ]; then + #reads /etc/systemd/system/klipper.service and gets the default printer.cfg location + KLIPPY_ARGS_LINE="$(grep "ExecStart=" /etc/systemd/system/klipper.service)" + KLIPPY_ARGS_COUNT="$(grep -o " " <<< "$KLIPPY_ARGS_LINE" | wc -l)" + i=1 + PRINTER_CFG=$(while [ "$i" != "$KLIPPY_ARGS_COUNT" ]; do grep -E "(\/[A-Za-z0-9\_-]+)+\/printer\.cfg" /etc/systemd/system/klipper.service | cut -d" " -f"$i"; i=$(( $i + 1 )); done | grep "printer.cfg") + ok_msg "printer.cfg location: '$PRINTER_CFG'" else PRINTER_CFG="" - warn_msg "Can't read /etc/default/klipper - File not found!" + warn_msg "Can't read printer.cfg location!" fi } diff --git a/scripts/install_klipper.sh b/scripts/install_klipper.sh index 38d06d0..4747ffb 100755 --- a/scripts/install_klipper.sh +++ b/scripts/install_klipper.sh @@ -1,5 +1,5 @@ install_klipper(){ - if [ -e /etc/init.d/klipper ] && [ -e /etc/default/klipper ]; then + if [ -e $KLIPPER_SERVICE1 ] && [ -e $KLIPPER_SERVICE2 ] || [ -e $KLIPPER_SERVICE3 ]; then ERROR_MSG="Looks like Klipper is already installed!" else get_user_selections_klipper @@ -12,6 +12,37 @@ install_klipper(){ get_user_selections_klipper(){ status_msg "Initializing Klipper installation ..." + #let user choose to install systemd or init.d service + unset INST_SYSTEMD + unset INST_INITD + while true; do + echo + top_border + echo -e "| Do you want to install Klipper as: |" + echo -e "| 1) Init.d Service (default) |" + echo -e "| 2) Systemd Service |" + hr + echo -e "| Please use the appropriate option for your chosen |" + echo -e "| Linux distribution. If you are unsure what to select, |" + echo -e "| please do some research before. |" + hr + echo -e "| If you run Raspberry Pi OS, both options will work. |" + bottom_border + read -p "${cyan}###### Please choose:${default} " action + case "$action" in + 1|"") + INST_KLIPPER_INITD="true" + INST_KLIPPER_SYSTEMD="false" + break;; + 2) + INST_KLIPPER_INITD="false" + INST_KLIPPER_SYSTEMD="true" + break;; + *) + print_unkown_cmd + print_msg && clear_msg;; + esac + done #ask user for building firmware while true; do echo @@ -61,14 +92,12 @@ klipper_setup(){ git clone $KLIPPER_REPO ok_msg "Klipper successfully cloned!" status_msg "Installing Klipper Service ..." - $KLIPPER_DIR/scripts/install-octopi.sh - ok_msg "Klipper installation complete!" - #create a klippy.log symlink in home-dir just for convenience - if [ ! -e ${HOME}/klippy.log ]; then - status_msg "Creating klippy.log Symlink ..." - ln -s /tmp/klippy.log ${HOME}/klippy.log - ok_msg "Symlink created!" + if [ "$INST_KLIPPER_INITD" = "true" ]; then + $KLIPPER_DIR/scripts/install-octopi.sh + elif [ "$INST_KLIPPER_SYSTEMD" = "true" ]; then + $KLIPPER_DIR/scripts/install-debian.sh fi + ok_msg "Klipper installation complete!" } flash_routine(){ diff --git a/scripts/install_moonraker.sh b/scripts/install_moonraker.sh index ea128f9..005e006 100755 --- a/scripts/install_moonraker.sh +++ b/scripts/install_moonraker.sh @@ -65,43 +65,6 @@ system_check_moonraker(){ } get_user_selections_moonraker(){ - #ask if moonraker only or moonraker + mainsail - #while true; do - # echo - # top_border - # echo -e "| Install the Moonraker API only? |" - # blank_line - # echo -e "| You can choose to install Moonraker and one of the |" - # echo -e "| following web interfaces: |" - # echo -e "| 1) Mainsail |" - # echo -e "| 2) Fluidd |" - # hr - # echo -e "| If you want to install a web interface later, just |" - # echo -e "| press 'ENTER' to continue the Moonraker installation. |" - # bottom_border - # read -p "${cyan}Please choose:${default} " selection - # case "$selection" in - # "") - # echo -e "###### > Moonraker only" - # INST_NOUI="true" - # break;; - # 1) - # echo -e "###### > Moonraker + Mainsail" - # INST_MAINSAIL="true" - # break;; - # 2) - # echo -e "###### > Moonraker + Fluidd" - # INST_FLUIDD="true" - # break;; - # *) - # print_unkown_cmd - # print_msg && clear_msg;; - # esac - #done - ##ask to change hostname if mainsail should be installed as well - #if [ "$INST_MAINSAIL" = "true" ]; then - # create_custom_hostname - #fi #user selection for printer.cfg if [ "$PRINTER_CFG_FOUND" = "false" ]; then unset SEL_DEF_CFG @@ -136,13 +99,13 @@ get_user_selections_moonraker(){ if [ "$KLIPPY_SL_FOUND" = "false" ]; then while true; do echo - read -p "${cyan}###### Create klippy.log symlink? (Y/n):${default} " yn + read -p "${cyan}###### Create klippy.log symlink? (y/N):${default} " yn case "$yn" in - Y|y|Yes|yes|"") + Y|y|Yes|yes) echo -e "###### > Yes" SEL_KLIPPYLOG_SL="true" break;; - N|n|No|no) + N|n|No|no|"") echo -e "###### > No" SEL_KLIPPYLOG_SL="false" break;; @@ -156,13 +119,13 @@ get_user_selections_moonraker(){ if [ "$MOONRAKER_SL_FOUND" = "false" ]; then while true; do echo - read -p "${cyan}###### Create moonraker.log symlink? (Y/n):${default} " yn + read -p "${cyan}###### Create moonraker.log symlink? (y/N):${default} " yn case "$yn" in - Y|y|Yes|yes|"") + Y|y|Yes|yes) echo -e "###### > Yes" SEL_MRLOG_SL="true" break;; - N|n|No|no) + N|n|No|no|"") echo -e "###### > No" SEL_MRLOG_SL="false" break;; @@ -331,19 +294,43 @@ moonraker_setup(){ } patch_klipper_sysfile(){ - status_msg "Checking /etc/default/klipper for necessary entries ..." - #patching new printer.cfg location to /etc/default/klipper - if ! grep -q "/klipper_config/printer.cfg" $KLIPPER_SERVICE2; then - status_msg "Patching new printer.cfg location to /etc/default/klipper ..." - sudo sed -i "/KLIPPY_ARGS=/ s|$PRINTER_CFG|/home/${USER}/klipper_config/printer.cfg|" /etc/default/klipper - ok_msg "New location is: '/home/${USER}/klipper_config/printer.cfg'" + if [ -e $KLIPPER_SERVICE2 ]; then + status_msg "Checking /etc/default/klipper for necessary entries ..." + #patching new printer.cfg location to /etc/default/klipper + if ! grep -q "/klipper_config/printer.cfg" $KLIPPER_SERVICE2; then + status_msg "Patching new printer.cfg location to /etc/default/klipper ..." + sudo sed -i "/KLIPPY_ARGS=/ s|$PRINTER_CFG|/home/${USER}/klipper_config/printer.cfg|" $KLIPPER_SERVICE2 + ok_msg "New location is: '/home/${USER}/klipper_config/printer.cfg'" + fi + #patching new UDS argument to /etc/default/klipper + if ! grep -q -- "-a /tmp/klippy_uds" $KLIPPER_SERVICE2; then + status_msg "Patching unix domain socket to /etc/default/klipper ..." + #append the new argument to /tmp/klippy.log argument + sudo sed -i "/KLIPPY_ARGS/s/\.log/\.log -a \/tmp\/klippy_uds/" $KLIPPER_SERVICE2 + ok_msg "Patching done!" + fi fi - #patching new UDS argument to /etc/default/klipper - if ! grep -q -- "-a /tmp/klippy_uds" $KLIPPER_SERVICE2; then - status_msg "Patching unix domain socket to /etc/default/klipper ..." - #append the new argument to /tmp/klippy.log argument - sudo sed -i "/KLIPPY_ARGS/s/\.log/\.log -a \/tmp\/klippy_uds/" $KLIPPER_SERVICE2 - ok_msg "Patching done!" + if [ -e $KLIPPER_SERVICE3 ]; then + status_msg "Checking /etc/systemd/system/klipper.service for necessary entries ..." + #patching new printer.cfg location to /etc/systemd/system/klipper.service + if ! grep -q "/klipper_config/printer.cfg" $KLIPPER_SERVICE3; then + status_msg "Patching new printer.cfg location to /etc/systemd/system/klipper.service ..." + sudo sed -i "/ExecStart=/ s|$PRINTER_CFG|/home/${USER}/klipper_config/printer.cfg|" $KLIPPER_SERVICE3 + ok_msg "New location is: '/home/${USER}/klipper_config/printer.cfg'" + #set variable if file got edited + SERVICE_FILE_PATCHED="true" + fi + #patching new UDS argument to /etc/systemd/system/klipper.service + if ! grep -q -- "-a /tmp/klippy_uds" $KLIPPER_SERVICE3; then + status_msg "Patching unix domain socket to /etc/systemd/system/klipper.service ..." + #append the new argument to /tmp/klippy.log argument + sudo sed -i "/ExecStart/s/\.log/\.log -a \/tmp\/klippy_uds/" $KLIPPER_SERVICE3 + ok_msg "Patching done!" + #set variable if file got edited + SERVICE_FILE_PATCHED="true" + fi + #reloading the units is only needed when the service file was patched. + [ "$SERVICE_FILE_PATCHED" = "true" ] && status_msg "Reloading unit ..." && sudo systemctl daemon-reload fi ok_msg "Check complete!" echo @@ -402,18 +389,10 @@ setup_printer_config_mainsail(){ read_printer_cfg_mainsail(){ SC="#*# <---------------------- SAVE_CONFIG ---------------------->" - if [ ! $(grep '^\[virtual_sdcard\]$' $PRINTER_CFG) ]; then - VSD="false" - fi - if [ ! $(grep '^\[pause_resume\]$' $PRINTER_CFG) ]; then - PAUSE_RESUME="false" - fi - if [ ! $(grep '^\[display_status\]$' $PRINTER_CFG) ]; then - DISPLAY_STATUS="false" - fi - if [ ! "$(grep '^\[include mainsail_macros\.cfg\]$' $PRINTER_CFG)" ]; then - MS_MACRO="false" - fi + [ ! "$(grep '^\[virtual_sdcard\]$' $PRINTER_CFG)" ] && VSD="false" + [ ! "$(grep '^\[pause_resume\]$' $PRINTER_CFG)" ] && PAUSE_RESUME="false" + [ ! "$(grep '^\[display_status\]$' $PRINTER_CFG)" ] && DISPLAY_STATUS="false" + [ ! "$(grep '^\[include mainsail_macros\.cfg\]$' $PRINTER_CFG)" ] && MS_MACRO="false" #check for a SAVE_CONFIG entry if [[ $(grep "$SC" $PRINTER_CFG) ]]; then SC_LINE=$(grep -n "$SC" $PRINTER_CFG | cut -d ":" -f1) diff --git a/scripts/remove.sh b/scripts/remove.sh index 95b4e85..550b753 100755 --- a/scripts/remove.sh +++ b/scripts/remove.sh @@ -2,6 +2,7 @@ remove_klipper(){ data_arr=( /etc/init.d/klipper /etc/default/klipper + /etc/systemd/system/klipper.service $KLIPPER_DIR $KLIPPY_ENV_DIR ${HOME}/klippy.log @@ -15,6 +16,13 @@ remove_klipper(){ sudo update-rc.d -f klipper remove ok_msg "Klipper Service removed!" fi + if [ -e /etc/systemd/system/klipper.service ]; then + status_msg "Removing Klipper Service ..." + sudo rm -rf /etc/systemd/system/klipper.service + sudo update-rc.d -f klipper remove + sudo systemctl daemon-reload + ok_msg "Klipper Service removed!" + fi if [[ -d $KLIPPER_DIR || -d $KLIPPY_ENV_DIR ]]; then status_msg "Removing Klipper and klippy-env directory ..." rm -rf $KLIPPER_DIR $KLIPPY_ENV_DIR && ok_msg "Directories removed!" diff --git a/scripts/status.sh b/scripts/status.sh index f9e3d60..b4cd852 100755 --- a/scripts/status.sh +++ b/scripts/status.sh @@ -15,9 +15,10 @@ klipper_status(){ klipper_data=( $KLIPPER_DIR $KLIPPY_ENV_DIR - $KLIPPER_SERVICE1 - $KLIPPER_SERVICE2 + SERVICE ) + #remove the "SERVICE" entry from the klipper_data array if a klipper service is installed + [ "$(systemctl list-units --full -all -t service --no-legend | grep -F "klipper.service")" ] && unset klipper_data[2] #count+1 for each found data-item from array for kd in "${klipper_data[@]}" do