#!/bin/sh
# Blakely's basic shell configuration file

# This file is compatible with basic `sh`, and can
# be sourced in all `sh`-compatible shells to get
# the basic parts of the local `sh` configuration.

# This file doesn't need a shebang at the top, but it has one
# so shellcheck knows this file has to be compatible with POSIX sh

### Pre-setup >>>
# shellcheck disable=SC1090
# Helper function to source files.
# Usage:
#   careful_source "file"                                             \
#     [optional: "Error message"]                                     \
#     [optional: $LINENO]  (use this if you have an error message)    \
#     [optional: test to run]  (-f, -x, -e)                           \
#     [optional: operation  (source, exec, command or nothing)]       \
careful_source() {
   if test "${4:--f}" "$1"; then
      ${5:-.} "$1"
   else
      if test -n "${2+foo}"; then
         printf "%s\n" "$2" >&2
         echo "Line number: $3" >&2
      fi
   fi
}

# for sending error messages from functions and whatnot
error() {
   ERROR_CODE="$?"
   >&2 echo "(zshrc) An error occurred on line number ${1}"
   return $ERROR_CODE
}

# change cursor to a beam by default
printf '\e[5 q'

### <<<
### Generic shell configuration >>>
### Environment variables >>>

# This has to go here because otherwise we don't know where the config files are :|
export SHELL_CONFIG_DIR="${SHELL_CONFIG_DIR:-"${XDG_CONFIG_HOME:-$HOME/.config}/shell"}"

# source the environment variable file
${SKIP_ENV:-false} || careful_source "$SHELL_CONFIG_DIR/env" \
   "Error: could not locate env file. Things could be messed up. Check that \$SHELL_CONFIG_DIR/env exists." $LINENO

${SKIP_LOCAL_ENV:-false} || careful_source "$SHELL_CONFIG_DIR/local/env" \

# <<<
### Aliases >>>
# Source the alias file
${SKIP_ALIASES:-false} || careful_source "$SHELL_CONFIG_DIR/aliases" \
   "Error: could not locate alias file. You may not have any aliases. Check that \$SHELL_CONFIG_DIR points to the right place." $LINENO

${SKIP_LOCAL_ALIASES:-false} || careful_source "$SHELL_CONFIG_DIR/local/aliases"

# <<<
### Functions >>>
# Organization: functions should be put in their own files, located in
# $SHELL_CONFIG_DIR/functions. If they can be converted into a script, they
# should be.
#
# To make a "function in a file" that fits with my personal spec, simply make a
# file, name it what the function is called, and put the function inside of it
# just like you would if you were writing it here. Make sure to describe what
# the function does at the top, and say what dependencies it has (if any).

# Function to load all functions that are stored in files
fnupdate() {
   for FN in "$SHELL_CONFIG_DIR"/functions/*;do
      . "$FN"
   done
}
${SKIP_FUNCTIONS:-false} || fnupdate

# <<<
### Options >>>
set -o vi

# <<<

# <<<
### Load the local shrc >>>
${SKIP_LOCAL_ENV:-false} || careful_source "$SHELL_CONFIG_DIR/local/shrc" \

# <<<
### Apply breaking changes with the update script >>>
${SKIP_UPDATES:-false} || careful_source "$SHELL_CONFIG_DIR/updates" \
   "Error: failed to run update script" $LINENO -x "command"

### <<<
# terminal colorscheme
careful_source "$SHELL_CONFIG_DIR/colors.sh"

# vim:fdm=marker:fmr=>>>,<<<:et:ft=sh:sw=3:ts=3