# -*- 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

name                mssql-tools
version             18.7.1.1
revision            0
set major           [lindex [split ${version} .] 0]
categories          databases
platforms           {darwin any}
supported_archs     arm64 x86_64
maintainers         {jann @roederja} openmaintainer
license             Restrictive

description         Sqlcmd and Bcp for Microsoft(R) SQL Server(R)
long_description    Sqlcmd and Bcp for Microsoft(R) SQL Server(R)

homepage            https://learn.microsoft.com/sql/tools/sqlcmd-utility
master_sites        https://download.microsoft.com/download/c22bfd87-5eaa-46a3-ace6-bc5b6894f692/

switch ${configure.build_arch} {
    x86_64 {
        distname    ${name}${major}-${version}-amd64
        checksums   rmd160  5a651f410d45f0285bdd516dcd6bf05c26c1a9ff \
                    sha256  0a25977c4526867a927f4e1800b29c68ac6b67eb07a27711741302e232b7140d \
                    size    307024
        set old_prefix  /usr/local
    }
    arm64 {
        distname    ${name}${major}-${version}-arm64
        checksums   rmd160  30660374ce2a58c238d9d18bf866e4a657dd4c58 \
                    sha256  fb457ef3574fa835ee6d8fab77f85109b092c7bd8cc988b13947bb13357ea7aa \
                    size    275892
        set old_prefix  /opt/homebrew
    }
}
worksrcdir          ${name}${major}-${version}

# sqlcmd and bcp 18 load "ODBC Driver 18 for SQL Server"; msodbcsql provides
# driver 18 only on macOS 10.15 and later.
depends_run         port:msodbcsql

if {${os.platform} eq "darwin" && ${os.major} < 19} {
    known_fail      yes
    pre-fetch {
        ui_error "${name} @${version} requires macOS 10.15 or later."
        return -code error "incompatible macOS version"
    }
}

use_configure       no

build {
    foreach bin {bcp sqlcmd} {
        system "install_name_tool -change ${old_prefix}/lib/libodbc.2.dylib ${prefix}/lib/libodbc.2.dylib ${worksrcpath}/bin/${bin}"
    }
}

destroot {
    xinstall -m 555 -W ${worksrcpath}/bin sqlcmd bcp ${destroot}${prefix}/bin
    xinstall -d ${destroot}${prefix}/share/resources/en_US
    xinstall -m 444 -W ${worksrcpath}/share/resources/en_US BatchParserGrammar.dfa BatchParserGrammar.llr bcp.rll SQLCMD.rll \
        ${destroot}${prefix}/share/resources/en_US
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 -W ${worksrcpath}/usr/share/doc/${name}${major} LICENSE.txt THIRDPARTYNOTICES.txt \
        ${destroot}${prefix}/share/doc/${name}
}

test.run            yes
test {
    system "${destroot}${prefix}/bin/sqlcmd -? | grep -q 'Microsoft (R) SQL Server Command Line Tool'"
    system "${destroot}${prefix}/bin/bcp -v | grep -q 'BCP - Bulk Copy Program for Microsoft SQL Server'"
}

# Microsoft publishes the version of the macOS tarballs only in its
# mssql-release repository.
livecheck.type      regex
livecheck.url       https://raw.githubusercontent.com/microsoft/homebrew-mssql-release/master/Formula/${name}${major}.rb
livecheck.regex     {version "(\d+(?:\.\d+)+)"}
