# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           golang 1.0

go.setup            github.com/tw93/Mole 1.19.0 V
name                [string tolower ${github.project}]-cleaner
revision            0
categories          sysutils
platforms           macosx
license             MIT
maintainers         {i0ntempest @i0ntempest} openmaintainer
description         Deep clean and optimize your Mac
long_description    ${github.project} is a Mac cleaning tool that can free up tens of GBs in one go.

checksums           rmd160  d08dedaec7fa5e5a669a5836478ae827591399d7 \
                    sha256  9a211c503f9dc530f4fbc0447bcae4b078d8f37aef45b74b30698e57215bc2d4 \
                    size    236136

depends_run-append  port:bash

set script_dir      ${prefix}/libexec/[string tolower ${github.project}]/
post-patch {
    reinplace "s|SCRIPT_DIR=\".*\"|SCRIPT_DIR=${script_dir}|" ${worksrcpath}/mole
    reinplace "s|#!/bin/bash|#!${prefix}/bin/bash|" ${worksrcpath}/mole {*}[glob ${worksrcpath}/*.sh] {*}[glob ${worksrcpath}/lib/*/*.sh]
}

go.offline_build    no

set time [clock format [clock seconds] -format %Y%m%dT%H%M%S]
build.args-append   -ldflags=\"-X 'main.Version=v${version}' -X 'main.BuildTime=${time}'\"
build.cmd           "cd ${worksrcpath}/cmd/analyze && ${build.cmd} -o ../../bin/analyze-go && cd ${worksrcpath}/cmd/status && ${build.cmd} -o ../../bin/status-go"

destroot {
    xinstall -m 755 ${worksrcpath}/[string tolower ${github.project}] ${destroot}${prefix}/bin
    ln -s [string tolower ${github.project}] ${destroot}${prefix}/bin/mo
    xinstall -d ${destroot}${script_dir}
    copy ${worksrcpath}/bin ${worksrcpath}/lib ${destroot}${script_dir}
    # All supported shells: bash, fish, zsh
    set comp_path(bash) ${destroot}${prefix}/share/bash-completion/completions/
    set comp_path(fish) ${destroot}${prefix}/share/fish/completions/
    set comp_path(zsh)  ${destroot}${prefix}/share/zsh/site-functions/
    set comp_file(bash) [string tolower ${github.project}]
    set comp_file(fish) [string tolower ${github.project}].fish
    set comp_file(zsh)  _[string tolower ${github.project}]
    set alt_comp_file(bash) mo
    set alt_comp_file(fish) mo.fish
    set alt_comp_file(zsh)  _mo
    foreach shell {bash fish zsh} {
        xinstall -d $comp_path(${shell})
        system -W ${worksrcpath}/bin "./completion.sh ${shell} > $comp_path(${shell})$comp_file(${shell})"
        ln -s $comp_file(${shell}) $comp_path(${shell})$alt_comp_file(${shell})
    }
}

