#!/usr/bin/env perl

# ISC LICENSE
# Copyright Stefan Hagen <sh+ports@codevoid.de>

# usage: ./mkport thing thing thing > Makefile
#        ./mkport -h
#
# "thing" is something you want in the port. It can be a module, distfile
# provider or build tool.
#
# The script is far from complete. I'm just adding constraints and
# support for key words along the way.
#
# The script runs top down and builds up constraints (for example no
# MASTER_SITES when GH_* is present. And prefills WANTLIB and *_DEPENDS
# based on modules it knows.
#
# The idea is to create a sensible default Makefile to start with.
# That means I won't add all variables for a given module. Just the ones
# that are most likely needed.
#
# DONE
# lua cmake qmake cpan qt5 qt6 python
#
# TODO
# erlang gnome gnu gnustep intltool java
#
# SKIPPED (rare use)
# apache-module asterisk-sounds fortran gcc4 clang heimdal imake qt4
#
# UNSURE (or unknown)
# dconf font gconf2
#
# USE PORTGEN
# go ruby perl
#
# MAYBE PORTGEN IN FUTURE?
# cabal cargo
#
# DIDN'T LOOK AT THOSE YET
# kde-applications kf5  mariadb meson mono  mozilla ocaml pear  pecl php
# postgresql scons tcl tk xfce4

use strict;
use warnings;
use File::Basename;

my %arg = map {$_ => 1} @ARGV;

# USAGE
if(exists $arg{"-h"}) {
      print "Usage: ".basename($0)." thing thing thing > Makefile\n";
      print "Things: ";
      open(my $f, '<', "$0") or die $!;
      my @lines = sort grep s/^exists \$arg\{"([^_]\w+)"\}.*/$1/g, <$f>;
      @lines = do { my %s; grep { !$s{$_}++ } @lines };
      while(my $l=shift(@lines)) { chomp($l); print "$l "; }
      print "\n";
      exit(0);
}

# DEFAULTS
$arg{"_distname"}     = "\${V}";
$arg{"_maintainer"}   = 'Stefan Hagen <sh+ports@codevoid.de>';
$arg{"_license"}      = "LICENSE";
$arg{"_mastersites"}  = "";
$arg{"_makeflags"}    = "";
$arg{"_testflags"}    = "";
$arg{"_wantlib"}      = [];
$arg{"_libdepends"}   = [];
$arg{"_rundepends"}   = [];
$arg{"_builddepends"} = [];
$arg{"_testdepends"}  = [];
$arg{"_configstyle"}  = "";

sub addlist {
    push(@{$arg{$_[0]}}, ( $_[1] ));
}

sub printlist {
    print "$_[0]".join(' ', sort @{$arg{$_[1]}})."\n\n"
}

# CONSTRAINTS
if(exists $arg{"github"}) {
    delete $arg{"_distname"};
    delete $arg{"_mastersites"};
}
if(exists $arg{"cpan"}) {
    delete $arg{"_mastersites"};
    delete $arg{"_makeflags"};
    delete $arg{"_testflags"};
    delete $arg{"_wantlib"};
    delete $arg{"_libdepends"};
    $arg{"_license"} = "Perl";
    $arg{"_configstyle"} = "modinst";
}
if(exists $arg{"autoconf"}) {
    $arg{"_configstyle"} = "autoconf";
}
if(exists $arg{"nobuild"}) {
    delete $arg{"_wantlib"};
    delete $arg{"_builddepends"};
    delete $arg{"_configstyle"};
    delete $arg{"_makeflags"};
}

# COMMENT
print "COMMENT =\t\n\n";

# PORT INFO
print "V =\t\t\n";
exists $arg{"github"}
    and print "GH_ACCOUNT =\t\n"
    and print "GH_PROJECT =\t\n"
    and print "GH_TAGNAME =\t\${V}\t\n"
    and print "\#GH_COMMIT =\t\n";
exists $arg{"cpan"}
    and print "PKG_ARCH =\t*\n";
exists $arg{"_distname"}
    and print "DISTNAME =\t\${V}\n"
    and print "\#PKGNAME =\t\${DISTNAME}-\${V}\n";
exists $arg{"github"}
    and print "\#PKGNAME =\t\${GH_PROJECT}-\${V}\n";

print "\nCATEGORIES =\t\n\n";

# HOMEPAGE
print "HOMEPAGE =\t\n\n";

# MAINTAINER
exists $arg{"_maintainer"}
    and print "MAINTAINER =\t", $arg{'_maintainer'}, "\n\n";
exists $arg{"cpan"}
    and print "CPAN_AUTHOR =\t\n\n";

# LICENSE
exists $arg{"_license"}
    and print "\# $arg{'_license'}\n";
print "PERMIT_PACKAGE =\tYes\n\n";

# MASTER_SITES
exists $arg{"_mastersites"}
    and print "MASTER_SITES =\t\n"
    and print "\#EXTRACT_SUFX =\t\n\n";

