#!/usr/bin/env bash
#
# Build tests for AVR
#

# exit on first failure
set -e

#
# Backup pins_RAMPS.h
#
cp Marlin/src/pins/pins_RAMPS.h Marlin/src/pins/pins_RAMPS.h.backup

#
# Build with the default configurations
#
restore_configs
exec_test $1 $2 "Default Configuration"

#
# Test 2 extruders (one MAX6675) and heated bed on basic RAMPS 1.4
# Test a "Fix Mounted" Probe with Safe Homing, some arc options,
# linear bed leveling, M48, leveling debug, and firmware retraction.
#
opt_set MOTHERBOARD BOARD_RAMPS_14_EEB
opt_set EXTRUDERS 2
opt_set TEMP_SENSOR_0 -2
opt_set TEMP_SENSOR_1 1
opt_set TEMP_SENSOR_BED 1
opt_set POWER_SUPPLY 1
opt_set GRID_MAX_POINTS_X 16
opt_enable PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING \
           SDSUPPORT EEPROM_SETTINGS FAST_FILE_TRANSFER \
           REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TEST \
           BLINKM PCA9632 RGB_LED NEOPIXEL_LED AUTO_POWER_CONTROL PINS_DEBUGGING \
           NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM \
           AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE \
           SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE
opt_enable_adv FWRETRACT ARC_P_CIRCLES ADVANCED_PAUSE_FEATURE CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS \
               POWER_LOSS_RECOVERY POWER_LOSS_PIN POWER_LOSS_STATE \
               MAX7219_DEBUG LED_CONTROL_MENU CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CODEPENDENT_XY_HOMING
opt_set_adv FANMUX0_PIN 53
exec_test $1 $2 "RAMPS with 2 extruders, RRDFGSC, Linear ABL, LEDs, and many options"

#
# Test a probeless build of AUTO_BED_LEVELING_UBL, with lots of extruders
#
use_example_configs AnimationExample
opt_set SHOW_CUSTOM_BOOTSCREEN
opt_set MOTHERBOARD BOARD_AZTEEG_X3_PRO
opt_set EXTRUDERS 5
opt_set TEMP_SENSOR_1 1
opt_set TEMP_SENSOR_2 5
opt_set TEMP_SENSOR_3 20
opt_set TEMP_SENSOR_4 999
opt_set TEMP_SENSOR_BED 1
opt_enable AUTO_BED_LEVELING_UBL RESTORE_LEVELING_AFTER_G28 DEBUG_LEVELING_FEATURE G26_MESH_EDITING ENABLE_LEVELING_FADE_HEIGHT SKEW_CORRECTION \
           EEPROM_SETTINGS EEPROM_CHITCHAT REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER SDSUPPORT
opt_enable_adv USB_FLASH_DRIVE_SUPPORT SDCARD_SORT_ALPHA STATUS_MESSAGE_SCROLLING SCROLL_LONG_FILENAMES LIGHTWEIGHT_UI \
               CUSTOM_USER_MENUS I2C_POSITION_ENCODERS BABYSTEPPING BABYSTEP_XY LIN_ADVANCE NANODLP_Z_SYNC QUICK_HOME JUNCTION_DEVIATION
exec_test $1 $2 "Azteeg X3 with 5 extruders, RRDFGSC, probeless UBL, Linear Advance, and more"

#
# Add a Sled Z Probe, use UBL Cartesian moves, use Japanese language
#
opt_enable Z_PROBE_SLED SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE
opt_set LCD_LANGUAGE jp-kana
opt_disable SEGMENT_LEVELED_MOVES
opt_enable_adv BABYSTEP_ZPROBE_OFFSET DOUBLECLICK_FOR_Z_BABYSTEPPING BABYSTEP_HOTEND_Z_OFFSET
exec_test $1 $2 "... Sled Z Probe, Skew, UBL Cartesian moves, Japanese, and Z probe BABYSTEPPING"

