from datetime import datetime, timedelta
import re
import sys
import subprocess
import os

# Define the first date in URLs list
yeari  = sys.argv[1]
monthi = sys.argv[2]
dayi   = sys.argv[3]

OUT_DIR = "/dados/sismom/SisMOM/sipec/mswep/daily"
daily_output_dir = os.path.join(OUT_DIR, yeari, monthi)
os.makedirs(daily_output_dir, exist_ok=True)
# Read string containing the URLs
with open('mswep_links_'+yeari, 'r') as file:
    lines = file.readlines()

# First date in URLs as a string
first_date_str = lines[0].strip()


# Regular expression to find the ID
# It looks for a sequence of characters that are not a forward slash,
# located after '/d/' and before the next '/'.
pattern = r"/d/(.*?)/"

# Find all matches in the string
ids = re.findall(pattern, lines[1].strip())
# Convert the first date string to a datetime object
current_date = datetime.strptime(first_date_str, "%Y%m%d")
# Create an empty list to store the date-ID pairs
output_pairs = []
# Iterate through the list of IDs
for file_id in ids:
    # Format the current date back into a string
    formatted_date = current_date.strftime("%Y%m%d")

    # Create the date-ID pair string
    pair_string = f"{formatted_date},{file_id}"

    # Add the pair string to the list
    output_pairs.append(pair_string)

    # Increment the date by one day
    current_date += timedelta(days=1)

# Join the list of pairs into a single string separated by commas
final_output = ",".join(output_pairs)

# Print the final result
#print(final_output)

# Split the entire string by the comma to get a list of all fields
fields = final_output.split(',')

# Iterate through the list with the index
for i, field in enumerate(fields):
    # Check if the field matches the search string
    if field == yeari+monthi+dayi :
        # The next field is the one immediately after it (at index i + 1)
        next_field = fields[i + 1]
        print(f"Found match: {field}")
        print(f"The next field is: {next_field}")
        # Stop the loop once the item is found
        break
file_output = yeari+monthi+dayi+".nc"
daily_output = os.path.join(daily_output_dir, file_output)
# Define the curl command as a list of arguments
command = ["curl", "-Lb",
    "/tmp/gcokie",
    "https://drive.google.com/uc?export=download&confirm=Uq6r&id="+next_field,
    "-o",
    daily_output
]
print(command)
# Run the command
# The `capture_output=True` and `text=True` arguments are useful for
# capturing stdout and stderr, if needed.
try:
    result = subprocess.run(command, check=True, capture_output=True, text=True)
    print("Command executed successfully!")
    print("STDOUT:", result.stdout)
    print("STDERR:", result.stderr)
except subprocess.CalledProcessError as e:
    print(f"Error executing command: {e}")
    print("STDOUT:", e.stdout)
    print("STDERR:", e.stderr)
