#!/bin/bash
# check running as root
userid=`id -u`
if [ ! "$userid" = "0" ]; then
	echo "This script must be run as root...exiting"
	echo
	exit 1
fi
# get directory owner and group 
currentdir=`pwd`
ownerid=`stat -c %U $currentdir`
groupid=`stat -c %G $currentdir`
if [ "$ownerid" = "root" ]; then ownerid=0; fi
if [ "$groupid" = "root" ]; then groupid=0; fi
if [ "$currentdir" = "/" ]; then
	echo "This script cannot be run from the / directory...exiting"
	echo
	exit 1
fi

# check for read/write access and running on Linux fs
tf1=0;tf2=0
for i in `seq 1 5000`
do
	if [ "$tf1" = "0" ]; then
		if [ ! -f t$i ] && [ ! -d t$i ]; then tf1=t$i; fi
	elif [ "$tf2" = "0" ]; then
		if [ ! -f t$i ] && [ ! -d t$i ]; then tf2=t$i; fi
	else
		break
	fi
done
if [ ! "$tf1" = "0" ] && [ ! "$tf2" = "0" ]; then
	touch $tf1 2> /dev/null
	if [ ! -e $tf1 ]; then
		echo "Unable to create a test file. This indicates that this partition"
		echo "is mounted read only. To work with this script, the test file"
		echo "must be created on a *Linux* partition, and that"
		echo "partition must be mounted with r/w privileges...exiting"
		echo
		exit 1
	fi
	chmod +x $tf1
	chmod -x $tf1
	cp $tf1 $tf2
	if [ -x $tf2 ]; then
		rm $tf1; rm $tf2
		echo "This script must be run on a Linux file system. It appears to be either FAT/FAT32 or NTFS."
		echo " Please run this script on a Linux partition (EXT2/3/4 or REISERFS)"
		echo "...exiting"
		echo
		exit 1
	fi
	rm $tf1; rm $tf2
fi

yn=0
while [ "$yn" = "0" ]
do
	clear
	echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	echo "        Script for changing default compiz window decorator in PCLinuxOS"  
	echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	echo
	echo "Y/y switch to EMERALD window decorator"
	echo
	echo "N/n switch to GTK window decorator "
	echo
	echo -n "Enter y or n, then press <Enter>: "
	read yn
	if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
		yn=y
		break
	elif [ "$yn" = "n" ] || [ "$yn" = "N" ]; then
		yn=n
		break
	fi
	yn=0
done

if [ "$yn" = "n" ]
	then
	echo
	echo "You have chosen GTK as a default Window Decorator for Compiz" 
	grep -l "yes}" $(which compiz-fusion) | xargs -r sed -i 's/yes}/no}/g'
	echo
else
	echo
	echo "You have chosen Emerald as a default Window Decorator for Compiz"
	grep -l "no}" $(which compiz-fusion) | xargs -r sed -i 's/no}/yes}/g'
	echo
fi
	echo "If you want to see/apply changes do logout/login "
	
