#!/usr/bin/python3 -B

# This script expects the directory ~/samba-rsync-ftp to exist and to be a
# copy of the /home/ftp/pub/rsync dir on samba.org.  It also requires a
# git checkout of rsync (feel free to use your normal rsync build dir as
# long as it doesn't have any uncommitted changes).
#
# If this is run with -tu, it will make an updated "nightly" tar file in
# the nightly dir.  It will also remove any old tar files, regenerate the
# HTML man pages in the nightly dir, and then rsync the changes to the
# samba.org server.

import os, sys, re, argparse, glob
from datetime import datetime, timezone
from getpass import getpass

sys.path = ['packaging'] + sys.path

from pkglib import *

# Where the local copy of /home/ftp/pub/rsync/dev/nightly should be updated.
dest = os.environ['HOME'] + '/samba-rsync-ftp/dev/nightly'
samba_host = os.environ['SAMBA_HOST']
nightly_symlink = f"{dest}/rsync-HEAD.tar.gz"

def main():
    now = datetime.now(timezone.utc)
    name = now.strftime('rsync-HEAD-%Y%m%d-%H%MGMT')
    ztoday = now.strftime('%d %b %Y')
    today = ztoday.lstrip('0')
    gen_target = 'gensend' if args.upload else 'gen'

    if not os.path.isdir(dest):
        die("$dest does not exist")
    if not os.path.isdir('.git'):
        die("There is no .git dir in the current directory.")
    if not os.path.exists('rsyncd.conf.5.md'):
        die("There is no rsync checkout in the current directory.")

    mandate_gensend_hook()

    if args.make_tar:
        check_git_state('master')
        cmd_chk(['touch', 'NEWS.md'])
        cmd_chk(['make', gen_target])
        cmd_chk(['rsync', '-a', *glob.glob('*.[1-9].html'), dest])

        gen_files = get_gen_files()

        confversion = get_configure_version()

        # All version values are strings!
        last_version, last_protocol_version = get_OLDNEWS_version_info()
        protocol_version, subprotocol_version = get_protocol_versions()

        if 'dev' in confversion or 'pre' in confversion:
            if last_protocol_version != protocol_version:
                if subprotocol_version == '0':
                    die("SUBPROTOCOL_VERSION must not be 0 for a non-final release with a changed PROTOCOL_VERSION.")
            elif subprotocol_version != '0':
                die("SUBPROTOCOL_VERSION must be 0 when the PROTOCOL_VERSION hasn't changed from the last release.")
        elif subprotocol_version != '0':
            die("SUBPROTOCOL_VERSION must be 0 for a final release.")

        name_slash = name + '/'
        tar_name = f"{name}.tar.gz"

        print('Creating', tar_name)

        cmd_chk(['rsync', '-a', *gen_files, name_slash])
        cmd_chk(f"git archive --format=tar --prefix={name}/ HEAD | tar xf -")
        cmd_chk(['support/git-set-file-times', '--quiet', '--prefix', name_slash])
        cmd_chk(['fakeroot', 'tar', 'czf', os.path.join(dest, tar_name), name])
        cmd_chk(['rm', '-rf', name])

        if os.path.lexists(nightly_symlink):
            os.unlink(nightly_symlink)
        os.symlink(tar_name, nightly_symlink)

    os.chdir(dest)

    tar_files = list(reversed(sorted(glob.glob('rsync-HEAD-*'))))
    if len(tar_files) > 10:
        for fn in tar_files[10:]:
            print('Removing', fn)
            os.unlink(fn)

    cmd_run('ls -ltr'.split())

    if args.upload:
        cmd = 'rsync -aivHP --delete-after'.split()
        partial_dir = os.environ.get('RSYNC_PARTIAL_DIR', None)
        if partial_dir:
            cmd.append('-fR ' + partial_dir)
        cmd_chk([*cmd, '.', f"{samba_host}:/home/ftp/pub/rsync/dev/nightly"])


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A helper script for "nightly" tar files.', add_help=False)
    parser.add_argument('--make-tar', '-t', action='store_true', help=f"Create a new tar file in {dest}.")
    parser.add_argument('--upload', '-u', action='store_true', help="Upload the revised nightly dir to {samba_host}.")
    parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
    args = parser.parse_args()
    main()

# vim: sw=4 et
