/*
    doc.me - me-doc targets
 */

Me.load({

    modules: [
        'doc.es',
    ],

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

    targets: {
        doc: {
            depends: ['patch', 'manpages', 'api', 'clean-doc', 'ssi'],
        },

        'patch': {},
        'api': {},
        'clean-doc': {},

        manpages: {
            depends: ['all'],
            action: `
                let man = Cmd.locate('man')
                let man2html = Cmd.locate('man2html')
                if (man && man2html) {
                    for each (file in ls(['${SRC}/doc/man/*.1', '${SRC}/doc/man/*.3'])) {
                        let name = file.basename
                        trace('Generate', 'Manual page for ' + name)
                        let response = run([man, file], {noshow: true})
                        file.replaceExt('txt').write(response.replace(/.\b/g, ''))
                        let cmd = Cmd([man2html, '-u'], {detach: true})
                        cmd.write(response)
                        cmd.finalize()
                        file.replaceExt('html').write(cmd.response)
                    }
                } else {
                    trace('Skip', 'manpages bacause man or man2html cannot be located')
                }
            `,
        },

        ssi: {
            action: `
                let dsi = Cmd.locate('dsi')
                if (dsi) {
                    if (!me.dir.src.join('doc/dsi').exists) {
                        trace('Skip', 'No server side includes')
                    } else {
                        trace('Process', 'Html server side includes (dsi)')
                        let doc = me.dir.src.join('doc')
                        for each (file in ls('${SRC}/doc/**.html')) {
                            file = file.relativeTo(doc)
                            vtrace('Patch', file)
                            run([dsi, file], {dir: doc})
                        }
                    }
                } else {
                    trace('Skip', 'ssi bacause dsi cannot be located')
                }
            `,
        },

        tidy: {
            depends: ['doc', 'ssi'],
            enable: false,
            action: `
                let tidy = Cmd.locate('tidy')
                if (tidy) {
                    trace('Tidy', 'HTML doc')
                    let doc = me.dir.src.join('doc')
                    for each (file in ls('${SRC}/doc/**html')) {
                        trace('Tidy', file)
                        file = file.relativeTo(doc)
                        run(tidy + ' -q -config tidy.conf ' + file)
                    }
                } else {
                    trace('Skip', 'tidy bacause "tidy" cannot be located')
                }
            `,
        },
    },
})
