#! /usr/bin/python

# Importing necessary external libraries
import sys

# Gcode commands at the beginning
header = """
G21 ;
F100 ; This says set units to mm  and sets the speed/feedrate to 1000 mm/minute
G90 ; absolute coordinates

G28; Go to home position

G1 X200 Y150 Z40 ; Go to sponge position

G4 S3 ; wait 3 second
"""

# Gcode commands to be repeated
repeat = """
G1 X200 Y150 Z30 ; goes to Z30
G4 P200 ; wait 0.4 seconds
G1 X200 Y150 Z20 ; goes to Z20 (10mm towards plate)
G4 S1 ; wait 1 second
"""

# How many times we want the repeat
# It uses the parameter we write when we execute the code in the terminal
nb = int(sys.argv[1])

# Creating a text/gcode file
# The file will be called `pattern-10x.gcode`, `pattern-36x.gcode`
# according to the number of repeats
f = open("pattern-%dx.gcode" % nb, "w")

# Writing the init Gcode commands in the text file
f.write(header)

# Writing the repeated Gcode commands in the text file
f.write(nb * repeat)

# Closing the text file
f.close()
