#!/bin/sh 

# Shade -- interactive shell script for manipulating Marco Nelissen's WindowShade app_server
# hack. Run install script before running this. Useless without WindowShade, which can
# be downloaded from: http://www.xs4all.nl/~marcone/be.html .
# This script isn't finished yet, but I'm not going to have another chance to work on it for a while, so ...
# Have fun.
#
# Scot Hacker, beos@birdhouse.org, www.birdhouse.org/beos/software/
# Freeware, always.


## Define the settings directory plus locations for WindowShade and Shade
settings="/boot/home/config/settings/Shade"
ShadeLoc=`cat $settings/.shadeloc`
WindowShadeLoc=`cat $settings/.winshadeloc`
ColorLoc="$ShadeLoc/colorkey"



### Look for the settings file file created by the Shade installer. Quit if absent.
if [ -f $settings/\.shadeloc ]; then
	WindowShadeLoc=`cat $settings/.winshadeloc`
	echo
else
	echo Shade can\'t find its settings file.
	echo Please re-run the shade installer.
	echo Quitting now.
	exit
fi 	



### Check for existence of WindowShade as specified in 
### Shade settings file. Quit if absent.
if [ -f $WindowShadeLoc ]; then
	echo Good. WindowShade found.
	export WinShadeLoc="$WindowShadeLoc"
else
	echo WindowShade is not where your settings file says it should be.
	echo Make sure WindowShade is installed on your system. If it is\,
	echo re\-run the Shade installer script.
	echo Quitting now\.
	exit
fi



### Read in variables from last current config file

if [ -f $settings/current ]; then

td=`grep 3d $settings/current | cut -d' ' -f3`  
tabon=`grep -w tabon $settings/current | cut -d' ' -f2`  
taboff=`grep -w taboff $settings/current | cut -d' ' -f2`  
activeborder=`grep -w activeborder $settings/current | cut -d' ' -f2`  
inactiveborder=`grep -w inactiveborder $settings/current | cut -d' ' -f2`  
frame=`grep -w frame $settings/current | cut -d' ' -f2`  

	else
## Defaults if no current config is found	
td=1.9
tabon=ffcb00
taboff=ebebeb
activeborder=d8d8d8
inactiveborder=ebebeb
frame=989898

fi 	


### Main menu
echo 
echo Set the color of a single UI element:
echo 
echo 1\) Active title tab
echo 2\) Inactive title tab
echo 3\) Active border
echo 4\) Inactive border
echo 5\) Frame
echo D\) 3-D Effects
echo
echo Or save and restore configurations:
echo
echo 6\) Save current settings to a config file
echo 7\) Restore a saved configuration
echo 8\) Restore original BeOS colors
echo 9\) Print current variables
echo Q\) Quit
echo 
read choice



### Save config routine

if [ "$choice" = "6" ]; then 
	echo 
	echo What would you like to call this config\?
	read newname
		{ 
		echo \#!/bin/sh
		echo $WindowShadeLoc -3d $td \\
		echo -tabon $tabon \\
		echo -taboff $taboff \\
		echo -activeborder $activeborder \\
		echo -inactiveborder $inactiveborder \\
		echo -frame $frame \\
		echo -refresh
		} > $settings/$newname

	echo $newname has been saved.
	exit
fi	



### Read 3-D customizations
if [ "$choice" = "d" ]; then 
	echo 3-D effects are controlled by changing the apparent depth of
	echo borders, buttons, and tabs. The default is 1.9, but you can try 
	echo numbers as high as 20 with interesting effects. Negative
	echo values are accepted too. Enter a dimensional number here:
	read td
	echo Dimensionality set to $td .
	
		{ 
		echo \#!/bin/sh
		echo $WindowShadeLoc -3d $td \\
		echo -tabon $tabon \\
		echo -taboff $taboff \\
		echo -activeborder $activeborder \\
		echo -inactiveborder $inactiveborder \\
		echo -frame $frame \\
		echo -refresh
		} > $settings/current
		echo Change committed to current config.
	
		$WinShadeLoc -tabon $tabon -taboff $taboff -3d $td -refresh &
		
	exit
fi	