#
# Test a Servo Probe
# ...with AUTO_BED_LEVELING_3POINT, DEBUG_LEVELING_FEATURE, EEPROM_SETTINGS, EEPROM_CHITCHAT, EXTENDED_CAPABILITIES_REPORT, and AUTO_REPORT_TEMPERATURES
#
restore_configs
opt_enable Z_PROBE_SERVO_NR Z_SERVO_ANGLES DEACTIVATE_SERVOS_AFTER_MOVE \
           AUTO_BED_LEVELING_3POINT DEBUG_LEVELING_FEATURE EEPROM_SETTINGS EEPROM_CHITCHAT
opt_set NUM_SERVOS 1
opt_enable_adv NO_VOLUMETRICS EXTENDED_CAPABILITIES_REPORT AUTO_REPORT_TEMPERATURES AUTOTEMP G38_PROBE_TARGET
exec_test $1 $2 "RAMPS with Servo Probe, 3-Point ABL, DEBUG_LEVELING_FEATURE, EEPROM, G38, and more"

#
# Test MESH_BED_LEVELING feature, with LCD
#
restore_configs
opt_enable MESH_BED_LEVELING G26_MESH_EDITING MESH_G28_REST_ORIGIN LCD_BED_LEVELING MESH_EDIT_MENU ULTIMAKERCONTROLLER
exec_test $1 $2 "MESH_BED_LEVELING feature, with LCD"

#
# Test MINIRAMBO with PWM_MOTOR_CURRENT and many features
#
restore_configs
opt_set MOTHERBOARD BOARD_MINIRAMBO
opt_enable PROBE_MANUALLY AUTO_BED_LEVELING_BILINEAR G26_MESH_EDITING LCD_BED_LEVELING MESH_EDIT_MENU \
           EEPROM_SETTINGS EEPROM_CHITCHAT \
           M100_FREE_MEMORY_WATCHER M100_FREE_MEMORY_DUMPER M100_FREE_MEMORY_CORRUPTOR \
           INCH_MODE_SUPPORT TEMPERATURE_UNITS_SUPPORT \
           ULTIMAKERCONTROLLER SDSUPPORT \
           PRINTCOUNTER NOZZLE_PARK_FEATURE NOZZLE_CLEAN_FEATURE PCA9632
opt_enable_adv BEZIER_CURVE_SUPPORT EXPERIMENTAL_I2CBUS \
               ADVANCED_PAUSE_FEATURE ADVANCED_PAUSE_CONTINUOUS_PURGE FILAMENT_LOAD_UNLOAD_GCODES PARK_HEAD_ON_PAUSE \
               LCD_INFO_MENU M114_DETAIL
opt_set_adv PWM_MOTOR_CURRENT "{ 1300, 1300, 1250 }"
opt_set_adv I2C_SLAVE_ADDRESS 63
exec_test $1 $2 "MINIRAMBO for PWM_MOTOR_CURRENT etc"

#
# Mixing Extruder with 5 steppers, Cyrillic
#
restore_configs
opt_set MOTHERBOARD BOARD_AZTEEG_X3_PRO
opt_enable MIXING_EXTRUDER CR10_STOCKDISPLAY
opt_set MIXING_STEPPERS 5
opt_set LCD_LANGUAGE ru
exec_test $1 $2 "Mixing Extruder with 5 steppers, Cyrillic"

#
# Test DUAL_X_CARRIAGE
#
restore_configs
opt_set MOTHERBOARD BOARD_RUMBA
opt_set EXTRUDERS 2
opt_set TEMP_SENSOR_1 1
opt_enable USE_XMAX_PLUG
opt_enable_adv DUAL_X_CARRIAGE
exec_test $1 $2 "DUAL_X_CARRIAGE"

#
# Test SPEAKER with BOARD_BQ_ZUM_MEGA_3D and BQ_LCD_SMART_CONTROLLER
#
#restore_configs
#opt_set MOTHERBOARD BOARD_BQ_ZUM_MEGA_3D
#opt_set LCD_FEEDBACK_FREQUENCY_DURATION_MS 10
#opt_set LCD_FEEDBACK_FREQUENCY_HZ 100
#opt_enable BQ_LCD_SMART_CONTROLLER SPEAKER

#
# Enable COREXY
#
#restore_configs
#opt_enable COREXY
#exec_test $1 $2 "Stuff"

