# -*- mode: Sh; fill-column: 78; -*-

export_function()
{
  local name=$1
  local alias_dir=$PWD/.direnv/aliases
  mkdir -p "$alias_dir"
  PATH_add "$alias_dir"
  local target="$alias_dir/$name"
  if declare -f "$name" >/dev/null; then
    echo "#!$SHELL" > "$target"
    declare -f "$name" >> "$target" 2>/dev/null
    # Notice that we add shell variables to the function trigger.
    echo "$name \$*" >> "$target"
    chmod +x "$target"
  fi
}

use_haunt()
{
    gcroot="${HOME}/.config/guix/gcroots/haunt"
    if [ -L "$gcroot" ]
    then
        rm -v "$gcroot"
    fi
    PACKAGES_MAINTENANCE=(direnv git sshpass password-store less openssh gnupg)
    PACKAGES=(bash coreutils grep findutils which)
    eval "$(guix environment --search-paths --root="$gcroot" --pure --load=guix.scm --ad-hoc ${PACKAGES[@]} ${PACKAGES_MAINTENANCE[@]} "$@")"
}

use_guix()
{
    GUIX_GITHUB_TOKEN="$(pass show github/tokens/GUIX_GITHUB_TOKEN)"
    export GUIX_GITHUB_TOKEN

    GUIX_PACKAGE_PATH=""
    export GUIX_PACKAGE_PATH

    packages_maintenance=(
        direnv
        git
        git:send-email
        git-cal
        gnupg
        guile-colorized
        guile-readline
        inetutils
        less
        ncurses
        openssh
        password-store
        sshpass
        xdot
        which
    )
    export ENV_PACKAGES_MAINTENANCE="${packages_maintenance[@]}"

    packages=(help2man guile-sqlite3 guile-gcrypt)
    export ENV_PACKAGES="${packages[@]}"

    GIT_SSL_CAINFO=/run/current-system/profile/etc/ssl/certs/ca-certificates.crt
    export GIT_SSL_CAINFO
    SSL_CERT_DIR=/etc/ssl/certs
    export SSL_CERT_DIR
    NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
    export NIX_SSL_CERT_FILE
    SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
    export SSL_CERT_FILE

    # https://lists.gnu.org/archive/html/guix-devel/2016-09/msg00859.html
    environment()
    {
        set -x
        case "$1" in
            build)
                guix environment --pure guix --ad-hoc ${ENV_PACKAGES[@]} ${ENV_PACKAGES_MAINTENANCE[@]} -- .direnv/aliases/build
                ;;
            *)
                guix environment --pure guix --ad-hoc ${ENV_PACKAGES[@]} ${ENV_PACKAGES_MAINTENANCE[@]} "$@"
                ;;
        esac

    }
    export_function environment

    repl()
    {
        set -x
        ./pre-inst-env guix repl -L "$(./pre-inst-env guix build emacs-geiser)"/share/geiser/guile
    }
    export_function repl

    configure()
    {
        ./configure --localstatedir=/var --prefix=
    }
    export_function configure

    build()
    {
        make -k -j "$(nproc)"
        if [ $# -gt 0 ]
        then
            case "$1" in
                refresh)
                    ./pre-inst-env guix refresh -l "${@:2}"                             \
                        | cut -d: -f 2                                                  \
                        | tr ' ' '\n'                                                   \
                        | cut -d@ -f 1                                                  \
                        | xargs ./pre-inst-env guix build --no-grafts --no-offload
                    ;;
                *)
                    ./pre-inst-env guix build --no-grafts --no-offload "$@"
                    ;;
            esac
        fi
        for file in po/*/*.po
        do
            git update-index --assume-unchanged "$file"
        done
    }
    export_function build

    push()
    {
        sshpass -Ppassphrase -p"$(pass show localhost/ssh/id_rsa_savannah)" git push --set-upstream origin
    }
    export_function push

    pull()
    {
        sshpass -Ppassphrase -p"$(pass show localhost/ssh/id_rsa_savannah)" git pull --rebase
    }
    export_function pull

    guix_pull()
    {
        mkdir -p current
        guix pull --profile="${PWD}/current/guix" --url="file://${PWD}" --disable-authentication "$@"
    }
    export_function guix_pull

    shell()
    {
        guix shell ${ENV_PACKAGES[@]} ${ENV_PACKAGES_MAINTENANCE[@]} --development guix "$@"
    }
    export_function shell

    clear
    echo "
direnv:
  build        build a package or just a project if no argument provided
  configure    run ./configure with predefined parameters
  environment  WARNING: deprecated: run 'guix environment' with presetted arguments
  push         push to upstream Git repository
  pull         pull changes from upstream Git repository
  repl         run guix repl
  shell        run 'guix shell' with presetted arguments

  guix_pull    guix pull current directory
"

    export XDG_RUNTIME_DIR="/run/user/$UID"
    export EDITOR="$HOME/.guix-profile/bin/emacsclient -nc"

    cat <<'EOF'
sudo --login \
GUILE_LOAD_PATH="${HOME}/.local/share/chezmoi/dotfiles/guixsd/modules:${GUILE_LOAD_PATH}" \
"${PWD}/pre-inst-env" guix system build \
--load-path="${HOME}/src/gitlab.com/nonguix/nonguix" \
--load-path="${HOME}/src/cgit.duckdns.org/guix/guix-wigust/guix" \
--load-path="${HOME}/.local/share/chezmoi/dotfiles/guixsd/modules" \
"${HOME}/.local/share/chezmoi/dotfiles/guixsd/guixsd.scm"
EOF
}

use_emacs-guix()
{
    gcroot="${HOME}/.config/guix/gcroots/guix"
    if [ -L "$gcroot" ]
    then
        rm -v "$gcroot"
    fi

    PACKAGES_MAINTENANCE=(
        direnv
        emacs
        git
        git-cal
        gnupg
        guile
        guile-colorized
        guile-irregex
        guile-readline
        inetutils
        less
        ncurses
        openssh
        password-store
        sshpass
        st
        which
        xdot
    )
    PACKAGES=(help2man)

    # https://lists.gnu.org/archive/html/guix-devel/2016-09/msg00859.html
    eval "$(guix environment --search-paths --root="$gcroot" --pure --load=guix.scm --ad-hoc ${PACKAGES[@]} ${PACKAGES_MAINTENANCE[@]} "$@")"

    GUILE_LOAD_PATH=$PWD/scheme:$GUILE_LOAD_PATH
    export GUILE_LOAD_PATH
    GUILE_LOAD_COMPILED_PATH=$PWD/scheme:$GUILE_LOAD_COMPILED_PATH
    export GUILE_LOAD_COMPILED_PATH
    EMACSLOADPATH="$PWD/elisp:$EMACSLOADPATH"
    export EMACSLOADPATH
}

use_dotfiles()
{
    check()
    {
        make check TESTS="tests/connect.bats tests/executables.bats tests/mail.bats tests/mjru.bats" "$@"
    }
    export_function check

    GUILE_LOAD_PATH="${PWD}/dotfiles/guixsd/modules:${GUILE_LOAD_PATH}"
    export GUILE_LOAD_PATH

    GUIX_PACKAGE_PATH="${PWD}/dotfiles/guixsd/modules:${GUIX_PACKAGE_PATH}"
    export GUIX_PACKAGE_PATH
}

use_guile()
{
    unset GUIX_PACKAGE_PATH
    PACKAGES=(
        autoconf
        automake
        bash
        coreutils
        flex
        gettext
        libtool
        readline
        texinfo
    )
    PACKAGES_MAINTENANCE=(
        direnv
    )
    gcroot="${HOME}/.config/guix/gcroots/guile"
    if [ -L "$gcroot" ]
    then
        rm -v "$gcroot"
    fi
    eval "$(guix environment --search-paths --root="$gcroot" --pure guile --ad-hoc ${PACKAGES[@]} ${PACKAGES_MAINTENANCE[@]} "$@")"
}

use_fasm()
{
    PACKAGES_MAINTENANCE=(bash binutils coreutils direnv)
    PACKAGES=(fasm)
    eval "$(guix environment --search-paths --pure --ad-hoc ${PACKAGES[@]} ${PACKAGES_MAINTENANCE[@]} "$@")"
}

use_artanis()
{
    PACKAGES_MAINTENANCE=(bash binutils coreutils direnv)
    PACKAGES=(artanis guile@2)
    eval "$(guix environment --search-paths --pure --ad-hoc ${PACKAGES[@]} ${PACKAGES_MAINTENANCE[@]} "$@")"
}

use_mjru_ci_jenkins()
{
    jenkins-jobs()
    {
        export PYTHONPATH=''
        "$HOME/.nix-profile/bin/jenkins-jobs" --conf "$HOME/.config/jenkins_jobs/jenkins_jobs_majordomo.ini" "$@"
    }
    export_function jenkins-jobs
}

use_mjru_nix()
{
    export NIX_ARGS
    nix-build()
    {
        "$HOME/.nix-profile/bin/nix-build" "${NIX_ARGS[@]}" "$@"
    }
    export_function nix-build
    nix-instantiate()
    {
        "$HOME/.nix-profile/bin/nix-instantiate" "${NIX_ARGS[@]}" "$@"
    }
    export_function nix-instantiate
    nix-shell()
    {
        NIX_ARGS=("--no-out-link"
                  "--option" "trusted-public-keys" "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cache.nixos.intr:6VD7bofl5zZFTEwsIDsUypprsgl7r9I+7OGY4WsubFA="
                  "--option" "builders" "ssh://nixos.intr x86_64-linux"
                  "--substituters" "https://cache.nixos.org/ https://cache.nixos.intr/")
        "$HOME/.nix-profile/bin/nix-shell" "${NIX_ARGS[@]}" "$@"
    }
    export_function nix-shell
}

use_nixpkgs()
{
    echo '+/home/oleg/src/nixpkgs' > .dumbjump
}
