#!/usr/bin/env python3
import os
import subprocess
import time
import json

def main():
    # Get our working copy
    local_rev = subprocess.check_output(['/usr/bin/svn', 'info', '--show-item', 'last-changed-revision', '/dist/']).decode('ascii', errors='ignore').strip()
    local_time_x = subprocess.check_output(['/usr/bin/svn', 'info', '--show-item', 'last-changed-date', '/dist/']).decode('ascii', errors='ignore').strip()
    local_time = time.strptime(local_time_x, "%Y-%m-%dT%H:%M:%S.%fZ")
        
    # Get remote svn
    remote_rev = subprocess.check_output(['/usr/bin/svn', 'info', '--show-item', 'last-changed-revision', 'https://dist.apache.org/repos/dist/release/']).decode('ascii', errors='ignore').strip()
    remote_time_x = subprocess.check_output(['/usr/bin/svn', 'info', '--show-item', 'last-changed-date', 'https://dist.apache.org/repos/dist/release/']).decode('ascii', errors='ignore').strip()
    remote_time = time.strptime(remote_time_x, "%Y-%m-%dT%H:%M:%S.%fZ")
    
    # dump as json
    jsout = json.dumps( {
        'now': int(time.time()),                           # This is now, used for checking mirroring frequency
        'local_revision': local_rev,                       # This is our local revision count. Should equal remote
        'local_changedate': int(time.mktime(local_time)),  # Last change date, as unix timestamp
        'remote_revision': remote_rev,                     # dist.a.o's last revision for release/ - should match local
        'remote_changedate': int(time.mktime(remote_time)) # Remote's last change unix timestamp. Should be within 30 min of local if revisions don't match
    }, indent = 2)
    open("/dist/zzz/status.json", "w").write(jsout)
    
if __name__ == "__main__":
    main()