#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2010 Christopher Bratusek		#
#							#
#########################################################

torswitch () {

	if ! [[ $EUID == 0 ]]; then
		exit 1
	fi

	case $1 in
		*on )
			if [[ $(grep forward-socks4a /etc/privoxy/config) == "" ]]; then
				echo "forward-socks4a / 127.0.0.1:9050 ." >> /etc/privoxy/config
			else
				sed -e 's/\# forward-socks4a/forward-socks4a/g' -i /etc/privoxy/config
				/etc/init.d/tor restart
				/etc/init.d/privoxy restart
			fi
		;;

		*off )
			sed -e 's/forward-socks4a/\# forward-socks4a/g' -i /etc/privoxy/config
			/etc/init.d/tor restart
			/etc/init.d/privoxy restart
		;;
	esac

}

repoadd () {

	reprepro -Vb "$REPO_HOME" include "$REPO_DIST" *.changes
	cd "$REPO_HOME"/dists/"$REPO_DIST"
	gpg -abs -o Release.gpg Release

}

wipe () {

	cryptsetup -d /dev/urandom -c aes-xts-plain create delete $1
	shred -vzn 0 /dev/mapper/delete
	sync && sleep 4
	cryptsetup remove delete

}

remount () {

	DEVICE=$1
	shift

	mount -oremount,$@ $DEVICE

}

xchown () {

	user="$1"
	shift

	for df in "$@";
		do su -c "chown -R $user:$user \"$df\"";
	done

}

psgrep() {

	if [[ $1 == "-u" ]]; then

		ps aux | grep -v grep | grep $2 | awk '{ print $2 " : " $11}' | tee .temp
		CMDS=$(cat .temp)

	elif [[ $1 != "" ]]; then

		ps aux | grep -v grep | grep "$1" | awk '{ print $11 " : " $2 " : " $1 }' | tee .temp
		CMDS=$(cat .temp)

	fi

	if [[ $CMDS == "" ]]; then
		echo "no matching process"
	fi

	rm -f .temp

}

pskill() {

	if [[ $1 ]]; then

		psgrep $1

		shift

		if [[ $CMDS != "" ]]; then

			echo -e "\nenter process number to kill:\n"
			read ID

			if [[ ! $ID == 0 || ! $ID == "" ]]; then
				kill $@ $ID
			fi

		fi

	fi

}