# MODULES
exists $arg{"cmake"}   and $arg{"_modules"} .= "devel/cmake ";
exists $arg{"lua"}     and $arg{"_modules"} .= "devel/lua ";
exists $arg{"qmake"}   and $arg{"_modules"} .= "devel/qmake ";
exists $arg{"qt5"}     and $arg{"_modules"} .= "x11/qt5 ";
exists $arg{"qt6"}     and $arg{"_modules"} .= "x11/qt6 ";
exists $arg{"python"}  and $arg{"_modules"} .= "lang/python ";
exists $arg{"ruby"}    and $arg{"_modules"} .= "lang/ruby ";
exists $arg{"tk"}      and $arg{"_modules"} .= "x11/tk ";
exists $arg{"gnome"}   and $arg{"_modules"} .= "gnome ";
exists $arg{"perl"}    and $arg{"_modules"} .= "perl ";
exists $arg{"cpan"}    and $arg{"_modules"} .= "cpan ";
exists $arg{"php"}     and $arg{"_modules"} .= "php ";
exists $arg{"fortran"} and $arg{"_modules"} .= "fortran ";
exists $arg{"cabal"}   and $arg{"_modules"} .= "devel/cabal ";
exists $arg{"cargo"}   and $arg{"_modules"} .= "devel/cargo ";
exists $arg{"java"}    and $arg{"_modules"} .= "java ";
exists $arg{"_modules"}
     and print "MODULES =\t$arg{'_modules'}\n";

# MOD*
exists $arg{"qt5"}
    and print "\#MODQT5_DEPS =\t\n"
    and print "\#MODQT5USE_CXX11 = No\t\n";
exists $arg{"qt6"}
    and print "\#MODQT6_DEPS =\t\n"
    and print "\#MODQT6USE_CXX11 = No\t\n";
exists $arg{"lua"}
    and print "MODLUA_VERSION =\t\${MODLUA_DEFAULT_VERSION}\n";
exists $arg{"python"}
    and print "MODPI_VERSION =\t\${MODPY_DEFAULT_VERSION}\n";
exists $arg{"qmake"}
    and print "\#MODQMAKEPROJECTS =\t\n";
exists $arg{"qmake"}
    and print "\#MODQMAKEARGS =\t\n";
exists $arg{"_modules"}
    and print "\n";

# WANTLIB
exists $arg{"lua"}
    and addlist("_wantlib","\${MODLUA_WANTLIB}");
exists $arg{"python"}
    and addlist("_wantlib","\${MODPY_WANTLIB}");
exists $arg{"cmake"}
    and addlist("_wantlib","\${COMPILER_LIBCXX}");

exists $arg{"_wantlib"}
    and print "\# ?uses pledge()?\n"
    and printlist("WANTLIB =\t","_wantlib");

# LIB_DEPENDS
exists $arg{"lua"}
    and addlist("_libdepends", "\${MODLUA_LIB_DEPENDS}");
exists $arg{"python"}
    and addlist("_libdepends", "\${MODPY_LIB_DEPENDS}");

exists $arg{"_libdepends"}
    and printlist("LIB_DEPENDS =\t", "_libdepends");

# RUN_DEPENDS
exists $arg{"_rundepends"}
    and printlist("RUN_DEPENDS =\t", "_rundepends");

# BUILD_DEPENDS
exists $arg{"cpan"}
    and addlist("_builddepends", "\${RUN_DEPENDS}");
exists $arg{"autoconf"}
    and addlist("_builddepends", "\${MODGNU_AUTOCONF_DEPENDS}")
    and addlist("_builddepends", "\${MODGNU_AUTOMAKE_DEPENDS}")
    and addlist("_builddepends", "devel/libtool");
exists $arg{"_builddepends"}
    and printlist("BUILD_DEPENDS =\t", "_builddepends");

# TEST_DEPENDS
exists $arg{"_testflags"}
    and printlist("\#TEST_DEPENDS =\t", "_testdepends");

# COMPILER
exists $arg{"gcc"}
    and print "COMPILER=\tbase-clang ports-gcc\n\n";

# CONFIGURE
exists $arg{"autoconf"}
    and print "\#AUTOCONF_VERSION =\t?.??\n"
    and print "\#AUTOMAKE_VERSION =\t?.??\n\n";
exists $arg{"_configstyle"}
    and print "\#CONFIGURE_STYLE =\t", $arg{'_configstyle'}, "\n\n";

# FLAGS
exists $arg{"_makeflags"}
    and print "\#MAKE_FLAGS =\t\n\n";

exists $arg{"nobuild"}
    and print "NO_BUILD =\tYes\n\n";

# USE FLAGS
exists $arg{"gmake"}
    and print "USE_GMAKE =\tYes\n\n";

# TESTS
exists $arg{"_testflags"}
    and print "\#NO_TEST =\tYes\n"
    and print "\#TEST_TARGET =\t\n\n";
exists $arg{"cpan"}
    and print "\#MODCPAN_EXAMPLES =\tYes\n\n";

print ".include <bsd.port.mk>\n";
