#!/bin/sh set -eu SLIC3R_CONFIG_BASE="${HOME}/.Slic3r" TEMPERATURES='retraction wiping probing removal_bed' warn() { local fmt="${1}" shift 1 printf "Warning: ${fmt}\n" "${@}" return 0 } preprocess() { local printer="${1}" local gcode="${2}" local filament="${3}" shift 3 local ini= local temp= local temp_val= local script= local gcode_pp= # Find and back up printer/filament configuration ini="${SLIC3R_CONFIG_BASE}/printer/${printer}, ${filament}.ini" if ! cp "${ini}" "${ini}~" 2>/dev/null; then warn 'Missing configuration for printer "%s" and filament "%s"'\ "${printer}" "${filament}" return 0 fi # Load and validate filament temperature settings for temp in ${TEMPERATURES}; do eval "local ${temp}_temperature=" done . "${SLIC3R_CONFIG_BASE}/filament/${filament}.rc" for temp in ${TEMPERATURES}; do temp_val="$(eval "printf '%s' \"\${${temp}_temperature}\"")" case "${temp_val}" in '') eval "${temp}_temperature=0" ;; *[!0-9]*) warn 'Invalid "%s" value "%s" of filament "%s"'\ "${temp}_temperature" "${temp_val}" \ "${filament}" return 0 ;; esac done # Preprocess G-code and insert into printer/filament configuration script='1h; 1!H; ${ g; s/\n/\\n/g; ' for temp in ${TEMPERATURES}; do temp="${temp}_temperature" eval "script=\"\${script}s/<\${temp}>/\${${temp}}/g; \"" done script="${script}"'s|[\/&]|\\&|g; p; };' gcode_pp="${SLIC3R_CONFIG_BASE}/printer/${printer} - ${gcode}.gcode" gcode_pp="$(sed -n "${script}" 0<"${gcode_pp}")" sed "s/^\\(${gcode}_gcode *= *\\).*\$/\\1${gcode_pp}/;" \ "${ini}~" 1>"${ini}" return 0 } preprocess_all() { local gcode= local printer= local rc= local filament= for gcode in "${SLIC3R_CONFIG_BASE}/printer/"*' - '*'.gcode'; do gcode="${gcode##*/}" printer="${gcode% - *.gcode}" gcode="${gcode##${printer} - }" gcode="${gcode%.gcode}" for rc in "${SLIC3R_CONFIG_BASE}/filament/"*'.rc'; do filament="${rc##*/}" filament="${filament%.rc}" preprocess "${printer}" "${gcode}" "${filament}" done done return 0 } main() { preprocess_all return 0 } main "${@}"