#!@PYTHONEXE@
#    This file is part of GNUnet.
#    (C) 2010 Christian Grothoff (and other contributing authors)
#
#    GNUnet is free software: you can redistribute it and/or modify it
#    under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License,
#    or (at your option) any later version.
#
#    GNUnet is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    SPDX-License-Identifier: AGPL3.0-or-later
#
# Testcase for file-sharing command-line tools (recursive publishing & download)
import sys
import os
import subprocess
import re
import shutil
import tarfile
import filecmp

srcdir = "../.."
gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
if gnunet_pyexpect_dir not in sys.path:
    sys.path.append(gnunet_pyexpect_dir)

from gnunet_pyexpect import pexpect
from pydiffer import dcdiff

if os.name == 'posix':
    download = './gnunet-download'
    gnunetarm = 'gnunet-arm'
    publish = './gnunet-publish'
    unindex = './gnunet-unindex'
    search = './gnunet-search'
    directory = './gnunet-directory'
elif os.name == 'nt':
    download = './gnunet-download.exe'
    gnunetarm = 'gnunet-arm.exe'
    publish = './gnunet-publish.exe'
    unindex = './gnunet-unindex.exe'
    search = './gnunet-search.exe'
    directory = './gnunet-directory.exe'

if os.name == "nt":
    shutil.rmtree(
        os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True
    )
else:
    shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)

arm = subprocess.Popen([gnunetarm, '-sq', '-c', 'test_gnunet_fs_rec_data.conf'])
arm.communicate()

tar = tarfile.open('test_gnunet_fs_rec_data.tgz')
tar.extractall()
# first, basic publish-search-download run
try:
    pub = pexpect()
    pub.spawn(
        None, [
            publish, '-c', 'test_gnunet_fs_rec_data.conf', '-k', 'testdir',
            'dir/'
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    # Can't say much for publishing, except that the last one is the toplevel directory
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect("stdout", re.compile(r"Publishing `.+' done\.\r?\n"))
    pub.expect(
        "stdout",
        re.compile(
            r"URI is `gnunet://fs/chk/[A-Z0-9]{103}\.[A-Z0-9]{103}\.\d+'\.\r?\n"
        )
    )
    pub.expect(
        "stdout", re.compile(r"Publishing `.+[\\/]dir[\\/]' done\.\r?\n")
    )
    m = pub.expect("stdout", re.compile(r".+\r?\n"))
    if not m:
        sys.exit(3)
    output = m.string
    url = output[output.find("`") + 1:output.find("'")]

    down = pexpect()
    down.spawn(
        None, [
            download, '-c', 'test_gnunet_fs_rec_data.conf', '-R', '-o',
            'rdir.gnd', url
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    down.expect("stdout", re.compile(r"Downloading `rdir.gnd' done (.*).\r?\n"))

    d = pexpect()
    d.spawn(
        None, [directory, '-c', 'test_gnunet_fs_rec_data.conf', 'rdir/a.gnd'],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    d.expect("stdout", re.compile(r"Directory `a/' meta data:\r?\n"))
    d.expect("stdout", re.compile(r"Directory `a/' contents:\r?\n"))
    d.expect("stdout", re.compile(r"COPYING (.*)\r?\n"))
    d.expect("stdout", re.compile(r"INSTALL (.*)\r?\n"))

    os.remove("rdir/b.gnd")
    os.remove("rdir/a.gnd")
    diff = dcdiff('dir', 'rdir')
    if len(diff) != 0:
        raise Exception(
            "Unexpected difference between source directory and downloaded result:\n{}"
            .format(diff)
        )

finally:
    arm = subprocess.Popen([
        gnunetarm, '-eq', '-c', 'test_gnunet_fs_rec_data.conf'
    ])
    arm.communicate()
    if os.name == "nt":
        shutil.rmtree(
            os.path.join(os.getenv("TEMP"), "gnunet-test-fs-py-rec"), True
        )
    else:
        shutil.rmtree("/tmp/gnunet-test-fs-py-rec", True)
    shutil.rmtree("dir", True)
    shutil.rmtree("rdir", True)
    shutil.rmtree("rdir.gnd", True)
