dotfiles

Obligatory dotfiles repo
git clone git://git.danielmoch.com/dotfiles.git
Log | Files | Refs

my (9497B)


      1 #!/bin/sh
      2 #
      3 # ~/bin/my
      4 #
      5 
      6 # Command and handlers for them are added as-needed. Some commands only
      7 # have one handler, but exist to avoid re-writes across my dotfiles
      8 # later on.
      9 
     10 [ $# -eq 0 ] && exit 1
     11 
     12 log="$HOME/var/log/my"
     13 command=$1
     14 shift
     15 
     16 __mylog()
     17 {
     18 	echo "$1 (`/bin/date +'%m/%d/%Y %H:%M:%S'`): $2" >> $log
     19 }
     20 
     21 __getlocker()
     22 {
     23 	for locker in slock xlock
     24 	do
     25 		if type $locker >/dev/null 2>&1
     26 		then
     27 			echo $locker
     28 			exit 0
     29 		fi
     30 	done
     31 	for locker in light-locker cinnamon-screensaver gnome-screensaver xscreensaver
     32 	do
     33 		if my processes | grep $locker > /dev/null 2>&1
     34 		then
     35 			echo $locker
     36 			exit 0
     37 		fi
     38 	done
     39 
     40 	__mylog "__getlocker" "No screen locker running"
     41 	exit 1
     42 }
     43 
     44 case $command in
     45 	mailto)
     46 		for m in mutt mail
     47 		do
     48 			if type $m > /dev/null 2>&1
     49 			then
     50 				mailer=$m
     51 				break
     52 			fi
     53 		done
     54 		if [ -n "$2" ]
     55 		then
     56 			$mailer -s "$2" $1
     57 		else
     58 			$mailer $1
     59 		fi
     60 		;;
     61 	search)
     62 		query=`echo $1 | tr ' ' '+'`
     63 		exec my open -T https://duckduckgo.com/?q=$query
     64 		;;
     65 	term)
     66 		if type st > /dev/null 2>&1
     67 		then
     68 			exec st "$@"
     69 		elif type urxvtd > /dev/null 2>&1
     70 		then
     71 			urxvtc "$@"
     72 			if [ "$?" -eq 2 ]
     73 			then
     74 				urxvtd -q -o -f
     75 				urxvtc "$@"
     76 			fi
     77 			exit 0
     78 		elif type urxvt > /dev/null 2>&1
     79 		then
     80 			exec urxvt "$@"
     81 		elif type xterm > /dev/null 2>&1
     82 		then
     83 			exec xterm "$@"
     84 		elif type x-terminal-emulator > /dev/null 2>&1
     85 		then
     86 			exec x-terminal-emulator "$@"
     87 		fi
     88 
     89 		[ "$1" = "-e" ] && shift && set -- "-x" $@
     90 		if type xfce4-terminal > /dev/null 2>&1
     91 		then
     92 			exec xfce4-terminal "$@"
     93 		elif type gnome-terminal > /dev/null 2>&1
     94 		then
     95 			exec gnome-terminal "$@"
     96 		fi
     97 
     98 		__mylog "my term" "No terminal emulator found"
     99 		exit 1
    100 		;;
    101 	lock)
    102 		sleep 1
    103 
    104 		locker=`__getlocker`
    105 
    106 		if [ -z "$locker" ]
    107 		then
    108 			__mylog "my lock" "no locker running"
    109 			exit 1
    110 		fi
    111 		case $locker in
    112 			light-locker|xlock|slock) : ;;
    113 			*) $locker-command -l ;;
    114 		esac
    115 		exec xset dpms force off
    116 		;;
    117 	wallpaper)
    118 		if my processes | grep rio >/dev/null 2>&1
    119 		then
    120 			return
    121 		fi
    122 		xsetroot -solid "#444444"
    123 		[ -x "$HOME/.fehbg" ] && "$HOME/.fehbg" >> $log 2>&1
    124 		;;
    125 	standby)
    126 		if type systemctl > /dev/null 2>&1
    127 		then
    128 			locker=`__getlocker`
    129 			if [ -z "$locker" ]
    130 			then
    131 				__mylog "my standby" "no locker running"
    132 				exit 1
    133 			fi
    134 			if [ ! "$locker" = "light-locker" ]
    135 			then
    136 				sleep 1
    137 				$locker-command -l
    138 				xset dpms force off
    139 			fi
    140 			exec systemctl hybrid-sleep
    141 		else
    142 			__mylog "my standby" "No handler found"
    143 		fi
    144 		;;
    145 	shutdown)
    146 		if type systemctl > /dev/null 2>&1
    147 		then
    148 			exec systemctl poweroff
    149 		elif type poweroff > /dev/null 2>&1
    150 		then
    151 			exec my term -e sudo poweroff
    152 		else
    153 			__mylog "my shutdown" "No handler found"
    154 		fi
    155 		;;
    156 	perms)
    157 		exec my-perms "$@"
    158 		;;
    159 	init)
    160 		exec my-init "$@"
    161 		;;
    162 	open)
    163 		exec my-open "$@"
    164 		;;
    165 	sound)
    166 		if type pactl > /dev/null 2>&1 && pactl stat > /dev/null 2>&1
    167 		then
    168 			[ -z "$1" ] && exit 1
    169 			if [ $1 = "status" ]
    170 			then
    171 				sink_status=`pactl list sinks | grep ^[[:space:]]*Volume | awk '{print $5}'`
    172 				source_status=`pactl list sources | grep ^[[:space:]]*Volume | awk '{print $5}'`
    173 				echo -n "Sinks: "
    174 				for sink in $sink_status
    175 				do
    176 					echo -n "$sink "
    177 				done
    178 				echo ""
    179 				echo -n "Sources: "
    180 				for source in $source_status
    181 				do
    182 					echo -n "$source "
    183 				done
    184 				echo ""
    185 				exit 0
    186 			else
    187 				exec pactl "$@"
    188 			fi
    189 		else
    190 			__mylog "my sound" "No handler found"
    191 		fi
    192 		;;
    193 	kbopts)
    194 		setxkbmap -option -option ctrl:nocaps
    195 		;;
    196 	screen)
    197 		# defer to local script if it exists
    198 		if [ -x "$HOME/bin/my-screen.local" ]
    199 		then
    200 			exec "$HOME/bin/my-screen.local" "$@"
    201 		fi
    202 		xrandr_options=`xrandr -q | grep ' disconnected' | awk '{print $1}'`
    203 		for option in $xrandr_options
    204 		do
    205 			xrandr --output $option --off
    206 		done
    207 
    208 		xrandr_options=`xrandr -q | grep ' connected' | awk '{print $1}'`
    209 		numoptions=`echo $xrandr_options | wc -w`
    210 		if [ $numoptions -eq 1 ]
    211 		then
    212 			xrandr --output $xrandr_options --auto --primary
    213 		else
    214 			# Prefer external monitors
    215 			# If more than one display is connected, then enable the
    216 			# first non-primary display, and turn off all the others.
    217 			primary=`xrandr -q | grep primary | awk '{print $1}'`
    218 
    219 			for option in $xrandr_options
    220 			do
    221 				[ $option = $primary ] && primary_found=1 && continue
    222 				[ -n "$primary_found" ] && selection=$option && break
    223 			done
    224 
    225 			xrandr --output $selection --auto --primary
    226 
    227 			for option in $xrandr_options
    228 			do
    229 				[ $option != $selection ] && xrandr --output $option --off
    230 			done
    231 		fi
    232 		my wallpaper
    233 		;;
    234 	brightness)
    235 		if type xbacklight > /dev/null 2>&1
    236 		then
    237 			if xbacklight -list > /dev/null 2>&1
    238 			then
    239 				if [ "$1" = "monitor" ]
    240 				then
    241 					# Future-proofing ... gmux is specific, imagine other
    242 					# systems will be different
    243 					for opt in gmux intel_backlight
    244 					do
    245 						selection=`xbacklight -list | grep $opt`
    246 						num=`echo $selection | wc -w`
    247 						[ $num -gt 0 ] && break
    248 					done
    249 				else
    250 					selection=`xbacklight -list | grep $1`
    251 					num=`echo $selection | wc -w`
    252 				fi
    253 				[ $num -gt 1 ] && __mylog "my brightness" "More than one $1" && exit 1
    254 				[ $num -lt 1 ] && __mylog "my brightness" "No $1 to adjust" && exit 1
    255 				shift
    256 				exec xbacklight -ctrl $selection "$@"
    257 			else
    258 				if [ "$1" = "monitor" ]
    259 				then
    260 					shift
    261 					exec xbacklight "$@"
    262 				else
    263 					__mylog "my brightness" "handler incapable of adjusting key brightness"
    264 				fi
    265 			fi
    266 		else
    267 			__mylog "my brightness" "No handler found"
    268 		fi
    269 		;;
    270 	battery)
    271 		uname=`uname -s`
    272 
    273 		case $1 in
    274 			remaining)
    275 				case $uname in
    276 					*OpenBSD*)
    277 						apm -l
    278 						;;
    279 					*Linux*)
    280 						if [ `acpi | wc -l` -eq 2 ]
    281 						then
    282 							acpi | awk 'getline' | awk '{print $4}' | sed 's~%~~' | sed 's~,~~'
    283 						else
    284 							acpi | awk '{print $4}' | sed 's~%~~' | sed 's~,~~'
    285 						fi
    286 						;;
    287 					*Darwin*)
    288 						battery_info=`ioreg -rc AppleSmartBattery`
    289 						echo $battery_info | grep -o '"CurrentCapacity" = [0-9]\+' | awk '{print $3}'
    290 						;;
    291 					*CYGWIN*)
    292 						wmic path Win32_Battery Get EstimatedChargeRemaining /format:list 2>/dev/null | grep '[^[:blank:]]' | cut -d= -f2
    293 						;;
    294 					*)
    295 						echo "no battery status"
    296 						exit 1
    297 				esac
    298 				exit 0
    299 				;;
    300 			total)
    301 				case $uname in
    302 					*Linux*|*CYGWIN*|*OpenBSD*)
    303 						echo 100
    304 						;;
    305 					*Darwin*)
    306 						battery_info=`ioreg -rc AppleSmartBattery`
    307 						echo $battery_info | grep -o '"MaxCapacity" = [0-9]\+' | awk '{print $3}'
    308 						;;
    309 					*)
    310 						echo "no battery status"
    311 						exit 1
    312 				esac
    313 				exit 0
    314 				;;
    315 			percent)
    316 				echo "$((`my battery remaining` * 100 / `my battery total`))"
    317 				exit 0
    318 				;;
    319 		esac
    320 		;;
    321 	status)
    322 		echo -n "Batt: `my battery percent`% | "
    323 		cat $HOME/var/forecastio/current_forecast.txt | tr -d '\n'
    324 		printf " | "
    325 		/bin/date +'%l:%M %p'
    326 		exit 0
    327 		;;
    328 	i3status)
    329 			while :
    330 			do
    331 				echo "`my status`" || exit 1
    332 				sleep 15
    333 			done
    334 		;;
    335 	copy)
    336 		uname=`uname -s`
    337 
    338 		case $uname in
    339 			*Darwin*)
    340 				reattach-to-user-namespace pbcopy
    341 				;;
    342 			*CYGWIN*)
    343 				cat > /dev/clipboard
    344 				;;
    345 			*Linux*|OpenBSD)
    346 				exec xclip
    347 				;;
    348 			*)
    349 				return
    350 		esac
    351 		;;
    352 	paste)
    353 		uname=`uname -s`
    354 
    355 		case $uname in
    356 			*Darwin*)
    357 				reattach-to-user-namespace pbpaste
    358 				;;
    359 			*CYGWIN*)
    360 				cat /dev/clipboard
    361 				;;
    362 			*Linux*|OpenBSD)
    363 				xclip -o
    364 				;;
    365 			*)
    366 				return
    367 		esac
    368 		;;
    369 	netrc)
    370 		exec my-netrc "$@"
    371 		;;
    372 	dotfiles)
    373 		predicate="$1" && shift
    374 		case $predicate in
    375 			check)
    376 				cd "$HOME"
    377 			aweekago=$(($(/bin/date +%s) - 604800))
    378 			lastupdate=$(stat --format=%Y ._.djmoch)
    379 			[ $lastupdate -lt $aweekago ] && "Dotfiles more than one week old"
    380 				;;
    381 			update)
    382 				if [ -d "$HOME/.dotfiles_tgz" ]
    383 				then
    384 
    385 					if type sha256sum > /dev/null 2>&1
    386 					then
    387 						sha_exec=sha256sum
    388 						fieldnum=1
    389 					else
    390 						sha_exec=sha256
    391 						fieldnum=4
    392 					fi
    393 
    394 					curl -s https://git.danielmoch.com/dotfiles/snapshot/dotfiles-master.tar.gz > /tmp/dotfiles.tgz.$$
    395 					oldsum=`$sha_exec "$HOME/.dotfiles_tgz/dotfiles.tar.gz" | cut -d ' ' -f $fieldnum`
    396 					newsum=`$sha_exec "/tmp/dotfiles.tgz.$$" | cut -d ' ' -f $fieldnum`
    397 					if [ ! "$oldsum" = "$newsum" ]
    398 					then
    399 						mv /tmp/dotfiles.tgz.$$ "$HOME/.dotfiles_tgz/dotfiles.tar.gz"
    400 						cd "$HOME"
    401 						tar xzf .dotfiles_tgz/dotfiles.tar.gz
    402 						for file in $(ls -A dotfiles-master)
    403 			do
    404 				cp -r dotfiles-master/$file .
    405 			done
    406 						rm -rf dotfiles-master
    407 					else
    408 						rm /tmp/dotfiles.tgz.$$
    409 					fi
    410 				fi
    411 				my init -f > /dev/null 2>&1
    412 				touch "$HOME/._.djmoch"
    413 				;;
    414 		esac
    415 		;;
    416 	cron)
    417 		[ "$1" = systemd ] && mycron_systemd=1
    418 		case ":$PATH:" in
    419 			*:"$HOME/bin":*) ;;
    420 			*) PATH="$HOME/bin:$PATH" ;;
    421 		esac
    422 		cronpath="$HOME/lib/cron.d"
    423 		logpath="$HOME/var/log"
    424 		for job in $cronpath/*
    425 		do
    426 			if [ -x $job ]
    427 			then
    428 				if [ -n "$mycron_systemd" ]
    429 				then
    430 					$job
    431 				else
    432 					$job > $logpath/`basename $job` 2>&1
    433 				fi
    434 			fi
    435 		done
    436 		;;
    437 	login_async)
    438 		starttime=`/bin/date '+%s'`
    439 		runningtime=0
    440 		while [ $runningtime -le 120 ]
    441 		do
    442 			if ping -c 1 danielmoch.com > /dev/null 2>&1
    443 			then
    444 				connected=1
    445 				break
    446 			fi
    447 			sleep 1
    448 			runningtime=$((`/bin/date '+%s'` - $starttime))
    449 		done
    450 		if [ -n "$connected" ]
    451 		then
    452 			cd "$HOME"
    453 		aweekago=$(($(/bin/date +%s) - 604800))
    454 		lastupdate=$(stat -f %m ._.djmoch)
    455 		[ $lastupdate -lt $aweekago ] && my dotfiles update
    456 		fi
    457 		;;
    458 	login)
    459 		my login_async &
    460 		;;
    461 	processes)
    462 		if [ -L /bin/ps ]
    463 		then
    464 			# busybox
    465 			ps -o user,comm= | grep $LOGNAME | awk '{print $2;}'
    466 		else
    467 			ps -U $LOGNAME -o comm=
    468 		fi
    469 		;;
    470 	*) echo "my: command $command not found" >&2
    471 esac