Commit 95ae61fc authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Split .shell/funcs into .shell/lib/*.sh files

parent 0023d66a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ if [ -z "$BASH_PROFILE_LOADED" ]; then
	return # bash_profile sources this file
fi

. ~/.shell/funcs
. ~/.shell/lib/setup.sh

USE_PROMPT=yes do_update

+1 −1
Original line number Diff line number Diff line
. ~/.shell/funcs
. ~/.shell/lib/funcs.sh
. ~/.shell/colours

# Environment variables
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ COMMANDS="Commands:
"


. ~/.shell/funcs
. ~/.shell/lib/funcs.sh

die() {
	code=${CODE-$?}
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ set -eu
: ${MIN_DELAY_MINS:=45} ${MAX_DELAY_MINS:=120}
: ${IMAGES_DIRECTORY:=${HOME}/.backgrounds}

. ~/.shell/funcs
. ~/.shell/lib/funcs.sh

if [ $# -gt 0 ]; then
	echo >&2 "Using '$*' as setter command"
+13 −151
Original line number Diff line number Diff line
#!/bin/sh

[ -n "${SOURCED_SHELL_FUNCS-}" ] && return
SOURCED_SHELL_FUNCS=yes

source_first()
{
	for sf in "$@"; do
		[ -f "$sf" ] && . "$sf" && return
	done
}

source_each()
{
	for sf in "$@"; do
		[ -f "$sf" ] && . "$sf"
	done
}

has()
{
	type "$1"
} >/dev/null 2>&1

get_platform()
{
	case "$OSTYPE" in
		linux-gnu*) echo gnu ;;
		darwin*) echo darwin ;;
		*) echo $OSTYPE ;;
	esac
}

get_branch()
{
	git rev-parse --abbrev-ref HEAD
}

manage_path()
{
	local var="$1" val="$2"; shift 2
	eval local cur=\$$var
	local cur=`echo $cur | sed -E 's!(^|:)'"$val"'(:|$)!\1\2!;s!^:|:$!!;s!::!:!'`
	local pre="$cur" post=
	while [ $# -gt 0 ]; do
		case "$1" in
			begin*|before|infront)
				post="$cur"
				pre=
				;;
			check)
				test -d "$val" || return 0 ;;
		esac
		shift
	done
	eval $var="'${pre:+$pre:}$val${post:+:$post}'"
}

get_shell()
{
	if [ -n "$ZSH_NAME" ]; then echo zsh; return; fi
	if [ -n "$BASH" ]; then echo bash; return; fi
	echo sh
}

git_check()
{
	local v
	GIT_VERSION=0
	for v in `git --version|cut -f3 -d\ |tr '.' ' '|cut -f1-3 -d\ `; do
		let 'GIT_VERSION = (GIT_VERSION << 8) + (v & 0xff)'
	done

	if [ $GIT_VERSION -ge $((0x020900)) ]; then
		local numcpu
		if has lscpu; then
			numcpu=`lscpu -p|grep '^[^#]'|wc -l`
		elif [ -n "${NUMBER_OF_PROCESSORS:-}" ]; then
			numcpu="$NUMBER_OF_PROCESSORS"
		else
			numcpu=1
		fi
		GIT_JOBS="--jobs=$(((numcpu>1)?(numcpu-1):(numcpu)))"
		return
if [ "${TERM-dumb}" = dumb ] && [ -t 1 ]; then
	_prefix="`tput setaf 1; tput bold`$0:`tput sgr0; tput setaf 1`"
	printf "$_prefix %s\n" \
		"Sourcing '~/.shell/funcs' is deprecated, please change to" \
		"either '~/.shell/lib/setup.sh' or '~/.shell/lib/funcs.sh'" |
		fmt --prefix="$_prefix" --width=`tput cols` >&2
	tput sgr0
	unset _prefix
fi

	GIT_JOBS=
}
. $HOME/.shell/lib/setup.sh

update_worktree()
{
	local upstream=`git rev-parse --abbrev-ref --symbolic-full-name @{u}`
	if [ -z "$upstream" ]; then
		return 0
	fi
	local original=`git rev-parse HEAD`
	if git merge --ff --no-edit $upstream origin/master; then
		git submodule sync --recursive
		git submodule update --init --recursive --depth=1 ${GIT_JOBS}
	else
		git reset $original
	fi
}

do_update()
{
	local timestamp=${HOME}/.shell/.update
	local now=`date +%s` updated=`date -r $timestamp +%s 2>/dev/null`
	local BOLD_BLUE=$'\033[34;1m' DEFAULT=$'\033[m'
	local prompt="${BOLD_BLUE}Would you like to update dotfiles?${DEFAULT} "
	local unit ans

	if [ -n "${USE_PROMPT:-}" ]; then
		if [ $now -lt $((updated + 604800)) ]; then
			return
		fi

		case `get_shell` in
			zsh) read "?$prompt" ans ;;
			*) read -p "$prompt" ans ;;
		esac
		case "$ans" in
			[Yy]|[Yy][Ee][Ss]) ;;
			[Nn]|[Nn][Oo]) touch "$timestamp"; return ;;
			*) return ;;
		esac
	fi

	(cd ~
		set -eu
		git fetch origin &&
		git_check &&
		update_worktree
	) || return

	touch "$timestamp"

	# re-source this file
	unset SOURCED_SHELL_FUNCS
	. ~/.shell/funcs

	# ensure systemd units are enabled
	if type systemctl >/dev/null 2>&1; then
		systemctl --user daemon-reload
		for unit in ~/.config/systemd/user/*; do
			if [ -d "$unit" ]; then continue; fi
			echo "Enabling $unit"
			systemctl --user enable "${unit##*/}" 2>/dev/null
		done
	fi
}
# vim: ft=sh
Loading