aboutsummaryrefslogtreecommitdiff
path: root/mkshrc
blob: 033ee502f7bc88f5558ea3a65e2cf60f0efe3852 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# Copyright 2018 Mitchell Riedstra

# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# If you wish to prevent non KSH shells from running this
# [ -z "$KSH_VERSION" ] && return

# This will hopefully fix the bullshit with zsh
if \
	[ $SHELL = "/bin/zsh" ] || \
	[ $SHELL = "/usr/bin/zsh" ] || \
	[ $SHELL = "/usr/local/bin/zsh" ] ; then
# Bash doesn't like an empty if statement apparently
printf ""
else


precmd() {
	typeset e=$?

	(( e )) && print -n "$e|"
}
set_ps1() {
		PS1='$(precmd)${BG}${ACCENT_C}${ACCENT_P}${USER_C}${USER:=$(id -un)}@$(hostname)${ACCENT_C}${ACCENT_P} ${ACCENT_P}${PATH_C}${PWD:-?}${ACCENT_C}${ACCENT_P}${END_C}
${END_P} '
		export PS1;
}
set_root_ps1() {
PS1='$(precmd)${BG}${ACCENT_C}${ACCENT_P}${HOST_C}$(hostname)${ACCENT_C}${ACCENT_P} ${ACCENT_P}${PATH_C}${PWD:-?}${ACCENT_C}${ACCENT_P}${END_C}
${END_P} '
export PS1;
}
# RED: 31, GREEN: 32, YELLOW: 33, BLUE: 34, MAGENTA: 35, CYAN: 36, WHITE: 37, GREY 38. and No color which is 0.
set_prompt() {
	if [ `id -u` -eq 0 ] ; then
		# Previously
		# Red for root, and no username 
		# PS1="$(hostname) \$PWD
		echo $TERM | grep "256color$" > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
			BG=""
			ACCENT_C=""
			ACCENT_P="|"
			HOST_C=""
			PATH_C=""
			END_C=""
			set_root_ps1
			END_P="#"
		else
			BG=""
			ACCENT_C=""
			ACCENT_P="|"
			HOST_C=""
			PATH_C=""
			END_P="#"
		fi
		set_root_ps1
	else	
		# Previously
		# PS1="${USER:=$(id -un)}@$(hostname) \$PWD
		echo $TERM | grep "256color$" > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
			BG=""
			ACCENT_C=""
			ACCENT_P="|"
			USER_C=""
			PATH_C=""
			END_C=""
			END_P="%"
		else
			BG=""
			ACCENT_C=""
			ACCENT_P="|"
			USER_C=""
			PATH_C=""
			END_C=""
			END_P="$"
		fi
		set_ps1
	fi
}
set_basic_prompt() {
	PS1='$(precmd)'"${USER:=$(id -un)}@$(hostname) \$PWD $ "
	export PS1;
}
set_retro_prompt() {
	export PS1="\$ "
	if [ `id -u` -eq 0 ] ; then
		export PS1="# "
	fi
}
set_super_basic_prompt() {
	alias cd=do_cd
}
do_cd() {
        builtin cd $@
        pwd=${PWD##HOME}
        [ ${#pwd} -lt ${#PWD} ] && pwd="~$pwd"
        [ "$hostname" ] || hostname=$(hostname)
        PS1="$USER@$hostname:$pwd \$ "
        [ $(id -u) -eq 0 ] && PS1="$USER@$hostname:$pwd # "
}

# ZSH doesn't handle these functions all too well
fi

setaliases() {
	alias ls="ls -F";
	alias l="ls -lhF";
	alias ll="ls -lhF";
	alias g="grep";
	alias m="$PAGER";
	alias j="jobs";
	alias p="pwd";
	alias gdb='gdb -q'
	alias bc='bc -q'
	alias eg='egrep'
	alias search='egrep -rniI'
}

setcoloraliases() {
	if [ `uname` = "Linux" ] ; then
		alias ls="ls --color=auto -F";
		alias lm='ls -lh --color=force | more';
	elif [ `uname` = "FreeBSD" ] ; then
		alias ls="ls -FG";
		alias lm='CLICOLOR_FORCE="1" ls -l | less -r';
	fi

	grep --help | grep fucks - >/dev/null 2>&1
	if [ $? -eq 0 ] ; then
		alias grep="grep --color=auto";
		alias egrep='egrep --color=auto'
		alias search='egrep --color=auto -rnI';
	fi

	alias diff="$(which diff colordiff 2>/dev/null | tail -n 1)";
	alias t='tree -CdL'
}

setsudoaliases() {
	if which sudo > /dev/null 2>&1 && [ -x "$(which sudo)" ] ; then
	# Package managers
	# Isn't it amazing how many distros you end up using?
	alias apt-get='sudo apt-get'
	alias yum='sudo dnf'
	alias dnf='sudo dnf'
	alias pacman='sudo pacman'
	alias xbps-install='sudo xbps-install'
	alias xbps-remove='sudo xbps-remove'
	alias pkg='sudo pkg'

	# Filesystem utilities
	alias zfs='sudo zfs'
	alias btrfs='sudo btrfs'
	alias mount='sudo mount'
	alias umount='sudo umount'
	fi
}

# Usage debugstarttls $ipaddr:$port
debugstarttls() {
	openssl s_client -starttls smtp -crlf -connect $1 $2
}


getpkgsrc() {
	cd $HOME
	pull -f pkg.txz -u "https://www.rygel.us/misc/pkg-$(uname).txz"
	tar -xJvf pkg.txz && rm pkg.txz
	if ! [ -e $HOME/.mkshrc.pre ] ; then
		printf "export PATH=\$HOME/pkg/bin:\$PATH\n" > $HOME/.mkshrc.pre
	fi
	cd -
}
installvis() {
	cd $HOME
	pull -u "https://www.rygel.us/misc/vis-$(uname)" -f vis-editor
	if [ $(id -u) -eq 0 ] ; then
		install -o root -m 555 vis-editor /usr/local/bin/vis-editor && \
		rm vis-editor
	else
		if ! [ -d $HOME/bin ] ; then
			mkdir $HOME/bin
		fi
		install -m 555 vis-editor $HOME/bin/vis-editor && \
		rm vis-editor
	fi

}
set_retro() {
	set_retro_prompt
	unalias vi
	unalias vim
	unalias nvim
	# nvim has some serious issues with konsole and cool-retro-term
	export EDITOR="$(which vi nvi vim 2>/dev/null | tail -n1)"
	export VISUAL="$EDITOR"
	alias vi="$EDITOR"
	alias nvim="$EDITOR"
	export TERM="$(echo $TERM | sed -e's/-256color$//')"
}
# Run Steam 
unfuck_steam() {
LD_PRELOAD='/usr/$LIB/libstdc++.so.6 /usr/$LIB/libgcc_s.so.1 /usr/$LIB/libxcb.so.1 /usr/$LIB/libgpg-error.so' /usr/bin/steam
}
counter_figlet() {
COUNTER="1";
while true ; do
	figlet "$COUNTER";
	COUNTER="$(echo $COUNTER+1 |bc)";
	sleep 1;
	clear;
done 
}
counter_single_line() {
COUNTER="1";
while true ; do
	sleep 1;
	printf "\t$COUNTER\r";
	COUNTER="$(echo $COUNTER+1 |bc )";
done
}
# First argument is the interface on which to scan
ipv6_local_hosts() {
if ! [ -z $1 ] ; then
	interface="$1"
	ping6 -c 3 ff02::2%$interface \
		| grep 'bytes from' \
		| awk '{print $4}' \
		| sort \
		| uniq \
		| sed -e's/:$//'
else
	echo "You need to specify an interface"
fi
}
pulltermcolors() {
	file="$HOME/bin/terminal-colors"
	if ! [ -d $HOME/bin ] ; then
		mkdir $HOME/bin
	fi
	pull -u ${UPDATE_URL}/terminal-colors -f $file
	chmod +x $file
}
set_nocaps() {
	setxkbmap -option ctrl:nocaps
	setxkbmap -option shift:breaks_caps
}
pkg_switch_branch() {
	_f="/etc/pkg/FreeBSD.conf"
	cp $_f ${_f}.orig
	sed -e's/quarterly/latest/' < ${_f}.orig > ${_f}
}
find_most_recent_python() {
_python="$(which python2 2>/dev/null)"
$_python -c 'import os

max_mtime = 0
for dirname,subdirs,files in os.walk("."):
    for fname in files:
        full_path = os.path.join(dirname, fname)
        mtime = os.stat(full_path).st_mtime
        if mtime > max_mtime:
            max_mtime = mtime
            max_dir = dirname
            max_file = fname

print max_dir, max_file'
}
# System setup `chroot`, useful mainly on Linux
syschroot() {
	CHDIR=$1
	PROG=$2
	CUR_PS1="$PS1"
	PS1="(CHROOT) # "
	mount -o bind /dev $CHDIR/dev
	mount -o bind /sys $CHDIR/sys
	mount -o bind /proc $CHDIR/proc
	if [ "$PROG" = "/bin/bash" ] ; then
		PS1="(CHROOT) [ \u@\h \w ] # "
	fi
	chroot $CHDIR $PROG
	umount $CHDIR/dev $CHDIR/sys $CHDIR/proc
	PS1="$CUR_PS1"
}

# Usage: dir_oct:file_oct path
setperms() {
	 dir_perms=$(echo $1 | awk -F: '{print $1}')
	file_perms=$(echo $1 | awk -F: '{print $2}')
	find $2 -type f -exec chmod $file_perms {} \; ;
	find $2 -type d -exec chmod $dir_perms  {} \; ;
}

timestamp() {
	date +%m.%d.%y_%H.%M.%S
}


pull() {
	url=""
	file=""
	while [ $# -gt 0 ] ; do
		case $1 in
			-f)
				file="$2"
				shift; shift; ;;
			-u)
				url="$2"
				shift; shift; ;;
		esac
	done
	CMD="$(which curl wget fetch 2>/dev/null | tail -n1)"

	case $CMD in
		*curl)	"$CMD" "$url"  > "$file" ;;
		*wget)	"$CMD" "$url" -O "$file" ;;
		*fetch)	"$CMD" "$url" -o "$file" ;;
	esac
}
pulldotfile() {
	file="$1"
	pull -u "${UPDATE_URL}${file}" -f "$HOME/.${file}"
}
pullkeys() {
	file="keys/$1"
	if ! [ -d $HOME/.ssh ] ; then mkdir $HOME/.ssh ; fi
	chmod 700 $HOME/.ssh
	chmod 600 $HOME/.ssh/authorized_keys
	mv "$HOME/.ssh/authorized_keys" "$HOME/.ssh/authorized_keys.$(timestamp)" || \
		echo "Authorized Keys do not exist"
	pull -u "${UPDATE_URL}${file}" -f "$HOME/.ssh/authorized_keys"
	chmod 600 $HOME/.ssh/authorized_keys
}
pullsshconfig() {
	file="ssh/config"
	pull -u "${UPDATE_URL}${file}" -f "$HOME/.ssh/config"
}
updatetmuxconf() {
	pulldotfile "tmux.conf"
	pulldotfile "tmux_helper.sh"
	if ! [ -e $HOME/.tmux.conf.local ] && [ `id -u` -eq 0 ] ; then
		# If I ever change the color from green this will have to be updated I guess
		grep 'green' $HOME/.tmux.conf | sed -e's/green/red/g' > $HOME/.tmux.conf.local
	elif ! [ -e $HOME/.tmux.conf.local ] ; then
		touch $HOME/.tmux.conf.local
	fi
}
updategitconf() {
	file="gitconfig/$1"
	pull -u "${UPDATE_URL}${file}" -f "$HOME/.gitconfig"
}
updateshell() {
	pulldotfile "mkshrc"
	. $HOME/.mkshrc
}
updatevimrc() {
	OLDWD="$(pwd)"
	cd $HOME
	git clone $VIM_GIT_URL .vim
	if [ $? -gt 0 ] ; then
		cd $HOME/.vim
		git pull origin master
	else
		cd $HOME/.vim
	fi
	./setup.sh && \
	./link-vimrc.sh
	cd $OLDWD
}


