#!/usr/bin/env python3

import os
import sys
import matplotlib.pyplot as plt

# Get the name of the Python script that called this helper
source = os.path.splitext(os.path.basename(sys.argv[1]))[0]

# Save every open figure
for i, num in enumerate(plt.get_fignums(), start=1):
    plt.figure(num)

    if len(plt.get_fignums()) == 1:
        filename = source + ".jpeg"
    else:
        filename = source + "_" + str(i) + ".jpeg"

    plt.savefig(
        filename,
        format="jpeg",
        dpi=60,
        bbox_inches="tight"
    )

    plt.close(num)
