# -*- 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.20.0 V
name                [string tolower ${github.project}]-cleaner
revision            1
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  b81d5024857b3b7eb564904bb82add37240ff7b1 \
                    sha256  5c171ae3024d552d3f7e2c08148f6c7dc1a342bab89b81483e8a6d47d5d58b6a \
                    size    321676

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|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/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})
    }
}