#
# Test many less common options
#
restore_configs
opt_set MOTHERBOARD BOARD_AZTEEG_X3_PRO
opt_set EXTRUDERS 2
opt_set TEMP_SENSOR_1 -4
opt_set SERVO_DELAY "{ 300, 300, 300 }"
opt_enable COREYX USE_XMAX_PLUG \
           REPRAP_DISCOUNT_SMART_CONTROLLER BABYSTEPPING DAC_MOTOR_CURRENT_DEFAULT \
           FILAMENT_LCD_DISPLAY FILAMENT_WIDTH_SENSOR \
           ENDSTOP_INTERRUPTS_FEATURE ENDSTOP_NOISE_THRESHOLD FAN_SOFT_PWM SDSUPPORT \
           SWITCHING_TOOLHEAD NUM_SERVOS DEBUG_LEVELING_FEATURE
opt_set_adv FAN_MIN_PWM 50
opt_set_adv FAN_KICKSTART_TIME 100
opt_set_adv XY_FREQUENCY_LIMIT  15
opt_enable_adv SHOW_TEMP_ADC_VALUES HOME_Y_BEFORE_X EMERGENCY_PARSER \
               ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED ADVANCED_OK \
               VOLUMETRIC_DEFAULT_ON NO_WORKSPACE_OFFSETS ACTION_ON_KILL \
               EXTRA_FAN_SPEED FWERETRACT Z_DUAL_STEPPER_DRIVERS Z_DUAL_ENDSTOPS \
               MENU_ADDAUTOSTART SDCARD_SORT_ALPHA
exec_test $1 $2 "Many less common options"

######## Other Standard LCD/Panels ##############
#
# ULTRA_LCD
#
#restore_configs
#opt_enable ULTRA_LCD
#exec_test $1 $2 "Stuff"
#
# DOGLCD
#
#restore_configs
#opt_enable DOGLCD
#exec_test $1 $2 "Stuff"
#
# MAKRPANEL
# Needs to use Melzi and Sanguino hardware
#
#restore_configs
#opt_enable MAKRPANEL
#exec_test $1 $2 "Stuff"
#
# REPRAP_DISCOUNT_SMART_CONTROLLER, SDSUPPORT, BABYSTEPPING, RIGIDBOARD_V2, and DAC_MOTOR_CURRENT_DEFAULT
#
#restore_configs
#opt_set MOTHERBOARD BOARD_RIGIDBOARD_V2
#opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT BABYSTEPPING DAC_MOTOR_CURRENT_DEFAULT
#exec_test $1 $2 "Stuff"
# #
# G3D_PANEL with SDCARD_SORT_ALPHA and STATUS_MESSAGE_SCROLLING
#
#restore_configs
#opt_enable G3D_PANEL SDSUPPORT
#opt_enable_adv SDCARD_SORT_ALPHA STATUS_MESSAGE_SCROLLING SCROLL_LONG_FILENAMES
#opt_set_adv SDSORT_GCODE true
#opt_set_adv SDSORT_USES_RAM true
#opt_set_adv SDSORT_USES_STACK true
#opt_set_adv SDSORT_CACHE_NAMES true
#exec_test $1 $2 "Stuff"
#
# REPRAPWORLD_KEYPAD
#
# Cant find configuration details to get it to compile
#restore_configs
#opt_enable ULTRA_LCD REPRAPWORLD_KEYPAD REPRAPWORLD_KEYPAD_MOVE_STEP
#exec_test $1 $2 "Stuff"
#
# RA_CONTROL_PANEL
#
#restore_configs
#opt_enable RA_CONTROL_PANEL PINS_DEBUGGING
#exec_test $1 $2 "Stuff"

######## I2C LCD/PANELS ##############
#
# !!!ATTENTION!!!
# Most I2C configurations are failing at the moment because they require
# a different Liquid Crystal library "LiquidTWI2".
#
# LCD_SAINSMART_I2C_1602
#
#restore_configs
#opt_enable LCD_SAINSMART_I2C_1602
#exec_test $1 $2 "Stuff"
#
# LCD_I2C_PANELOLU2
#
#restore_configs
#opt_enable LCD_I2C_PANELOLU2
#exec_test $1 $2 "Stuff"
#
# LCD_I2C_VIKI
#
#restore_configs
#opt_enable LCD_I2C_VIKI
#exec_test $1 $2 "Stuff"
#
# LCM1602
#
#restore_configs
#opt_enable LCM1602
#exec_test $1 $2 "Stuff"

