aboutsummaryrefslogblamecommitdiff
path: root/bin/chc.sh
blob: ee030f5fe5c4b7b3212f8c1d20ccb0c2133610f3 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                                          






                                                                           



























                                                                     
# Cache the output of a command and emit it straight from the cache if not
# expired on each run
self=chc

# Check arguments for sanity
if [ "$#" -lt 3 ] ; then
    printf >&2 '%s: Need a cache path, a duration, and a command\n' "$self"
    exit 2
fi

# First argument is the cache path, second is the duration in seconds
cac=$1 dur=$2
shift 2

# Get the current timestamp with uts(1df)
uts=$(uts) || exit

# Function checks cache exists, is readable, and not expired
fresh() {
    [ -f "$cac" ] || return
    [ -r "$cac" ] || return
    exp=$(sed 1q -- "$cac") || return
    [ "$((exp > uts))" -eq 1 ]
}

# Write runs the command and writes it to the cache
write() {
    exp=$((uts + dur))
    printf '%u\n' "$exp"
    "$@"
}

# If the cache isn't fresh, try to write a new one, or bail out
fresh "$cac" || write "$@" > "$cac" || exit

# Emit the content (exclude the first line, which is the timestamp)
sed 1d -- "$cac"
"k">if ! hash git 2>/dev/null; then printf 'install: Could not find git(1)!\n' >&2 exit 1 fi # Define dotfiles directory and check it exists dotfiles=$HOME/.dotfiles if [[ ! -d $dotfiles ]]; then printf 'install: Could not find %s!\n' "$dotfiles" >&2 exit 1 fi # Check out submodules (cd -- "$dotfiles" && git submodule update --init) # Link in essential logical binaries mkdir -p -- "$HOME"/.local/bin lns "$dotfiles"/ack/ack "$HOME"/.local/bin/ack # Link in essential configuration files mkdir -p -- "$HOME"/.config lns "$dotfiles"/ack/ackrc "$HOME"/.ackrc lns "$dotfiles"/bash/bash_completion "$HOME"/.config/bash_completion lns "$dotfiles"/bash/bash_logout "$HOME"/.bash_logout lns "$dotfiles"/bash/bash_profile "$HOME"/.bash_profile lns "$dotfiles"/bash/bashrc "$HOME"/.bashrc lns "$dotfiles"/bash/bashrc.d "$HOME"/.bashrc.d lns "$dotfiles"/curl/curlrc "$HOME"/.curlrc lns "$dotfiles"/git/gitconfig "$HOME"/.gitconfig lns "$dotfiles"/readline/inputrc "$HOME"/.inputrc lns "$dotfiles"/sh/profile "$HOME"/.profile lns "$dotfiles"/sh/profile.d "$HOME"/.profile.d lns "$dotfiles"/terminfo "$HOME"/.terminfo lns "$dotfiles"/vim/vimrc "$HOME"/.vimrc lns "$dotfiles"/vim "$HOME"/.vim # Link in shell stuff while getopts :gmntx opt; do case $opt in g) mkdir -p -- "$HOME"/.gnupg lns "$dotfiles"/gnupg/gpg.conf "$HOME"/.gnupg/gpg.conf lns "$dotfiles"/gnupg/gpg-agent.conf "$HOME"/.gnupg/gpg-agent.conf ;; m) lns "$dotfiles"/mutt/muttrc "$HOME"/.muttrc lns "$dotfiles"/mutt "$HOME"/.mutt ;; n) mkdir -p -- "$HOME"/.config "$HOME"/.local/share/newsbeuter lns "$dotfiles"/newsbeuter "$HOME"/.config/newsbeuter ;; t) lns "$dotfiles"/tmux/tmux.conf "$HOME"/.tmux.conf ;; x) lns "$dotfiles"/i3 "$HOME"/.i3 lns "$dotfiles"/X/Xmodmap "$HOME"/.Xmodmap lns "$dotfiles"/X/Xresources "$HOME"/.Xresources lns "$dotfiles"/X/xsession "$HOME"/.xsession lns "$dotfiles"/X/xsessionrc "$HOME"/.xsessionrc lns "$dotfiles"/vim/gvimrc "$HOME"/.gvimrc ;; esac done shift $((OPTIND-1))