setcolorterminal() {
	case $TERM in
	xterm*)
		export TERM=xterm-256color
		# printf $'\033]0;'"$(hostname)"$'\007';
	;;
	screen*)
		export TERM=screen-256color
		# printf $'\033k'"$(whoami)"$'\033\\';
	;;
	esac
}

setlscolors() {
	if [ "$(uname)" = "Linux" ] ; then
		export LS_COLORS='di=36;40:ln=1;31;40:so=32;40:pi=33;40:ex=1;32;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:'
	elif [ "$(uname)" = "FreeBSD" ] ; then
		export LSCOLORS='gxBxcxdxCxegedabagacad'
	fi
}

colormanpages() {
	export LESS_TERMCAP_mb=""
	export LESS_TERMCAP_md=""
	export LESS_TERMCAP_me=""
	export LESS_TERMCAP_se=""
	export LESS_TERMCAP_so=""
	export LESS_TERMCAP_ue=""
	export LESS_TERMCAP_us=""

	## Gentoo is nuts
	export GROFF_NO_SGR=1
}

set_editor() {
	if [ -z "$EDITORS" ] ; then
		export EDITOR="vi"
	else
		export EDITOR="$(which $EDITORS 2>/dev/null | tail -n1)"
	fi

	export VISUAL="$EDITOR"

	# Set an alias for our editor of choice
	alias vi="$EDITOR"
	alias vim="$EDITOR"
	alias edit="$EDITOR"
	alias e="$EDITOR"
}
set_pager() {
	export PAGER="$(which cat more less 2>/dev/null | tail -n1)"
}
set_lang() {
export CHARSET="$1";
export LANG="$1";
}

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

NEWLINE='$(printf "\n")'
SHELL_CONFIG=".mkshrc"
COLOR=0
MY_LANG="en_US.UTF-8"

export EDITORS="ed vi vim nvim "
export PATH="$HOME/bin:$PATH"
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

UPDATE_URL="https://www.rygel.us/etc/"
VIM_GIT_URL="https://git.riedstra.us/mitch/vim-cfg"

PRELOAD="$HOME/${SHELL_CONFIG}.pre"
LOCAL="$HOME/${SHELL_CONFIG}.local"

# Don't put any variables below this, as they can't be overridden before the
# functions if you do that

# Load pre config
if [ -e $PRELOAD ] ; then
	. $PRELOAD
fi

set_pager
set_editor
set_lang $MY_LANG

setaliases
setsudoaliases
	

# Nasty conditional for support of many different shells
if
	[ $SHELL = "/bin/ksh" ] ||
	[ $SHELL = "/bin/mksh" ] ||
	[ $SHELL = "/usr/bin/mksh" ] ||
	[ $SHELL = "/usr/local/bin/mksh" ] ||
	! [ -z "$KSH_VERSION" ]
then
	export HISTFILE="$HOME/.ksh_history"
	set -o emacs
	bind ^L=clear-screen
	if [ $COLOR -eq 1 ] ; then
		set_prompt
	else
		set_basic_prompt
	fi
elif	[ $SHELL = "/bin/zsh" ] ||
		[ $SHELL = "/usr/bin/zsh" ] ||
		[ $SHELL = "/usr/local/bin/zsh" ] ; then
	set -o emacs
elif	[ $SHELL = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then
	set -o emacs
	export PS1="$(whoami)"'@\h:\w \$ '
else
	[ $SHELL = "/bin/sh" ] && set_super_basic_prompt
fi

if [ $COLOR -eq 1 ] ; then
	colormanpages
	setcolorterminal
	setcoloraliases
	setlscolors
fi

# Include local configuration options
if [ -f $LOCAL ] ; then
	. $LOCAL
fi