#!/usr/bin/env python3
"""Fake `sfile` for the stag/sgeo tests.

Prints canned sfile-style output with known GPS values, dispatched on
the file's basename (case-insensitive). Used by tests/run_tests.sh with
this directory first on PATH, so stag/sgeo exercise the real client
seam (src/geotag_client.c) against deterministic input.

Forms covered:
  sf.jpg    -- "Geotagging:" section, all fields (EXIF GPS)
  paris.jpg -- section form, no altitude / datum / datetime (defaults)
  tokyo.jpg -- section form, XMP source, no optional fields
  nyc.jpg   -- EXIF GPS tag-name fallback form (DMS rationals + refs,
               GPSDateStamp/GPSTimeStamp), no "source:" line
  anything else -- "none found" (no geotag)
"""
import os
import sys

name = os.path.basename(sys.argv[1])
low = name.lower()
ext = os.path.splitext(low)[1]
fmt = "PNG image" if ext == ".png" else "JPEG image"


def header():
    print("Format: %s (%s) -- task 332" % (fmt, ext.lstrip(".")))


def section(source, lat, lon, alt=None, datum=None, dt=None):
    header()
    print("Geotagging:")
    print("  source: %s" % source)
    print("  gps_latitude: %.6f" % lat)
    print("  gps_longitude: %.6f" % lon)
    if alt is not None:
        print("  gps_altitude_m: %.3f (above sea level)" % alt)
    if datum is not None:
        print("  gps_datum: %s" % datum)
    if dt is not None:
        print("  gps_datetime_utc: %s" % dt)


if low == "sf.jpg":
    # Full section form; DMS display in parens is ignored by the parser
    # (the decimal before it is authoritative).
    header()
    print("Geotagging:")
    print("  source: EXIF GPS")
    print("  gps_latitude: 37.774929 (37\xb046'29.64\"N)")
    print("  gps_longitude: -122.419418 (122\xb025'9.90\"W)")
    print("  gps_altitude_m: 12.000 (above sea level)")
    print("  gps_datum: WGS-84")
    print("  gps_datetime_utc: 2024-05-01 12:34:56")
elif low == "paris.jpg":
    section("EXIF GPS", 48.856600, 2.352200)
elif low == "tokyo.jpg":
    section("XMP", 35.676200, 139.650300)
elif low == "nyc.jpg":
    # EXIF tag-name fallback form, as sfile's uniform header table
    # would show it: DMS rationals plus hemisphere refs, separate
    # date/time tags, no "source:" line (source becomes "unknown").
    header()
    print("GPSLatitude: 40/1, 42/1, 4608/100")
    print("GPSLatitudeRef: N")
    print("GPSLongitude: 74/1, 0/1, 2160/100")
    print("GPSLongitudeRef: W")
    print("GPSAltitude: 10/1")
    print("GPSAltitudeRef: 0")
    print("GPSDateStamp: 2024:06:15")
    print("GPSTimeStamp: 8/1, 30/1, 0/1")
else:
    header()
    print("Geotagging:")
    print("  none found (searched EXIF GPS IFD in JPEG APP1; XMP in JPEG APP1)")
