unpack() {
# tomada de ebuild, proyecto gentoo


        ###############DISTDIR=$SOURCES
	local srcdir
	local x
	local y
	local myfail
	local eapi=${EAPI:-0}
	[ -z "$*" ]  && msg 4 "Nothing passed to the 'unpack' command"

	for x in "$@"; do
		msg 7 ">>>>>>>>>> Unpacking ${x} to ${UPACKINSTALLDIR}"
		
		y=${x%.*}
		y=${y##*.}

		if [[ ${x} == "./"* ]] ; then
			srcdir=""
		elif test -f ${ORI}/${x} ; then
                        msg 8 "SOURCE $ORI"
			srcdir=${ORI}/
		elif [[ ${x} == ${SOURCES%/}/* ]] ; then
			msg 4 "Arguments to unpack() cannot begin with \${SOURCES}."
                        return
#decia die
                        #exit
		elif [[ ${x} == "/"* ]] ; then
			msg 4 "Arguments to unpack() cannot be absolute"
                        return 
#decia die
                        #exit
		else
			srcdir="${SOURCES}/"
		fi
		if test ! -s ${srcdir}${x} ; then
                        msg 4 "${srcdir}${x} does not exist"
                        exit
                fi
                
		_unpack_tar() {
                     #echo KKKKKKKKKKKKKKKKK $srcdir - $x -$y
			if [ "${y}" == "tar" ]; then
                                
				$1 -dc "$srcdir$x" | tar xof -
                                #echo "puesto para que no falle, ver porque falla si lo saco, echo parece 0"
				#assert "$myfail"
			else
				$1 -dc "${srcdir}${x}" > ${x%.*} 
#|| die "$myfail"
			fi
		}

		myfail="failure unpacking ${x}"
		case "${x##*.}" in
			tar)
				tar xof "$srcdir$x" 
#|| die "$myfail"
				;;
			ZIP|zip|jar)
				unzip -qo "${srcdir}${x}" 
#|| die "$myfail"
				;;
			gz|Z|z)
				_unpack_tar gzip
				;;
			bz2|bz)
				_unpack_tar bzip2
				;;
			7Z|7z)
				local my_output
				my_output="$(7z x -y "${srcdir}${x}")"
				if [ $? -ne 0 ]; then
					echo "${my_output}" >&2
					#die "$myfail"
				fi
				;;
			RAR|rar)
				unrar x -idq -o+ "${srcdir}${x}" 
#|| die "$myfail"
				;;
			LHa|LHA|lha|lzh)
				lha xfq "${srcdir}${x}" 
#|| die "$myfail"
				;;
			a)
				ar x "${srcdir}${x}" 
#|| die "$myfail"
				;;

			lzma)
				_unpack_tar lzma
				;;
			xz)
				#if hasq $eapi 0 1 2 ; then
				#	echo "unpack ${x}: file format not recognized. Ignoring."
				#else
					_unpack_tar xz
				#fi
				;;
			*)
				msg 4 "unpack ${x}: file format not recognized. Ignoring."
				;;
		esac

	done

}

###########################3


#  case $ext  in
#tgz)
#   #
#	tar -C $UPACKDROOT -xozf "$PACKAGE" || msg 4 "$myfail"
#        mkdir -p $UPACKDINFO
#	cp -r $UPACKDROOT/.upackinfo/* $UPACKDINFO
#	rm -r $UPACKDROOT/.upackinfo
#				;;


#tbz)# sacadoo tbz2
#				bzip2 -dc "$PACKAGE" | tar -C $UPACKDROOT -xof -
#				#assert "$myfail"
#
#        mkdir -p $UPACKDINFO
#	cp -r $UPACKDROOT/.upackinfo/* $UPACKDINFO
#	rm -r $UPACKDROOT/.upackinfo
#				;;
#txz)
#				tar -C $UPACKDROOT -xoJf "$PACKAGE" 
#|| die "$myfail"
#        mkdir -p $UPACKDINFO
#	cp -r $UPACKDROOT/.upackinfo/* $UPACKDINFO
#	rm -r $UPACKDROOT/.upackinfo
#				;;
#
# *) 
     ORI=`pwd`
     cd $UPACKDROOT
   
     unpack $PACKAGE
     
     cd $ORI

#;;
#  esac
