#!/bin/sh -e
# SPDX-License-Identifier: CC0
# $Id: bootstrap.sh,v 1.2 2024/06/21 20:03:03 mcornick Exp $
# shellcheck disable=SC2016
prefix=$(dirname "$(dirname "$(which bash)")")
if [ "$prefix" = "/" ]; then
	prefix="/usr"
fi
if command -v vim >/dev/null 2>&1; then
	editor="vim"
else
	editor="vi"
fi
touch "$HOME/.bashrc.local" "$HOME/.bash_logout.local" "$HOME/.bash_profile.local"
echo '. "$HOME/.bash_logout.local"' >"$HOME/.bash_logout"
cat <<E0F >"$HOME/.bash_profile"
if [ ! "\$TMUX" ]; then
	export TERM="xterm-256color"
fi
. "\$HOME/.bashrc"
. "\$HOME/.bash_profile.local"
E0F
echo "set completion-ignore-case On" >"$HOME/.inputrc"
cat <<E0F >"$HOME/.bashrc"
PS1="\\u@\\h:\\w\\\$ "
alias ls="ls -F"
E0F
if [ "$editor" = "vim" ]; then
	echo 'alias vi="vim"' >>"$HOME/.bashrc"
fi
cat <<E0F >>"$HOME/.bashrc"
export EDITOR="$editor"
export LESS="FXMRc"
export PAGER="less"
export PATH="\$HOME/.local/bin:\$PATH"
export VISUAL="$editor"
if [[ -f "$prefix/share/bash-completion/bash_completion" ]]; then
	. "$prefix/share/bash-completion/bash_completion"
fi
case "\$TERM" in
xterm*)
	PROMPT_COMMAND='echo -ne "\033]0;\${USER}@\${HOSTNAME}:\${PWD}\007"'
	;;
*)
	;;
esac
. "\$HOME/.bashrc.local"
E0F
cat <<E0F >"$HOME/.vimrc"
filetype plugin indent on
nnoremap <silent> <C-L> :nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>
set autoindent
set autoread
set background=dark
set backspace=indent,eol,start
set complete-=i
set display+=lastline
set encoding=utf-8
set expandtab
set formatoptions+=j
set hidden
set history=1000
set ignorecase
set incsearch
set laststatus=2
set modeline
set nrformats-=octal
set pastetoggle=<leader>p
set ruler
set scrolloff=1
set sessionoptions-=options
set sidescrolloff=5
set smartcase
set smarttab
set statusline=%f\ %m\ %r\ %=\ %y\ %l,%c
set tabpagemax=50
set ttimeout
set ttimeoutlen=100
set viewoptions-=options
set wildmenu
autocmd BufRead,BufNewFile *.md set filetype=markdown textwidth=72
autocmd BufRead,BufNewFile *.mdoc set filetype=nroff
autocmd FileType python setlocal completeopt-=preview textwidth=79 tabstop=4
nnoremap <SPACE> <Nop>
source ~/.vimrc.local
E0F
if [ ! -f "$HOME/.vimrc.local" ]; then
	cat <<E0F >"$HOME/.vimrc.local"
syntax enable
let mapleader=" "
E0F
fi
cat <<E0F >"$HOME/.tmux.conf"
unbind C-b
set-option -g prefix C-a
bind C-a send-prefix
set-option -g status-right "#(whoami)@#(hostname -s) %R %F"
set -g status-right-length 50
set -g status-left-length 20
set -s escape-time 0
set -g history-limit 50000
set -g display-time 4000
set -g status-interval 5
set -g default-terminal "tmux-256color"
set -g status-keys emacs
set -g focus-events on
setw -g aggressive-resize on
source-file ~/.tmux.conf.local
E0F
touch "$HOME/.tmux.conf.local"
git config --global branch.autosetupmerge 'true' || true
git config --global color.ui 'auto' || true
git config --global core.autocrlf 'input' || true
# shellcheck disable=SC2088
git config --global core.excludesfile '~/.gitignore_global' || true
git config --global diff.mnemonicprefix 'true' || true
git config --global init.defaultBranch 'main' || true
git config --global pull.rebase 'true' || true
git config --global push.default 'current' || true
git config --global push.followTags 'true' || true
git config --global rerere.enabled 'true' || true
git config --global user.name 'Mark Cornick' || true
touch "$HOME/.gitignore_global"
if [ ! -f "$HOME/bootstrap.sh.local" ]; then
	printf "git config --global user.email mcornick@mcornick.com\n" >"$HOME/bootstrap.sh.local"
fi
# shellcheck disable=SC1091
. "$HOME/bootstrap.sh.local"
