#!/usr/bin/env python

# setup.py
#
# Project: AutoArchive
# License: GNU GPLv3
#
# Copyright (C) 2003 - 2026 Róbert Čerňanský



"""The setup script for AutoArchive."""



import os, os.path, glob
from setuptools import setup
from setuptools.command.sdist import sdist

import sys
sys.path.insert(0, ".")
from AutoArchive._meta import _Meta
del sys.path[0]



class PreserveLinksSdist(sdist):

    def copy_file(self, src, dst, preserve_mode = 1, preserve_times = 1, link = None, level = 1):
        """Copy a file with preservation of symbolic links in mind.

        If ``src`` is symlink pointing to a relative destination, ``dst`` will be created as a symlink pointing to
        the same destination.  If ``dst`` is directory, symlink with the same name as ``src`` will be created in
        ``dst``.  If ``src`` if not symlink then base :meth:`copy_file()` method will be called."""

        copied = False

        # if src is relative symbolic link, create dst as symlink pointing to the same destination
        if os.path.islink(src):
            linkDestination = os.readlink(src)
            if not os.path.isabs(linkDestination):

                if os.path.isdir(dst):
                    dstFile = os.path.join(dst, os.path.basename(src))
                else:
                    dstFile = dst

                if not self.dry_run:
                    os.symlink(linkDestination, dstFile)
                copied = True

        # call base copy_file() method if symlink was not created
        if not copied:
            dstFile, copied = super().copy_file(src, dst, preserve_mode = preserve_mode,
                                                preserve_times = preserve_times, link = link, level = level)

        return dstFile, copied



def findDataFiles(destDir, srcDir):
    """Returns content of ``srcDir`` in a format required by ``data_files``."""

    dataFiles = []
    srcDirName = os.path.dirname(srcDir)
    for root, dirs, files in os.walk(srcDir):
        if files:
            dataFiles.append(
                (os.path.join(destDir, os.path.relpath(root, srcDirName)),
                 list(map(lambda f: os.path.join(root, f), files))))
    return dataFiles



docDir = os.path.join("share/doc", _Meta.PACKAGE_NAME + "-" + _Meta.VERSION)

dataFiles = [
    (os.path.join("share", _Meta.PACKAGE_NAME), ["data/configuration/aa.conf.dist"]),
    ("share/man/man1", ["doc/user/man/aa.1", "doc/user/man/autoarchive.1"]),
    ("share/man/man5", ["doc/user/man/aa.conf.5",
                        "doc/user/man/aa_arch_spec.5"]),
    (docDir, ["README", "README.sk", "NEWS", "COPYING"]),
    (os.path.join(docDir, "examples"), glob.glob("doc/user/examples/*"))
    ]

dataFiles.extend(findDataFiles(docDir, "doc/user/html"))

setup(
    long_description = """\
**AutoArchive** is a simple utility to help create backups more easily.  The
idea of the program is that all essential information for creating a single
backup---such as list of directories that should be archived, the archive name,
etc.---is stored in a single file -- the `archive specification file`.  It can
use ‘tar’ for creating archives, it has a command line interface and supports
incremental backups.""",
    download_url = str.format(
        "https://sourceforge.net/projects/autoarchive/files/autoarchive/{0}/autoarchive-{0}.tar.gz/download",
        _Meta.VERSION),
    data_files = dataFiles,

    cmdclass = {"sdist": PreserveLinksSdist},
)
