#!/bin/bash
#
# Copyright (C) 2013  Etersoft
# Copyright (C) 2013  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


# list all supported formats: tar rar and so on
list_formats()
{
	a= patool formats | grep ".* files:" | sed "s/ .*//g"
	# TODO: do not use patool formats in such case?
	# See ArchiveCompressions in patool
	echo "bz2 gz gzip lzip xz lzma compress bzip2 Z tar.gz tar.bz2 tar.xz tar.bzip2 tar.lzma cpio.gz cpio.bz2 cpio.xz"
}

# returns true if XXX:
# TODO: check for supported target
is_target_format()
{
	[ "${1/:/}" = "$1" ] && return 1
	return 0
}

# TODO: detect by name, without comparing with existing?
# 1.zip -> zip
get_archive_ext()
{
	local i
	for i in $(list_formats) ; do
		[ -z "${1/*.$i/}" ] && echo "$i" && return
	done
	return 1
}

# 1.zip -> 1
get_archive_name()
{
	local ext
	ext=$(get_archive_ext "$1")
	if [ -n "$ext" ] ; then
		echo $(dirname "$1")/$(basename "$1" .$ext)
	else
		echo "$1"
		return 1
	fi
}

# FIXME
is_plain_text()
{
	file --mime-type "$1" | grep -q "text/" && echo "plain" && return
	file "$1" | grep -q "empty" && echo "plain" && return
	return 1
}


# TODO: improve
# TODO: detect by extension as default
get_archive_type()
{
	local aext
	test -r "$1" || { warning "Couldn't read $1" ; return 1; }

	# TODO: rewrite with mime
	file "$1" | grep -q "Zip archive data" && echo "zip" && return
	file "$1" | grep -q "RAR archive data" && echo "rar" && return

	if aext=$(get_archive_ext "$1") ; then
		echo $aext
		return
	fi
	return 1
}
