#!/bin/sh -e

if [[ ! -x "$(command -v emacs)" ]]; then
    yay -S --noconfirm emacs
fi

while [[ $# -gt 0 ]]; do
    case $1 in
        -d|--dry-run)
            dry=1
            shift
            ;;
        *)
            break
            ;;
    esac
done

log() {
    if [[ $dry == "1" ]]; then
        echo "[DRY-RUN] $@"
    else
        echo "$@"
    fi
}

execute() {
    log "executing: $@"
    if [[ $dry == "1" ]]; then
        return
    fi
    "$@"
}

tangle() {
    emacs -Q --batch --eval "
        (progn
            (require 'ob-tangle)
            (dolist (file command-line-args-left)
                (with-current-buffer (find-file-noselect file)
                    (org-babel-tangle))))
    " "$@"
}

execute tangle $@

