# -*- octave -*-
# Copyright © 2018, 2020, 2022  Rafael Laboissière <rafael@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <https://www.gnu.org/licenses/>.

1;
function out = sysout (cmd)
    [~, out] = system (cmd);
    out = deblank (out);
endfunction

if (exist ("./PKG_ADD") == 2)
    movefile ("PKG_ADD", "PKG_ADD.bak");
endif

pkg ("prefix", argv () {1}, argv () {2});

pkg ("local_list", fullfile (pwd (), "local-list"));
pkg ("global_list", fullfile (pwd (), "global-list"));

save_header_format_string ("# Generated by Octave during Debian package build");

if (exist ("PKG_ADD.bak") == 2)
    movefile ("PKG_ADD.bak", "PKG_ADD");
endif

global name
[updir, name, ext] = fileparts (pwd ());
pkgdir = sprintf ("%s%s", name, ext);
tarball = sprintf ("%s/%s.tar.gz", tempdir (), pkgdir);

cd (updir);

system (sprintf ("tar --create --gzip --file %s --exclude=debian --exclude=.git --exclude=.pc %s",
                 tarball, pkgdir));

# The following hack (defining a function and calling it immediatly
# after) avoids problems when the upstream PKG_DEL contains commands
# that remove all the users variables (like octave-datatypes, see
# https://github.com/pr0m1th3as/datatypes/issues/47)

function install_pkg_from_tarball (tarball)
    pkg ("-verbose", "-nodeps", "install", tarball);
    unlink (tarball);
endfunction

# The following hack should avoid the reproducibility problem
# https://tests.reproducible-builds.org/debian/issues/unstable/build_path_captured_by_octave_issue.html
# “octave packages embed the build path, typically with a /src suffix.”

function d = tempname()
    global name
    d = ["/tmp/" name "-build-dir"];
endfunction 

install_pkg_from_tarball (tarball);

# Local Variables:
# indent-tabs-mode: nil
# tab-width: 4
# End:
