shithub: rc

ref: f3afdf563f784c0a5703496f48c1a72dff4617f4
dir: /setbg/

View raw version
#!/bin/rc

rfork en
bgdir=$home/lib/theme/bg

fn default{
	>/dev/theme cat <<'...'
rioback 777777
...
	exit
}

fn list{
	if(! test -d $bgdir)
		error nodir
	bglist=`{walk -f $bgdir | grep -e '*.bit' | sort}
	switch($bglist){
	case ''
		error nolist
	case *
		echo 'available backgrounds in' $bgdir
		for(i in $bglist)
			echo '	-' `{basename $i .bit}
		exit
	}
}

fn error{
	switch($1){
	case nodev
		echo 'error: /dev/theme not found'
		echo 'is rio running and is it patched?'
		exit nodev
	case nodir
		echo 'error:' $bgdir 'is not a directory'
		exit nodir
	case nofile
		echo 'error: background file for "'$arg'" not found in' $bgdir
		exit nofile
	case nolist
		echo 'error: no valid backgrounds found in' $bgdir
		exit nolist
	case nopath
		echo 'error: no path specified'
		usage
	case nobg
		echo 'error: no background file specified'
		usage
	case unknown
		echo 'error: unknown option "'$flag'"'
		usage
	}
}

fn help{
	echo 'options:'
	echo '	-d for default colors'
	echo '	-p for setting $bgdir (default=$home/lib/theme/bg)'
	echo '	-l for list of available backgrounds'
	echo '	-r for random background'
	echo 'requires:'
	echo '	- rio patched for theme support (see https://ftrv.se/14)'
	echo '	- <name>.bit files in $bgdir'
	echo '		* ex:' $bgdir/glenda_wallpaper.bit
	usage
}

fn random{
	if(! test -d $bgdir)
		error nodir
	randbg=`{fortune <{walk -f $bgdir | grep -e '*.bit'}}
	switch($randbg){
	case ''
		error nolist
	case *
		echo rioback $randbg >/dev/theme
		exit
	}
}


fn usage{
	echo 'usage: setbg [-h help] [-d default] [-p /path/to/bgdir] [-l list] [-r random] <name>'
	exit usage
}


############ Main
if(! test -f /dev/theme)
	error nodev

while(~ $1 -*){
	switch($1){
	case -p
		switch($2){
		case '' -*
			error nopath
		case *
			bgdir=$2
			shift 2
		}
	case -d
		default
	case -h -help --help
		help
	case -l
		list
	case -r
		random
	case *
		flag=$1
		error unknown
	}
}
switch($#*){
case 0
	if(~ $#bg 1)
		arg=$bg
	if not
		error nobg
case 1
	arg=$1
case *
	usage
}

if(! test -d $bgdir)
	error nodir

if(! test -f $bgdir/$arg.bit)
	error nofile

echo rioback $bgdir/$arg.bit > /dev/theme