### Restore config routine
if [ "$choice" = "7" ]; then
	echo 
	echo Which existing configuration would you like to restore?
	ls $settings/
	read restore
	echo Restore $restore\? \(Y\/N\)
	
		read yn
		if [ "$yn" = "y" ]; then 
			cp $settings/$restore $settings/current
			$settings/$restore &
			exit
			else 
			echo No changes made.
			exit
		fi	

fi	


if [ "$choice" = "8" ]; then 
echo Resetting BeOS colors.
{
echo \#!/bin/sh
echo $WindowShadeLoc -3d 1.9 \\
echo -tabon ffcb00 \\
echo -taboff ebebeb \\
echo -activeborder d8d8d8 \\
echo -inactiveborder ebebeb \\
echo -frame 989898 \\
echo -refresh	
} > /boot/home/config/settings/Shade/current

/boot/home/config/settings/Shade/current
exit
fi	


if [ "$choice" = "9" ]; then 

### Get Netscape names from hex
tabonname=`grep -i -w $tabon $ColorLoc | cut -d' ' -f2`
taboffname=`grep -i -w $taboff $ColorLoc | cut -d' ' -f2`
activebname=`grep -i -w $activeborder $ColorLoc | cut -d' ' -f2`
inactivebname=`grep -i -w $inactiveborder $ColorLoc | cut -d' ' -f2`
framename=`grep -i -w $frame $ColorLoc | cut -d' ' -f2`

	echo Variables are currently set at:
		echo -3d $td	
		echo -tabon $tabon -o- Netscape name: $tabonname
		echo -taboff $taboff -o- Netscape name: $taboffname
		echo -activeborder $activeborder -o- Netscape name: $activebname
		echo -inactiveborder $inactiveborder -o- Netscape name: $inactivebname
		echo -frame $frame -o- Netscape name: $framename
	exit
fi	


if [ "$choice" = "q" ]; then 
	echo Quitting.
	exit
fi	



	### Individual settings
		echo
		echo Spell color names as they appear 
		echo in Netscape color tables. Case is unimportant. 
		echo See included HTML pages for examples.
		echo
	
	
	if [ "$choice" = "1" ]; then 
		choice=tabon
		echo Enter a Netscape color name.
		read color
	fi	
	
	if [ "$choice" = "2" ]; then 
		choice=taboff
		echo Enter a Netscape color name:
		read color
	fi	
	
	if [ "$choice" = "3" ]; then 
		choice=activeborder
		echo Enter a Netscape color name:
		read color
	fi	
	
	if [ "$choice" = "4" ]; then 
		choice=inactiveborder
		echo Enter a Netscape color name:
		read color
	fi	
	
	if [ "$choice" = "5" ]; then 
		choice=frame
		echo Enter a Netscape color name:
		read color
	fi	



### Grep through $ColorKeyLoc file to find hex equivalent of chosen color

color=`grep -i -w $color $ColorLoc | cut -d' ' -f1`  

while [ "$color" = "" ]; do
	echo That color does not exist or is spelled incorrectly. 
	echo Try again \(Ctrl+C to quit\).
	read color
	color=`grep -i -w $color $ColorLoc | cut -d' ' -f1` 
done





### Change the object variable to match user's choice.


	if [ "$choice" = "tabon" ]; then
		tabon=$color
	fi
	
	if [ "$choice" = "taboff" ]; then
		taboff=$color
	fi
	
	if [ "$choice" = "activeborder" ]; then
		activeborder=$color
	fi
	
	if [ "$choice" = "inactiveborder" ]; then
		inactiveborder=$color
	fi
	
	if [ "$choice" = "frame" ]; then
		frame=$color
	fi				


### Confirm change ###
	echo Set $choice color to $color\? \(Y\/N\)
	echo
	read yn
	if [ "$yn" = "y" ]; then 


### Save new variables into permanent settings file
		{ 
		echo \#!/bin/sh
		echo $WindowShadeLoc -3d $td \\
		echo -tabon $tabon \\
		echo -taboff $taboff \\
		echo -activeborder $activeborder \\
		echo -inactiveborder $inactiveborder \\
		echo -frame $frame \\
		echo -refresh
		}  > $settings/current


### Commit the changes

	$WindowShadeLoc -$choice $color -refresh
	else 
	echo No changes made.
	echo
	exit
fi
