shellUtils.sh 13.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
if [ "x" == "x$__SHELLUTILS__" ]
then
	__SHELLUTILS__="shellUtils"

	# -------------------------------
	# Fehlermeldungen
	_shellUtils_errMsg[1]="Fehler beim Aufruf von \${FUNCNAME}: falsche Parameteranzahl: $#"
	_shellUtils_errMsg[2]="Fehler: Es läuft bereits eine Instanz (\${oldPid}) des scripts"
	_shellUtils_errMsg[3]="Fehler beim Aufruf von doSmbCommand: smbCommand nicht initialisiert"
	_shellUtils_errMsg[4]="Fehler: konnte keine Konfigurations-Datei (\${cName}) finden"
	#_shellUtils_errMsg[2]=
	# -------------------------------

	# -------------------------------
	# Kommandodefinitions Teil
	SH="/bin/bash"
	SMBCLIENT="/usr/bin/smbclient"
	PS="/bin/ps"
	BASENAME="/usr/bin/basename"
	PWDCMD="/bin/pwd"
	CONVERT="/usr/bin/convert"
	ICONV="/usr/bin/iconv"
	ECHO="/bin/echo"
	TEST="/usr/bin/test"
	SED="/usr/bin/sed"
	PRINTF="/usr/bin/printf"
	SORT="/usr/bin/sort"
	FTP="/usr/bin/ftp"
	CONVERT="/usr/bin/convert"
	CJPEG="/usr/bin/cjpeg"
	CAT="/bin/cat"
	MKDIR="/bin/mkdir"
	DATE="/bin/date"
	RM="/bin/rm"
	MAILCMD="/bin/bin/mailto"
	CUT="/usr/bin/cut"
	TR="/usr/bin/tr"
	LOCALE="/usr/bin/locale"
	GREP="/bin/grep"
	WC="/usr/bin/wc"
	MV="/usr/bin/mv"
	CP="/usr/bin/cp"
	EXPR="/usr/bin/expr"
	UNZIP="/usr/bin/unzip"
	XSLTPROC="/usr/bin/xsltproc"
	GAWK="/usr/bin/gawk"
	AWK="/usr/bin/awk"
	LS="/bin/ls"
	TOUCH="/usr/bin/touch"
	RECODE="/usr/bin/recode"
	GETOPT="/usr/bin/getopt"
	# -------------------------------

	# -------------------------------
	# Variablen
	_smbCommand=""
	# -------------------------------

	# -------------------------------
	# Funktionen
	# Aufruf: checkLock lockfile
	function checkLock
	{
		if [ $# -ne 1 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		local filename="$1"

		if [ -e "$filename" ]
		then
			local oldPid="`$CAT "$lockFile"`"
			local scriptName="`$BASENAME $0`"
			local running="`$PS -p "$oldPid" | $SED '1d; /'$scriptName'/!d'`"

			if [ -n "$running" ]
			then
				eval "echo \"${_shellUtils_errMsg[2]}\" >&2"
				exit 2
			fi
		fi

		echo $$ >"$lockFile"

		return $$
	}

	# Aufruf: doSmbCommand share domain user pass
	function initSmbCommand
	{
		if [ $# -ne 4 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		_smbCommand="$SMBCLIENT $1 $4 -U $3 -W $2"
	}

	# Aufruf: doSmbCommand path command
	function doSmbCommand
	{
		if [ $# -lt 1 -o $# -gt 2 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		if [ -z "$_smbCommand" ]
		then
			eval "echo \"${_shellUtils_errMsg[3]}\" >&2"
			exit 3
		fi

		if [ $# -eq 2 ]
		then
			eval "$_smbCommand -D $1 -c \"$2\""
		else
			eval "$_smbCommand -D $1" <&0
		fi
	}

	# Meine Implementation von Hashes fuer die bash verwendet zwei Arrays,
	# deren "Basisname" immer bei den aufrufen der Hash-Funktionen mit
	# angegeben werden muessen. Die beiden Arrays heissen dann 
	# _<<Basisname>>_key und _<<Basisname>>_value und enthalten entsprechen
	# jeweils zu einem Index ein key/value Paar.
	# Die groesste Magie in den Funktionen liegt darin die Arraynamen
	# immer richtig zu erzeugen.
	# Wenn man dieses Hashes benutzt sollte man bedenken, das das
	# Laufzeitverhalten O(n) ist, die Implementierung also nicht sehr
	# performant.
	# PS. O(n) => im unguenstigsten Fall hat man n Schleifendurchlaeufe um
	# auf ein Element in einem Hash mit n Elementen zuzugreifen.
	# Ich mags kaum zugeben aber an manchen Stellen ist das Laufzeitverhalten
	# sogar O(2n). (Es besteht wohl noch dringender Optimirungsbedarf)

	# eine Funktion um einen Hash zu leeren
	# Aufruf: unsetHash hashname
	function unsetHash
	{
		if [ $# -ne 1 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		unset _${1}_key
		unset _${1}_value
	}

	# eine Funktion um einen HashWert zu setzen. ein key und ein value array
	# werden erzeugt. Ueber getHashVal kann man wieder Werte abfragen.
	# Aufruf: setHashVal hashname key value
	function setHashVal
	{
		if [ $# -ne 3 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		getHashVal "${1}" "${2}" >/dev/null 2>&1
		local idx=$?
		eval "_${1}_key[$idx]=\"$2\""
		eval "_${1}_value[$idx]=\"$3\""
	}

	# und eine funktion um über einen key einen Wert aus einem Hash 
	# (siehe setHashVal) zu bekommen.
	# Aufruf: getHashVal hash key
	function getHashVal 
	{ 
		if [ $# -ne 2 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		local i=0
		local count=`getHashSize $1`
		local key=`$ECHO $2 | $SED 's/#/\\\\#/'`

		while [ $i -lt $count ]
		do 
			if eval [ "\${_${1}_key[$i]}" = "$key" ]
			then 
				eval "echo \${_${1}_value[$i]}"
				return $i 
			fi 
		
			i=$((i+1)); 
		done; 

		return $i
	}

	# Aufruf: getHashSize hash
	function getHashSize
	{
		if [ $# -ne 1 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		eval "count=\${#_${1}_key[*]}"
		echo $count

		return $count
	}

	# Aufruf: getHashKeys hash
	function getHashKeys
	{
		if [ $# -ne 1 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		eval "ret=\"\${_${1}_key[0]}\""

		local i=1
		local count=`getHashSize $1`
		while [ $i -lt $count ]
		do
			eval "ret=\"$ret \${_${1}_key[$i]}\""
			i=$((i+1))
		done

		echo $ret

		return $count
	}

	# Funktion um die Zahlrepraesentation eines Monatsname zu bekommen.
	# Also Oktober => 10
	# Abhaengig davon das ein entsprechendes locale installiert ist.
	# Aufruf: getMonthNum Monatsname [locale]
	function getMonthNum
	{
		if [ $# -lt 1 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		if [ $# -eq 2 ]
		then
			LC_ALL=${2}
		fi

		mon=`$LOCALE -c LC_TIME | $SED '5!d;y/;/\n/' |\
			$GREP -n ${1} | $CUT -d\: -f1`

		echo $mon

		return $mon
	}

	# Funkktion um ein Kommando nach einem bestimmten Zeitraum abzubrechen
	# Aufruf: timeout cmd t
	# Retruns: >128 -> falls Timeout, return value von cmd->falls kein Timeout
	function timeout
	{  
		if [ $# -lt 2 ]
		then
			eval "echo \"${_shellUtils_errMsg[1]}\" >&2"
			exit 1
		fi

		# gewuenschtes Kommando als separaten Prozess starten
		$1 <&0 &
		local __cPid=$!

		# subshell starten die gewuenschte Zeit wartet bis sie obigen
		# Prozess killt.
		( sleep $2; kill -9 ${__cPid} ) >/dev/null 2>&1 &
		local __tPid=$!

		# neuen Trap setzen....evtl. vorhande alte Kommandos werden auch
		# ausgefuehrt! (Bei Programmabbruch auch auf jeden Fall beide 
		# Sub-Prozesse killen)
		oldTrap="`trap -p EXIT`"
		oldTrapCmd="`echo $oldTrap | sed 's/^trap -- \(.*\) EXIT$/\1/'`"

		trapCmd="kill -9 ${__cPid} >/dev/null 2>&1"
		trapCmd="${trapCmd};kill -9 ${__cPid} >/dev/null 2>&1"
		trapCmd="${trapCmd};eval $oldTrapCmd"
		trap "${trapCmd}" EXIT

		# warte bis Subprozess mit Kommando endet
		wait ${__cPid} >/dev/null 2>&1
		local __cExit=$?

		# alte trap Kommandos wiederherstellen
		test "$oldTrapCmd" && eval "$oldTrapCmd" || trap '-' EXIT

		# falls sie noch laeuft wartende subshell killen.
		kill -9 $__tPid >/dev/null 2>&1
		return $__cExit
	}
	# -------------------------------

fi

function getDiscInfo
{
	local device

	test $# -ge 1 && device="$1"

	unset diskData cddbId dTracks dLength dIndex dText dExtra

	diskData="`cdda2wav -D ${device} -N -J -v toc,sectors 2>&1`"

	cddbId="`echo "$diskData" |\
		sed '/^CDDB/!d;s/^.*0x\(.*\)$/\1/'`"
	dTracks="`echo "$diskData" |\
		sed '/tracks/!d;s/^.*tracks:\([0-9]*\).*$/\1/'`"
	dLength="`echo "$diskData" |\
		sed '/tracks/!d;s/^.*time \([0-9:\.]*\).*$/\1/'`"
	dIndex="`echo "$diskData" |\
		sed '/CDINDEX/!d;s/^.*: *\([a-zA-Z0-9_\.]*-\).*$/\1/'`"
	dText="`echo "$diskData" | sed '
		/CD-Text/!d;s/^[^:]*: *\(.*[^ ]\).*$/\1/'`"
	dExtra="`echo "$diskData" | sed '
		/CD-Extra/!d;s/^[^:]*: *\(.*[^ ]\).*$/\1/'`"
}

function getCDDBInfo
{
	local uri i hello

	uri="freedb.freedb.org/~cddb/cddb.cgi"

	test $# -ge 1 && cddbId="$1" || return 1
	test $# -ge 2 && uri="$2"

	hello="hello=${USER}+${HOSTNAME}+test+0.0.1"

	# first get the genre list from server
	eval "`curl -s "http://${uri}?cmd=cddb+lscat&${hello}&proto=6" |\
		sed '1d;$d' | tr -d '\r' | tr '\n' ',' |\
		sed 's/,$//;s/,/" "/g;s/^\(.*\)$/local -a genre=("\1")/'`"

	unset cddbInfo

	# search for info in all genres
	for i in "${genre[@]}"
	do
		local cmd="cmd=cddb+read+${i}+${cddbId}"
		local cddb="`curl -s "http://${uri}?${cmd}&${hello}&proto=6"`"

		local ret="`echo "$cddb" | head -c 3`"

		if [ "$ret" != "210" ]
		then
			if [ ${verbose:-0} -ge 3 ]
			then
				debug="`echo "$cddb" | head -n 1`"
				echo $"[ERROR] cddb: ${debug}"
			fi
		else
			cddb="`echo "$cddb" | tr -d '\r' | sed '1d;/^#/d;/^\.$/{d;q}'`"
			cddbInfo[${#cddbInfo[@]}]="$cddb"
		fi
	done
}

function getCDInfo
{
	local uri device sampler cnt cddb
	local delimiter

	sampler=0
	cddbEntry=""
	device="/dev/cdrom"
	uri="freedb.freedb.org/~cddb/cddb.cgi"

	test $# -ge 1 && sampler="$1"
	test $# -ge 2 && cddbEntry="$2"
	test $# -ge 3 && device="$3"
	test $# -ge 4 && uri="$4"

	getDiscInfo "$device"
	getCDDBInfo "$cddbId"

	cnt=${#cddbInfo[@]}
	if [ ${cnt} -gt 1 ]
	then
		if [ "$cddbEntry" -lt $cnt ] 2>/dev/null
		then
			cddb="${cddbInfo[$cddbEntry]}"
			echo "done"
		else
			echo
			echo "found more than 1 CDDB Entry. Please select:"
			
			local i=0;
			while [ $i -lt $cnt ]
			do
				local title="`echo "${cddbInfo[$i]}" | sed '/^DTITLE/!d;s/^.*=//'`"
				echo " [$i] - $title"
				i=$((i+1))
			done
			
			echo " [q] - quit script"
			echo

			cddb=""
			while [ -z "$cddb" ]
			do
				echo -n "==> "
				read i

				test "x$i" == "xq" && exit 1
				i="`echo "$i" | sed '/[^0-9]/d'`"
				test -z "$i" && continue

				cddb="${cddbInfo[$i]}"
				test -z "$cddb" && echo "wrong input ($i)"
			done
		fi
	else
		cddb="${cddbInfo[0]}"
		echo "done"
	fi

	eval "`echo "$cddb" | tr -d '\r' | sed '
		/DTITLE/s/^.*=\(\(.*\) \/ \)*\(.*\)$/dArtist="\2";dTitle="\3"/p
		/DYEAR/s/^.*=\(.*\)$/dYear="\1"/p
		/DGENRE/s/^.*=\(.*\)$/dGenre="\1"/p
		/TTITLE/s/^TTITLE\([0-9]*\)=\(.*\)$/tArtist[\1]="";tTitle[\1]="\2"/p
		d'`"

	if [ "$sampler" == "yes" ]
	then
		echo
		echo "some CDs contain content from various artist. If this is such a CD"
		echo "one might want to declare a pattern that separates the artist from"
		echo "the title information. Here are the titles as reported by CDDB:"
		echo

		for i in "${tTitle[@]}"
		do
			echo " - $i"
		done

		echo
		echo "if you want to split those entries in artist and title please give"
		echo "now the string pattern to split at."
		echo "Otherwise simply press ENTER here"
		echo -n "==> "

		__OLDIFS__="$IFS"
		IFS=""
		read delimiter
		IFS="$__OLDIFS__"

		if [ "x$delimiter" != "x" ]
		then
			eval "`echo "$cddb" | tr -d '\r' | sed '
				/TTITLE/s/^TTITLE\([0-9]*\)=\(\(.*\)'"${delimiter/\//\\/}"'\)*\(.*\)$/tArtist[\1]="\3";tTitle[\1]="\4"/p
				d'`"
		fi
	fi

	# Nu pack ich in alle tArtist die leer sind den dArtist.
	i=0
	while [ $i -lt ${#tArtist[@]} ]
	do
		tNum=$((i+1))
		tLength[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.(.*:.*)/ !d
			s/.*[^0-9]'$tNum'\.( *\([^)]*\).*$/\1/'`
		tStartSec[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.([^:]*)/ !d
			s/.*[^0-9]'$tNum'\.( *\([^)]*\).*$/\1/'`

		test $tNum -eq ${#tArtist[@]} && tNum="lead-out" || tNum=$((tNum+1))

		tEndSec[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.*([^:]*)/ !d
			s/.*[^0-9]'$tNum'\.*( *\([^)]*\).*$/\1/'`

		test -z "${tArtist[$i]}" && tArtist[$i]="$dArtist"
		i=$((i+1))
	done
}

function getCDInfo_old
{
	local uri device cmd i hello cddb ret diskData tNum

	uri="freedb.freedb.org/~cddb/cddb.cgi"
	device="/dev/cdrom"

	test $# -ge 1 && uri="$1"
	test $# -ge 2 && device="$2"

	hello="hello=${USER}+${HOSTNAME}+test+0.0.1"

	# first get the genre list from server
	eval "`curl -s "http://${uri}?cmd=cddb+lscat&${hello}&proto=6" |\
		sed '1d;$d' | tr -d '\r' | tr '\n' ',' |\
		sed 's/,$//;s/,/" "/g;s/^\(.*\)$/local -a genre=("\1")/'`"

	unset cddbId dTitle dYear dGenre tTitle

	diskData="`cdda2wav -D ${device} -N -J -v toc,sectors 2>&1`"

	cddbId="`echo "$diskData" |\
		sed '/^CDDB/!d;s/^.*0x\(.*\)$/\1/'`"
	dTracks="`echo "$diskData" |\
		sed '/tracks/!d;s/^.*tracks:\([0-9]*\).*$/\1/'`"
	dLength="`echo "$diskData" |\
		sed '/tracks/!d;s/^.*time \([0-9:\.]*\).*$/\1/'`"
	dIndex="`echo "$diskData" |\
		sed '/CDINDEX/!d;s/^.*: *\([a-zA-Z0-9_\.]*-\).*$/\1/'`"
	dText="`echo "$diskData" | sed '
		/CD-Text/!d;s/^[^:]*: *\(.*[^ ]\).*$/\1/'`"
	dExtra="`echo "$diskData" | sed '
		/CD-Extra/!d;s/^[^:]*: *\(.*[^ ]\).*$/\1/'`"

	# search for info in all genres
	for i in "${genre[@]}"
	do
		cmd="cmd=cddb+read+${i}+${cddbId}"
		#wget -O- "http://${uri}?${cmd}&${hello}&proto=6" 2>/dev/null
		cddb="`curl -s "http://${uri}?${cmd}&${hello}&proto=6"`"
	
		ret="`echo "$cddb" | head -c 3`"
		if [ "$ret" != "210" ]
		then
			if [ ${verbose:-0} -ge 3 ]
			then
				debug="`echo "$cddb" | head -n 1`"
				echo $"[ERROR] cddb: ${debug}"
			fi
			continue
		else
			eval "`echo "$cddb" | tr -d '\r' | sed '
				/DTITLE/s/^.*=\(\(.*\) \/ \)*\(.*\)$/dArtist="\2";dTitle="\3"/p
				/DYEAR/s/^.*=\(.*\)$/dYear="\1"/p
				/DGENRE/s/^.*=\(.*\)$/dGenre="\1"/p
				/TTITLE/s/^TTITLE\([0-9]*\)=\(\(.*\) \/ \)*\(.*\)$/tArtist[\1]="\3";tTitle[\1]="\4"/p
				d'`"
		fi
	done

	exit 1

	# Nu pack ich in alle tArtist die leer sind den dArtist.
	i=0
	while [ $i -lt ${#tArtist[@]} ]
	do
		tNum=$((i+1))
		tLength[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.(.*:.*)/ !d
			s/.*[^0-9]'$tNum'\.( *\([^)]*\).*$/\1/'`
		tStartSec[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.([^:]*)/ !d
			s/.*[^0-9]'$tNum'\.( *\([^)]*\).*$/\1/'`

		test $tNum -eq ${#tArtist[@]} && tNum="lead-out" || tNum=$((tNum+1))

		tEndSec[$i]=`echo "$diskData" | sed '
			/[^0-9]'$tNum'\.*([^:]*)/ !d
			s/.*[^0-9]'$tNum'\.*( *\([^)]*\).*$/\1/'`

		test -z "${tArtist[$i]}" && tArtist[$i]="$dArtist"
		i=$((i+1))
	done
}