{ perl, fetchgit, fetchurl, stdenvNoCC, lib, ... }:

with perl.pkgs;

let
  ClassMeasure = buildPerlModule {
    pname = "Class-Measure";
    version = "0.10";
    src = fetchurl {
      url = "mirror://cpan/authors/id/B/BL/BLUEFEET/Class-Measure-0.10.tar.gz";
      hash = "sha256-wLeesJpmzEH7g6rb0kh0NytGWnRAe5bgcimU7vv9JMo=";
    };
    buildInputs = [ ModuleBuildTiny Test2Suite ];
    propagatedBuildInputs = [ SubExporter ];
    meta = {
      homepage = "https://github.com/bluefeet/Class-Measure";
      description = "Create, compare, and convert units of measurement";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  MojoliciousPluginAuthentication = buildPerlPackage {
    pname = "Mojolicious-Plugin-Authentication";
    version = "1.39";
    src = fetchurl {
      url =
        "mirror://cpan/authors/id/J/JJ/JJATRIA/Mojolicious-Plugin-Authentication-1.39.tar.gz";
      sha256 = "sha256-n5nL31ysqj+j+WG+6U3pcibRd6NqFee6MDrnnRModNE=";
    };
    propagatedBuildInputs = [ Mojolicious ];
    meta = {
      homepage =
        "https://github.com/benvanstaveren/Mojolicious-Plugin-Authentication";
      description = "A plugin to make authentication a bit easier";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  MojoliciousPluginOAuth2 = buildPerlPackage {
    pname = "Mojolicious-Plugin-OAuth2";
    version = "2.02";
    src = fetchurl {
      url = "mirror://cpan/authors/id/J/JH/JHTHORSEN/Mojolicious-Plugin-OAuth2-2.02.tar.gz";
      hash = "sha256-E0kPucaJR+ZbFDrenWsPQnC0EhV+rFdyH3rUl8NHIYE=";
    };
    propagatedBuildInputs = [ IOSocketSSL Mojolicious ];
    meta = {
      homepage = "https://github.com/marcusramberg/Mojolicious-Plugin-OAuth2";
      description = "Auth against OAuth2 APIs including OpenID Connect";
      license = lib.licenses.artistic2;
    };
  };
  GISDistance = buildPerlModule {
    pname = "GIS-Distance";
    version = "0.20";
    src = fetchurl {
      url = "mirror://cpan/authors/id/B/BL/BLUEFEET/GIS-Distance-0.20.tar.gz";
      hash = "sha256-srL4d0zdq24+SdNJiO+v6P4NUA/21Xth+GYUCVvxQj4=";
    };
    buildInputs = [ ModuleBuildTiny Test2Suite ];
    propagatedBuildInputs = [ ClassMeasure ConstFast namespaceclean strictures ];
    meta = {
      homepage = "https://github.com/bluefeet/GIS-Distance";
      description = "Calculate geographic distances";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  GISDistanceFast = buildPerlModule {
    pname = "GIS-Distance-Fast";
    version = "0.16";
    src = fetchurl {
      url = "mirror://cpan/authors/id/B/BL/BLUEFEET/GIS-Distance-Fast-0.16.tar.gz";
      hash = "sha256-xn8/80PKpZOj+wRx3JjsSPK+87S00uALom4zk87sB0E=";
    };
    buildInputs = [ ModuleBuildTiny Test2Suite ];
    propagatedBuildInputs = [ GISDistance namespaceclean strictures ];
    meta = {
      homepage = "https://github.com/bluefeet/GIS-Distance-Fast";
      description = "C implementation of GIS::Distance formulas";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  TravelStatusDEDeutscheBahn = buildPerlModule {
    pname = "Travel-Status-DE-DeutscheBahn";
    version = "6.03";
    src = fetchurl {
      url = "mirror://cpan/authors/id/D/DE/DERF/Travel-Status-DE-DeutscheBahn-6.03.tar.gz";
      hash = "sha256-5SEmrdZagl9h0e2F5dpFuwy4KNeh7YsnuuT6WrWpkLE=";
    };
    buildInputs = [ FileSlurp TestCompile TestPod ];
    propagatedBuildInputs = [ ClassAccessor DateTime DateTimeFormatStrptime JSON LWP LWPProtocolhttps ListMoreUtils ];
    meta = {
      description = "Interface to the online arrival/departure";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  TravelStatusDEDBWagenreihung = buildPerlModule {
    pname = "Travel-Status-DE-DBWagenreihung";
    version = "0.12";
    src = fetchurl {
      url = "mirror://cpan/authors/id/D/DE/DERF/Travel-Status-DE-DBWagenreihung-0.12.tar.gz";
      hash = "sha256-vc1cyuL4EjwQG9B34+MbL6AxFUFIyjPKstbPH8rtIk4=";
    };
    buildInputs = [ TestCompile TestPod ];
    propagatedBuildInputs = [ ClassAccessor JSON LWP TravelStatusDEIRIS ];
    meta = {
      description = "Interface to Deutsche Bahn Wagon Order API";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  TravelStatusDEIRIS = buildPerlModule {
    pname = "Travel-Status-DE-IRIS";
    version = "1.96";
    src = fetchurl {
      url = "mirror://cpan/authors/id/D/DE/DERF/Travel-Status-DE-IRIS-1.96.tar.gz";
      hash = "sha256-ncXTqCEF7il1pjX8D4gqmAv//AD0o5MrIiev5aViJcc=";
    };
    buildInputs = [ FileSlurp JSON TestCompile TestFatal TestNumberDelta TestPod TextCSV ];
    propagatedBuildInputs = [ ClassAccessor DateTime DateTimeFormatStrptime GISDistance LWP LWPProtocolhttps ListCompare ListMoreUtils ListUtilsBy TextLevenshteinXS XMLLibXML ];
    meta = {
      description = "Interface to IRIS based web departure monitors";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  Cache = buildPerlModule {
    pname = "Cache";
    version = "2.11";
    src = fetchurl {
      url = "mirror://cpan/authors/id/S/SH/SHLOMIF/Cache-2.11.tar.gz";
      sha256 =
        "e1d2d89677981166abc5bb6e5ecc6471f001f13eb56d5be9544d8047dc08a592";
    };
    buildInputs = [ Test2Suite ];
    propagatedBuildInputs = [ FileNFSLock Heap IOString TimeDate DBFile ];
    meta = {
      description = "Provide a generic Cache mechanism";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };
  Heap = buildPerlPackage {
    pname = "Heap";
    version = "0.80";
    src = fetchurl {
      url = "mirror://cpan/authors/id/J/JM/JMM/Heap-0.80.tar.gz";
      sha256 =
        "ccda29f3c93176ad0fdfff4dd6f5e4ac90b370cba4b028386b7343bf64139bde";
    };
    meta = { };
  };
  CAMPDF = buildPerlPackage {
    pname = "CAM-PDF";
    version = "1.60";
    src = fetchurl {
      url = "mirror://cpan/authors/id/C/CD/CDOLAN/CAM-PDF-1.60.tar.gz";
      sha256 =
        "e76afc7f38a624924a77c5c988c36c9e388bfa7716e75cd397fef8e1b42ebb89";
    };
    propagatedBuildInputs = [ CryptRC4 TextPDF ];
    meta = {
      description = "PDF manipulation library";
      license = with lib.licenses; [ artistic1 gpl1Plus ];
    };
  };

  perlWithPackages = perl.withPackages (_: [
    Cache
    CAMPDF
    CryptEksblowfish
    DateTime
    DateTimeFormatStrptime
    EmailSender
    GISDistance
    GISDistanceFast
    ListUtilsBy
    MIMEtools
    Mojolicious
    MojoliciousPluginAuthentication
    MojoliciousPluginOAuth2
    MojoPg
    TextCSV
    TextMarkdown
    TravelStatusDEDBWagenreihung
    TravelStatusDEDeutscheBahn
    TravelStatusDEIRIS
    UUIDTiny
    JSON
    JSONXS
    DBFile
    FileSlurp
  ]);

  # Note: set location of config via $MOJO_CONFIG when running index.pl
  travelynx = stdenvNoCC.mkDerivation {
    pname = "travelynx";
    version = "2.6.7";
    src = fetchgit {
      url = "https://github.com/derf/travelynx";
      rev = "2.6.7";
      sha256 = "sha256-1bmOpoUkCMB8b/dkWlrUkzP2wqkLFg4+OaruC2UDKMo=";
    };
    buildInputs = [ perlWithPackages ];
    buildPhase = ''
      substituteInPlace index.pl \
        --replace "/usr/bin/env perl" ${perlWithPackages.outPath}/bin/perl
      chmod +x index.pl
    '';
    installPhase = ''
      mkdir -p $out
      cp -r * $out
    '';
    passthru.perl = perlWithPackages;
  };

in travelynx