#!/bin/sh
command -v curl >/dev/null 2>&1 || { echo >&2 "curl not found"; exit 127; }
command -v git >/dev/null 2>&1 || { echo >&2 "git not found"; exit 127; }

if ! test -e ~/.ssh/known_hosts; then
    mkdir -p ~/.ssh -m 0700
    touch ~/.ssh/known_hosts
fi

echo 'crondor.cs.wisc.edu ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKlITlNQwTfW4h2Qq3xsLiZQN95UqoQtP0pdpC+ZBQPd' >> ~/.ssh/known_hosts


mysite=https://pages.cs.wisc.edu/~matyas
cd
test -e .gitconfig || scp matyas@crondor.cs.wisc.edu:.gitconfig .gitconfig
mkdir -p bin

download_bin () {
    local prog
    prog=$1

    test -x "bin/$prog" || { curl -Lo "bin/$prog" "$mysite/$prog" && chmod +x "bin/$prog"; }
}

download_bin mr
download_bin vcsh
download_bin ag

test -r .mrconfig || curl -Lo .mrconfig $mysite/mrconfig-minimal
test -r .tmux.conf || curl -Lo .tmux.conf $mysite/tmux.conf
test -r .screenrc || curl -Lo .screenrc $mysite/screenrc

bin/mr checkout && \
    cd .shell && \
    ./install.sh "$HOME" && \
    cd ..

