/*
    package.me -- me-package MakeMe file
 */

Me.load({
    blend: [
        'manifest.me',
    ],

    modules: [
        'package.es',
    ],

    scripts: {
        postloadall: `
            if (me.platform.like == 'windows') {
                me.prefixes = me['windows-prefixes']
            }
        `,
    },

    internal: {
        platforms: [ 'local' ],
    },

    'package-prefixes': {
        root:       '/',
        base:       '${prefixes.root}/usr/local',
        data:       '${prefixes.root}',
        state:      '${prefixes.root}/var',

        app:        '${prefixes.base}/lib/${settings.name}',
        vapp:       '${prefixes.app}/${settings.version}',

        bin:        '${prefixes.base}/bin',
        inc:        '${prefixes.base}/include',
        lib:        '${prefixes.base}/lib',
        man:        '${prefixes.base}/share/man',
        sbin:       '${prefixes.base}/sbin',

        etc:        '${prefixes.data}/etc/${settings.name}',
        web:        '${prefixes.state}/www/${settings.name}-default',
        log:        '${prefixes.state}/log/${settings.name}',
        spool:      '${prefixes.state}/spool/${settings.name}',
        cache:      '${prefixes.state}/spool/${settings.name}/cache',

        src:        '${settings.name}-${settings.version}',
    },

    targets: {
        check: {
            action: `
                let missing = checkInstalled()
                if (missing) {
                    let existing = checkUninstalled()
                    if (existing) {
                        trace('Status', 'Product partially removed, existing: ' + existing.join(' '))
                        for each (f in existing) {
                            dump(f, f.files())
                        }
                    } else {
                        trace('Status', 'Not installed')
                    }
                } else {
                    trace('Status', 'Installed')
                }
            `,
        },

        checkInstalled: {
            action: `
                let missing = checkInstalled()
                if (missing) {
                    trace('Missing', missing)
                    throw 'Missing directories: \n' + missing.join('\n')
                } else {
                    trace('Installed', 'Products directories are created')
                }
            `,
        },

        checkUninstalled: {
            action: `
                let existing = checkUninstalled()
                if (existing) {
                    trace('Existing', existing)
                    throw 'Existing directories: \n' + existing.join('\n')
                } else {
                    trace('Info', 'Product is not installed')
                }
            `,
        },

        install: {
            home: '${TOP}',
            action: ' ',
            generate: ' ',
            depends: [ 'all', 'stop', 'installBinary', 'start' ],
        },

        installBinary: {
            home: '${TOP}',
            action: "installBinary()",
            depends: [ 'all' ],
            'generate-capture': true,
        },

        installPackage: {
            home: '${TOP}',
            action: "installPackage()",
        },

        package: {
            home: '${TOP}',
            enable: "'${OS}.match(/linux|macosx|windows/)",
            depends: ['packageBinary', 'packageSource', 'packagePak'],
        },

        packageBinary: {
            home: '${TOP}',
            depends: ['all'],
            action: "packageBinary()",
        },

        packageSource: {
            home: '${TOP}',
            depends: ['all'],
            action: "packageSource()",
        },

        packagePak: {
            home: '${TOP}',
            depends: ['all'],
            action: "packagePak()",
        },

        uninstall: {
            home: '${TOP}',
            action: "uninstallBinary()",
            depends: [ 'all', 'stop' ],
            'generate-capture': true,
            generate: ' ',
        },

        uninstallPackage: {
            action: "uninstallPackage()",
        },

        whatInstalled: {
            action: "whatInstalled()",
        },

        cache: {
            home: '${TOP}',
            depends: ['all'],
            action: "cache()",
        },

        testPackage: {
            type: 'test',
            action: `
                let package = packageName()
                if (!package || !package.exists || me.settings.depth < 2) {
                    trace('Test', 'testPackage -- SKIPPED (No package to install)')
                } else if (me.settings.depth < 2) {
                    trace('Test', 'testPackage -- SKIPPED (Runs at depth 2 on windows or macosx)')
                } else {
                    if (Config.OS == 'macosx' && App.uid != 0) {
                        trace('Test', 'testPackage -- SKIPPED (Must run as root)')
                    } else {
                        trace('Test', 'testPackage')
                        if (me.prefixes.bin.join('uninstall').exists) {
                            trace('Test', 'Cleanup prior to installation')
                            uninstallPackage()
                            checkUninstalled()
                        }
                        installPackage()
                        if (missing = checkInstalled()) {
                            trace('Test', 'Install is missing files: ' + missing)
                        }
                        trace('Test', me.settings.name + ' -V')
                        assert(Cmd.run([me.prefixes.bin.join(me.settings.name), '-V']).trim() == me.settings.version)
                        uninstallPackage()
                        if (Config.OS == 'windows') App.sleep(1000)
                        if (existing = checkUninstalled()) {
                            trace('Warning', 'The uninstall for testPackage was not clean: ' + existing)
                        } else {
                            trace('Test', 'Uninstall is clean')
                        }
                    }
                }
            `,
        },

        /* Default stubs */
        start: { action: '', generate: ''},
        stop: { action: '',  generate: ''},
    },
})
