#! /bin/bash

# Unpacks downloaded sources in standards directories

echo "

This tool will unpack downloaded sources for compiling *MoviX*.
It will only work if you have downloaded the compressed source files.

Press CTRL+C now if you don't want this.
"

# Constants and setup

# The files to unpack. Change this only if you know what you're doing.
unpack="linux.tar.bz2=linux
mplayer.tar.bz2=mplayer
alsa.tar.bz2=alsa
alsa-utils.tar.bz2=alsa-utils
alsa-lib.tar.bz2=alsa-lib
sdl.tar.gz=sdl
perl.tar.gz=perl
aalib.tar.gz=aalib
lirc.tar.bz2=lirc
em8300.tar.gz=em8300
matroxset.tar.gz=matroxset
epiatvout.tar.gz=epiatvout
epiafb.tar.gz=epiafb
i810fb-lite.diff.bz2=kernel-patches
aureal88xx.tar.bz2=aureal88xx
acpi.diff.bz2=kernel-patches
bootsplash.diff.bz2=kernel-patches
splashutils.tar.bz2=splashutils
nvtv.tar.gz=nvtv
atitvout.tar.gz=atitvout
s3switch.zip=s3switch
devfsd.tar.bz2=devfsd
nmixer.tar.gz=nmixer
rexima.tar.gz=rexima
busybox.tar.bz2=busybox"

# The following seems to be already contained in Linux 2.4.21
# mga-tvout.gz=kernel-patches

# This doesn't seem to be necessary
# fbset-through-vt.gz=kernel-patches

# The following doesn't currently work (kernel 2.4.21)
# i810fb-lite.tar.bz2=i810fb-lite
# 

# ------------- No need to change anything below ------------
sourcedir=/home/movix/work/source

# Work begins here
while getopts ":hd" opt; do
	case $opt in

		# help
		h )     echo "Usage:

  -h   this help

  -d   delete the target directory before overwriting

"
                        exit;
		;;

		# delete the target directory
		d )	deleteTarget=y
		;;

	esac
done


# Give the user some time 
sleep 2

# Function for unpacking a file
unpack () {
# params: $1 file name, $2 directory

	if [ -e "$1" ]; then
		echo "Unpacking $1 to $2..."

		# Determine the file type
		case "$1" in 
			*.tar.bz2 )
				# Delete the target dir if the user asked for it
				if [ "X$deleteTarget" = "Xy" ]; then
					if [ -d "$2" ]; then
						rm -rf "$2"
					fi
				fi

				mkdir "$2.tmp"
				tar --bzip2 --directory "$2.tmp" -xf "$1"
				mv "$2.tmp"/* "$2"
				rm -rf "$2.tmp"
			;;
			
			*.tar.gz )
				# Delete the target dir if the user asked for it
				if [ "X$deleteTarget" = "Xy" ]; then
					if [ -d "$2" ]; then
						rm -rf "$2"
					fi
				fi

				mkdir "$2.tmp"
				tar --directory "$2.tmp" -xzf "$1"
				mv "$2.tmp"/* "$2"
				rm -rf "$2.tmp"
			;;
			
			*.zip )
				# Delete the target dir if the user asked for it
				if [ "X$deleteTarget" = "Xy" ]; then
					if [ -d "$2" ]; then
						rm -rf "$2"
					fi
				fi

				# zip without subdirs
				mkdir "$2"
				unzip -qqd "$2" "$1"
			;;
			
			*.diff.bz2 )
			# a bzipped patch
				mkdir -p "$2"
				cp "$1" "$2"
				pushd "$2"
				bunzip2 "$1"
				popd
			;;
			
			*.diff )
			# a plain-text patch
				mkdir -p "$2"
				cp "$1" "$2"
			;;
			
			*.gz )
			# probably a patch file
				mkdir -p "$2"
				cp "$1" "$2"
				pushd "$2"
				gunzip "$1"
				popd
			;;

			*) 
				echo "Don't know what to do with file $1"
			;;
		esac
	else
	# File not there - probably an optional file
		echo "Not unpacking $1 because it's not there."
	fi
}

# Main part

pushd $sourcedir

for line in $unpack; do
	# split the file specifications into file name and directory
	directory=`echo "$line" | sed 's/^\([^=]\+\)=\(.*\)$/\2/'` 
	filename=`echo "$line" | sed 's/^\([^=]\+\)=\(.*\)$/\1/'` 

	unpack "$filename" "$directory"
done

popd
