########################################################################
# OPENBSD PORT TOOLS
########################################################################

#
# CONFIGURATION
#

ME=sdk
MAKE_JOBS=$(sysctl -n hw.ncpuonline)
FETCH_JOBS=12

CVSROOT=$ME@cvs.openbsd.org:/cvs

case $(hostname -s) in
    x13)     PKGOPT="-Dsnap"; ;;
    openbsd) PKGOPT="-Dsnap"; ;;
esac

alias pkg_add="doas \pkg_add ${PKGOPT}"
alias pkg_delete="doas \pkg_delete ${PKGOPT}"
alias pkg_info="\pkg_info ${PKGOPT}"

#
# SUPER LAZY ALIASES FOR THE ALIASES
#

alias p=port-last
alias pj=port-jump
alias pmark=port-mark
alias po=port-out
alias pvim="port-patch"

#
# ALIASES
#

# navigation
alias port-jump="_portjump"
alias port-mark="_portmark"
alias port-last="_portlast"
alias port-out="_portout"

# cleaning
alias port-sweep="_portsweep"
alias port-clean="_portclean"

# making
alias port-refake="_refake"
alias port-reinstall="_reinstall"
alias port-repackage="_repackage"
alias port-remake="_remake"

# checking
alias port-check="_portcheck"
alias port-lib-depends-check="_portlibcheck"
alias port-homepage="_porthomepage"

# generating
alias port-modcargo-gen-crates="_portmodcargo_gen"
alias port-modcargo-gen-licenses="_portmodcargo_genlic"
alias port-modgo-gen-modules="_portmodgoupdate"

# source control
alias port-diff="_portgendiff"
alias cvs-update="_cvsupdate"
alias cvs-update-all="_cvsupdateall"

alias cvs-diff="_cvsdiff"
alias cvs-commit="_cvscommit"
alias cvs-release="_cvsrelease"
alias cvs-checkout="_cvscheckout"

# dpb
alias proot-rebuild="_prootrebuild"
alias proot-shell="_prootshell"
alias dpb-make="_dpbmake"

# other
alias port-roach="portroach-cli -m codevoid"
alias developers="_listdevs"
alias port-patch="_patchvim"
alias port-repology="_repology &"


#
# FUNCTIONS
#

_portsweep() {
    find . \( -name "*.orig" \
           -o -name "*.rej" \
           -o -empty \
           \) -delete
}

_portjump() {
    [ -z $1 ] && return 1
    cd /usr/ports

    LIST=$( ls -d1 \
            *$1* \
            */*$1* \
            mystuff/*$1* \
            mystuff/*/*$1* \
            openbsd-wip/*$1* \
            openbsd-wip/*/*$1* \
                2>/dev/null \
            | sort -u \
            | grep -Ev "pobj|CVS|locks|packages|distfiles"
          )

    if [ $(echo "$LIST" | wc -l) -gt 1 ]
    then
        if [ -f /usr/local/bin/fzf ]
        then
            cd $(echo "$LIST" | fzf -e -i)
        else
            select dir in $LIST
            do
                cd $dir
                return 0
            done
        fi
    else
        cd $LIST
    fi
}

_remake() {
    make clean=all
    make -j$FETCH_JOBS fetch
    make
}

_repackage() {
    make clean=packages
    doas -u _pbuild rm -f $(make show="PLIST_REPOSITORY ARCH FULLPKGNAME" |  tr '\n' '/' | sed 's|/$||g')
    FETCH_PACKAGES=No make package
}

_refake() {
    rm -f pkg/*.orig
    make clean=fake
    make fake
}

_reinstall() { make reinstall; }

_portgendiff() {(
    pwd | fgrep -q '/usr/ports/' || return 1
    set -A N $(make show="PKGNAME EPOCH REVISION")
    PN="${N[0]}${N[1]:+v${N[1]}}${N[2]:+p${N[2]}}"
    PD=$(pwd | cut -d"/" -f4,5)
    cd /usr/ports
    cvs -d $CVSROOT diff -uNp "${PD}" > ~/diffs/${PN}.diff
    echo ~/diffs/${PN}.diff
)}

_portclean() {(
    set -x
    cd /usr/ports
    doas rm -rf pobj/* plist logs packages bulk update distfiles/*
    mkdir -p plist logs packages bulk update
    doas make fix-permissions
)}

_cvscommand() { doas -u $ME cvs -d $CVSROOT $@; }
_cvsupdate() { _cvscommand -z1 -q up -Pd -A $@; }

_cvsupdateall() {(
    set -x
    cd /usr/src      && _cvsupdate
    cd /usr/xenocara && _cvsupdate
    cd /usr/ports    && _cvsupdate
    cd /usr/www      && _cvsupdate
)}

_cvsdiff() { _cvscommand diff -uNp $@; }
_cvscommit() { _cvscommand commit $@; }
_cvsrelease() { _cvscommand release $@; }
_cvscheckout() { _cvscommand -q -z1 checkout -P $1; }

_portmark() { pwd > /var/cache/pmark; }
_portlast() { cd "$(</var/cache/pmark)"; }

_portout() {
    cd "$(</var/cache/pmark)"
    cd "$(make show=WRKSRC)"
}

_porthomepage() { $BROWSER "$(make show=HOMEPAGE)" > /dev/null 2>&1 & }

_portcheck() { /usr/ports/infrastructure/bin/portcheck; }
_portlibcheck() { make port-lib-depends-check; }

_portmodgoupdate() { make MODGO_VERSION=latest modgo-gen-modules; }
_portmodcargo_gen() { make modcargo-gen-crates; }
_portmodcargo_genlic() { make modcargo-gen-crates-licenses; }

_patchvim() {
    if [ ! -f $1.orig.port ]
    then
        doas -u _pbuild cp $1 $1.orig.port
    fi
    doas -u _pbuild vim $1
}

_dpbmake() {(
    set -xe
    doas chroot /home/dpb /tmp/installdeps.sh $1
    doas /usr/ports/infrastructure/bin/dpb -B /home/dpb -c -I $1
)}

_prootrebuild() {
    doas proot -c /etc/proot.conf
    doas chroot /home/dpb pkg_add -Dsnap ccache gmake cmake meson ninja go rust
    {
        echo '#!/bin/sh -ex'
        echo 'cd /usr/ports/$1'
        echo 'FETCH_PACKAGES=-Dsnap make install-depends'
    } > /home/dpb/tmp/installdeps.sh
    chmod +x /home/dpb/tmp/installdeps.sh
}

_prootshell() { doas chroot /home/dpb/ ksh -i; }

_listdevs() {
    ssh $ME@cvs.openbsd.org 'fgrep -v ksh- /etc/passwd' \
        | egrep 111\|122 \
        | grep -v gitsync \
        | cut -d: -f1,5 | tr -s ',' \
        | sed 's/,/, /g;s/,[, ]*$//g' \
        | column -s":" -t \
        | fzf -i -e
}

_repology() {
    $BROWSER \
        "https://repology.org/projects/?search=&maintainer=sh%2Bports%40codevoid.de&category=&inrepo=openbsd" \
        >/dev/null 2>&1
}

