import java.util.stream.Collectors

def distDir = "${project.layout.buildDirectory.get()}/dist"

task licenses(type: DefaultTask) {
    doLast {
        copy {
            from "$projectDir/licenses/"
            into "$distDir/licenses/"
            include '*.license'
            rename { String name ->
                name.replace("license", "txt")
            }
        }
    }
}

def debugArguments = file("$projectDir/debug/debug_arguments.txt").text.lines().map(s -> '"' + s + '"').collect(Collectors.joining(' '))

if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
    task baseDist(type: DefaultTask) {
        doLast {
            copy {
                from "$distDir/jpackage/xpiped"
                into "$distDir/base/app"
            }
            copy {
                from "$projectDir/logo/logo.ico"
                into "$distDir/base/app"
            }
            copy {
                from "$projectDir/fonts"
                into "$distDir/base/app/fonts"
            }
            copy {
                from "$rootDir/lang"
                into "$distDir/base/app/lang"
            }

            file("$distDir/base/app/scripts").mkdirs()
            def debug = file("$distDir/base/app/scripts/xpiped_debug.bat")
            debug.text = file("$projectDir/debug/windows/xpiped_debug.bat").text.replace(
                    'JVM-ARGS',
                    debugArguments)
            debug.setExecutable(true)

            copy {
                from "$distDir/licenses"
                into "$distDir/base/licenses"
            }
        }
    }
} else if (org.gradle.internal.os.OperatingSystem.current().isLinux()) {
    task baseDist(type: DefaultTask) {
        doLast {
            copy {
                from "$distDir/jpackage/xpiped"
                into "$distDir/base/app"
            }
            copy {
                from "$projectDir/logo/logo.png"
                into "$distDir/base/"
            }
            copy {
                from "$projectDir/fonts"
                into "$distDir/base/app/fonts"
            }
            copy {
                from "$rootDir/lang"
                into "$distDir/base/app/lang"
            }

            // Fixes a JPackage bug
            copy {
                from "$distDir/base/app/lib/app/xpiped.cfg"
                into "$distDir/base/app"
            }

            file("$distDir/base/app/scripts").mkdirs()
            def debug = file("$distDir/base/app/scripts/xpiped_debug.sh")
            debug.text = file("$projectDir/debug/linux/xpiped_debug.sh").text.replace(
                    'JVM-ARGS',
                    debugArguments)
            debug.setExecutable(true, false)

            copy {
                from "$distDir/licenses"
                into "$distDir/base/licenses"
            }
        }
    }
} else {
    task baseDist(type: DefaultTask) {
        doLast {
            def app = "${productName}.app"
            copy {
                from "$distDir/jpackage/xpiped.app/Contents"
                into "$distDir/$app/Contents/"
            }
            copy {
                from "$projectDir/logo/logo.icns"
                into "$distDir/$app/Contents/Resources/"
            }
            copy {
                from "$distDir/licenses"
                into "$distDir/$app/Contents/Resources/licenses"
            }
            copy {
                from "$projectDir/fonts"
                into "$distDir/$app/Contents/Resources/fonts"
            }
            copy {
                from "$rootDir/lang"
                into "$distDir/$app/Contents/Resources/lang"
            }

            file("$distDir/$app/Contents/Resources/scripts").mkdirs()
            def debug = file("$distDir/$app/Contents/Resources/scripts/xpiped_debug.sh")
            debug.text = file("$projectDir/debug/mac/xpiped_debug.sh").text.replace(
                    'JVM-ARGS',
                    debugArguments)
            debug.setExecutable(true, false)
        }
    }
}

baseDist.dependsOn(licenses)
baseDist.dependsOn(jpackage)
dist.dependsOn(baseDist)
