#!/bin/bash
#
# Copyright (C) 2005, 2006 Lasse Collin <lasse.collin@tukaani.org>
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Last modified: 2005-08-08

_pkgfile() {
	sed -r '1i\
# Converted from CRUX ports format to TukBuild using crux2tukrepo.\

		s#^    #\t#
		s#\b(name|version|source)=#\U\1=#g
		s#\$(name|version|source)#$\U\1#g
		s#\$\{(name|version|source)\}#$\{\1\}#g
		s#\$SRC#$DISTFILES#g
		s#\brelease=([^ ]*)\b#BUILD=\$\{BUILD:-\1\}#g
		s#--libdir=[^ ]##
		s#--prefix=/usr/X11R6#--prefix=$X11PREFIX --libdir=$LIBDIR#
		s#--prefix=/usr#--prefix=$PREFIX --libdir=$LIBDIR#
		s#--sysconfdir=/etc#--sysconfdir=$SYSCONFDIR#
		s#--localstatedir=/var#--localstatedir=$LOCALSTATEDIR#
		s%^.*\brm .*usr/[^ ]*(man|info|doc|share/doc)\b.*$%#&%
		/^SOURCE=/,${/^ *$/{r /dev/stdin
			d}}
		/^build *\(\)/s#^.*$#&\n\t_explode_all\n\t_cflags -O2#
		/^\}$/{s%^.*$%\t_doc_except 0 $NAME-$VERSION\n#\t_chfix\n\t_chgrp_bin\n&%}
		' "${_SOURCE_DIR}/Pkgfile"
}

_parsemd5sum() {
	local SOURCE_MD5 I
	for ((I=0; I < ${#source[@]}; I++)); do
		SOURCE_MD5[I]=$(grep " $(basename "${source[I]}")$" \
				"${_SOURCE_DIR}/.md5sum" | cut -f 1 -d ' ')
	done
	echo -en 'SOURCE_MD5=(\t'
	I=0
	while :; do
		if [ "${SOURCE_MD5[$((I+1))]}" = "" ]; then
			echo -e "${SOURCE_MD5[I]} )"
			break
		fi
		echo -en "${SOURCE_MD5[I]}\\n\\t\\t"
		I=$((I + 1))
	done
	echo
}

_createdesc() {
	local I
	I=${#name}
	I=$((66 - I))
	[ $I -lt 4 ] && I=4 # Minimal input validation
	sed -n "s%^# *Description: *\([^ ].\{1,$I\}\).*$%$name (\1)\n%p
		s%^# *URL: *\([^ ]\{1,70\}\)$%\1%p
		" "${_SOURCE_DIR}/Pkgfile"
}

if [ "$1" = "--help" ]; then
	cat << "EOF"

crux2tukbuild - convert a CRUX port to a TukBuild

usage: crux2tukbuild [sourcedir] [destdir]

sourcedir is the directory containing Pkgfile and .md5sum files, possibly
with other files related to the port. destdir is the directory where
$name.TukBuild and $name.desc files with other required files are put.
If destdir or both destdir and sourcedir are omitted, current directory
is assumed.

EOF
	exit 0
fi

# If no arguments are given, source and destination are assumed
# to be current directory:
_SOURCE_DIR=${1:-.}
_DEST_DIR=${2:-.}

if [ ! -d "${_SOURCE_DIR}" -o ! -d "${_DEST_DIR}" ]; then
	echo "Source and/or destination are not directories."
	echo "Use \`--help' for more info."
	exit 1
fi

# Full paths:
_SOURCE_DIR=$(cd "${_SOURCE_DIR}"; pwd)
_DEST_DIR=$(cd "${_DEST_DIR}"; pwd)

# Check the existence of Pkgfile and .md5sum. .footprint is ignored.
if [ ! -f "${_SOURCE_DIR}/Pkgfile" -o ! -f "${_SOURCE_DIR}/.md5sum" ]; then
	echo "Pkgfile and/or .md5sum missing from the source directory."
	exit 1
fi

# Source the Pkgfile:
. "${_SOURCE_DIR}/Pkgfile"

if [ "$(echo "$name" | sed '/[^a-zA-Z0-9.!@_+-]/d')" != "$name" ]; then
	echo "name=$name contains unacceptable characters."
	exit 1
fi

_parsemd5sum | _pkgfile > "${_DEST_DIR}/$name.TukBuild"

_createdesc > "${_DEST_DIR}/$name.desc"

if [ "${_SOURCE_DIR}" != "${_DEST_DIR}" ]; then
	for I in "${source[@]}"; do
		[ -f "${_SOURCE_DIR}/$I" ] \
				&& cp "${_SOURCE_DIR}/$I" "${_DEST_DIR}"
	done
fi

exit 0
