[gcode_shell_command POWER_LOSS_RESUME]
command: bash /home/mks/scripts/plr/plr.sh
timeout: 120

[gcode_shell_command UPDATE_GCODE_LINES]
command: bash /home/mks/scripts/plr/update_gcode_lines.sh

[gcode_macro save_last_file]
gcode:
    {% set svv = printer.save_variables.variables %}
    {% set filepath=printer.virtual_sdcard.file_path %} 
    {% set filename=filepath.split('/')%}
    SAVE_VARIABLE VARIABLE=was_interrupted VALUE=True
    SAVE_VARIABLE VARIABLE=print_temp VALUE={printer.extruder.target}
    SAVE_VARIABLE VARIABLE=bed_temp VALUE={printer['heater_bed'].target}
    {% if printer["heater_generic chamber"] is defined %}
        SAVE_VARIABLE VARIABLE=hot_temp VALUE={printer['heater_generic chamber'].target}
    {% else %}
        SAVE_VARIABLE VARIABLE=hot_temp VALUE=0
    {% endif %}
    M118 Last File: { filename[-1] }
    M117 Last File: { filename[-1] }
    G4 P2000

[gcode_macro CLEAR_LAST_FILE]
gcode:
  {% set filename='' %}
  SAVE_VARIABLE VARIABLE=last_file VALUE='"{ filename }"'
  SAVE_VARIABLE VARIABLE=was_interrupted VALUE=False

[gcode_macro LOG_Z]
gcode:
    {% set z_pos = printer.gcode_move.gcode_position.z %}
    RESPOND MSG="Current Z is {z_pos}"
    SAVE_VARIABLE VARIABLE=power_resume_z VALUE={z_pos}

[gcode_macro RESUME_INTERRUPTED]
gcode:
    set_zoffset
    RUN_SHELL_COMMAND CMD=UPDATE_GCODE_LINES
    {% set profile_name = params.PROFILE_NAME|default(printer.save_variables.variables.profile_name)|string %}
    # {% set g_lines = params.GCODE_LINES|default(printer.save_variables.variables.gcode_lines)|string %}
    # {% set last_file = params.GCODE_FILE|default(printer.save_variables.variables.last_file)|string %}
    # {% set print_temp = params.PRINT_TEMP|default(printer.save_variables.variables.print_temp)|float %}
    # {% set bed_temp = params.BED_TEMP|default(printer.save_variables.variables.bed_temp)|float %}
    # {% set hot_temp = params.HOT_TEMP|default(printer.save_variables.variables.hot_temp)|float %}
    # {% set filepath = params.FILE_PATH|default(printer.save_variables.variables.file_path)|string %}
    BED_MESH_PROFILE LOAD={profile_name}
    RUN_SHELL_COMMAND CMD=POWER_LOSS_RESUME
    SDCARD_PRINT_FILE FILENAME=/.plr/plr.gcode


# [gcode_macro RESUME_INTERRUPTED]
# gcode:
#     set_zoffset
#     {% set profile_name = params.PROFILE_NAME|default(printer.save_variables.variables.profile_name)|string %}
#     {% set z_height = params.Z_HEIGHT|default(printer.save_variables.variables.power_resume_z)|float %}
#     {% set last_file = params.GCODE_FILE|default(printer.save_variables.variables.last_file)|string %}
#     {% set print_temp = params.PRINT_TEMP|default(printer.save_variables.variables.print_temp)|float %}
#     {% set bed_temp = params.BED_TEMP|default(printer.save_variables.variables.bed_temp)|float %}
#     {% set hot_temp = params.HOT_TEMP|default(printer.save_variables.variables.hot_temp)|float %}
#     {% set filepath = params.FILE_PATH|default(printer.save_variables.variables.file_path)|string %}
#     BED_MESH_PROFILE LOAD={profile_name}
#     RUN_SHELL_COMMAND CMD=POWER_LOSS_RESUME PARAMS="{z_height} {last_file} {print_temp} {bed_temp} {hot_temp} {filepath}"
#     SDCARD_PRINT_FILE FILENAME=/.plr/plr.gcode