#
# Language files test with REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
#
#restore_configs
#opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER SDSUPPORT
#for lang in an bg ca zh_CN zh_TW cz da de el el-gr en es eu fi fr gl hr it jp-kana nl pl pt pt-br ru sk tr uk test; do opt_set LCD_LANGUAGE $lang; echo "compile with language $lang ..."; exec_test $1 $2 "Stuff"; done
#
#restore_configs
#opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT
#for lang in an bg ca zh_CN zh_TW cz da de el el-gr en es eu fi fr gl hr it jp-kana nl pl pt pt-br ru sk tr uk test; do opt_set LCD_LANGUAGE $lang; echo "compile with language $lang ..."; exec_test $1 $2 "Stuff"; done

######## Example Configurations ##############
#
# Test a full-featured CR-10S config
#
use_example_configs Creality/CR-10S
opt_enable SHOW_CUSTOM_BOOTSCREEN
exec_test $1 $2 "Full-featured CR-10S config"
#
# BQ Hephestos 2
#restore_configs
#use_example_configs Hephestos_2
#exec_test $1 $2 "Stuff"
#
# Delta Config (generic) + ABL bilinear + PROBE_MANUALLY
use_example_configs delta/generic
opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER DELTA_CALIBRATION_MENU AUTO_BED_LEVELING_BILINEAR PROBE_MANUALLY
exec_test $1 $2 "Delta Config (generic) + ABL bilinear + PROBE_MANUALLY"
#
# Delta Config (generic) + UBL + ALLEN_KEY + OLED_PANEL_TINYBOY2 + EEPROM_SETTINGS
#
use_example_configs delta/generic
opt_enable AUTO_BED_LEVELING_UBL RESTORE_LEVELING_AFTER_G28 Z_PROBE_ALLEN_KEY EEPROM_SETTINGS EEPROM_CHITCHAT \
           OLED_PANEL_TINYBOY2 MESH_EDIT_GFX_OVERLAY
exec_test $1 $2 "Delta Config (generic) + UBL + ALLEN_KEY + OLED_PANEL_TINYBOY2 + EEPROM_SETTINGS"
#
# Delta Config (FLSUN AC because it's complex)
#
use_example_configs delta/FLSUN/auto_calibrate
exec_test $1 $2 "Delta Config (FLSUN AC because it's complex)"
#
# Makibox Config  need to check board type for Teensy++ 2.0
#
#use_example_configs makibox
#exec_test $1 $2 "Stuff"
#
# SCARA with TMC2130
#
use_example_configs SCARA
opt_enable USE_ZMIN_PLUG FIX_MOUNTED_PROBE AUTO_BED_LEVELING_BILINEAR PAUSE_BEFORE_DEPLOY_STOW \
           EEPROM_SETTINGS EEPROM_CHITCHAT ULTIMAKERCONTROLLER
opt_set X_DRIVER_TYPE TMC2130
opt_set Y_DRIVER_TYPE TMC2130
opt_set Z_DRIVER_TYPE TMC2130
opt_set E0_DRIVER_TYPE TMC2130
opt_enable_adv MONITOR_DRIVER_STATUS STEALTHCHOP HYBRID_THRESHOLD SENSORLESS_HOMING
exec_test $1 $2 "SCARA with TMC2130"
#
# TMC2208 Config
#
restore_configs
opt_set X_DRIVER_TYPE TMC2208
opt_set Y_DRIVER_TYPE TMC2208
opt_set Z_DRIVER_TYPE TMC2208
opt_set E0_DRIVER_TYPE TMC2208
opt_disable Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN
opt_enable_adv MONITOR_DRIVER_STATUS STEALTHCHOP HYBRID_THRESHOLD TMC_DEBUG
exec_test $1 $2 "TMC2208 Config"
#
# tvrrug Config need to check board type for sanguino atmega644p
#
#use_example_configs tvrrug/Round2
#exec_test $1 $2 "Stuff"
#
#

# clean up
restore_configs