# -*- 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.44.1 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  987d6902a6cb4c4079a66e1e0c5aa1517b91aefc \
                    sha256  fa5abe50f190de58bba273352928fbae80a7214d0711937d27914edf7af2da1e \
                    size    1181833

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}/bin/*.sh] {*}[glob ${worksrcpath}/lib/*/*.sh]
    # disable self-update and self-remove
    reinplace "s|update_mole\$|echo \"Command disabled, please use \\\\\`sudo port upgrade ${name}\\\\\` to update [string tolower ${github.project}]\"; exit 1|" ${worksrcpath}/mole
    reinplace "s|update_mole \.\*|echo \"Command disabled, please use \\\\\`sudo port upgrade ${name}\\\\\` to update [string tolower ${github.project}]\"; exit 1|" ${worksrcpath}/mole
    reinplace "s|remove_mole \.\*|echo \"Command disabled, please use \\\\\`sudo port uninstall ${name}\\\\\` to remove [string tolower ${github.project}]\"; exit 1|" ${worksrcpath}/mole
}

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/vendor_completions.d/
    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})
    }
}

