#!/bin/bash -e

#
# A fordításhoz használt fájlok terén jó adag rugalmasságot megengedünk.
# Nyilván elképzelhetetlen, hogy mondjuk a perms fájlt kézzel szerkesztő
# embert kötelezzük a szép tabulálás vagy az ábécérend tartására.
#
# Ugyanakkor úgy illendő, hogy SVN-be commit, valamint csomagfordítás előtt
# ezeket a fájlokat áteresztjük egy ellenőrző és kipofozó fázison.
#
# A szkriptnek tehát egyszerre két célt is kell szolgálnia.
#
# Az egyik az, hogy helyben hozza egységes alakra a fájlokat. Ily módon azok
# jobban átláthatók lesznek, ha tovább akarunk velünk dolgozni, valamint
# készen fognak állni az SVN rendszerbe berakásra.
#
# A másik cél az, hogy csomagfordítás előtt egységes alakra hozza a
# fájlokat, valamint a csomagfordító szkriptek életének megkönnyítése
# érdekében az opcionális fájlokat létrehozza, értelemszerűen általában
# nulla hosszúsággal. Tovább bonyolítja a helyzetet, hogy ilyenkor a forrás
# könyvtárat csak olvashatónak kell feltételeznie.
#
# A felülvizsgált fájlok új tartalmával minden esetben csak akkor írja felül
# a régit, ha a tartalom ténylegesen megváltozott. Így ha valamelyik fájl
# tartalma változatlan maradt, akkor az időcímkéje is marad a régi.
#
# A név történelmi okokból "check", talán lehetne jobb nevet is találni.
#

. /usr/lib/uhubuild/uhubuild-commonenvironment
. /usr/lib/uhubuild/uhubuild-common

shopt -s extglob

copy=no
while true; do
	case "$1" in
	  --copy)
		copy=yes
		shift
		continue
		;;
	  *)
		break
		;;
	esac
done

if [ $# -gt 1 ]; then
	die "Használat: uhubuild-check [--copy] [forrás-könyvtár]"
fi

# A forrás könyvtár vagy az aktuális, vagy a megadott paraméter
if [ -z "${UHUBUILD_SRCDIR:-}" ]; then
	export UHUBUILD_SRCDIR="$(cd "${1:-.}" && pwd -P)"
fi

cd "$UHUBUILD_SRCDIR"

if [ -z "${UHUBUILD_CHECK_TOPONLY:-}" ]; then
	# Ha egy forráskönyvtár valamely alkönyvtárában állunk, akkor kijjebb mászunk
	case "$UHUBUILD_SRCDIR" in
		*/packages/*/description|*/packages/*/summary|*/packages/*/words)
			cd ../../..
			;;
		*/packages/*)
			cd ../..
			;;
		*/addons*|*/config|*/packages|*/patches|*/sources)
			cd ..
			;;
		*)
			;;
	esac
	export UHUBUILD_SRCDIR="$(pwd -P)"
fi

if [ "$copy" = yes ]; then
	if [ -z "${UHUBUILD_SRC2DIR:-}" ]; then
		die "Nincsen megadva az UHUBUILD_SRC2DIR könyvtár!"
	fi
	rm -rf "$UHUBUILD_SRC2DIR"
	mkdir -p "$UHUBUILD_SRC2DIR"
else
	tmpdir UHUBUILD_SRC2DIR
	export UHUBUILD_SRC2DIR
fi

pp ()
{
	cat
}

no_double_empty_line ()
{
	local line nl start
	nl=1
	start=1
	# A grep betesz a végére egy newline-t, ha nem volna ott.
	# A read ugyanis nem látja a newline nélkül záródó sort.
	grep '' | while IFS='' read -r line; do
		if [ "$line" = "" ]; then
			nl=1
			continue
		fi
		if [ $nl = 1 ]; then
			if [ $start = 0 ]; then
				printf "\n"
			fi
			nl=0
			start=0
		fi
		printf "%s\n" "$line"
	done
}

no_empty_line ()
{
	grep -v '^$' || true
}

reformat ()
{
	local cmd
	if [ "$1" = 0 ]; then
		cmd=no_empty_line
	elif [ "$1" = 1 ]; then
		cmd=no_double_empty_line
	else
		die "reformat: hibás használat!"
	fi
	tr -d '\r' | sed -e 's/[[:blank:]]*$//' | sed -e '$a\' | $cmd
}

no_leading_slash ()
{
	sed -e 's@^/*@@'
}

leading_slash ()
{
	sed -e 's@^/*@/@'
}

no_trailing_slash ()
{
	sed -e 's@/$@@'
}

check ()
{
	true
	if [ ! -f "$1" ]; then
		error "Hiányzó file: $1"
		false
	fi
}

#
# Ismeretlen fájlnevek...
#

unknown ()
{
	if [ -f "$1" ]; then
		warn "ismeretlen fájl: $1"
	else if [ -d "$1" ]; then
			warn "ismeretlen könyvtár: $1"
		else
			warn "ismeretlen izé: $1"
		fi
	fi
}

illegal ()
{
	if [ -f "$1" ]; then
		error "nem megengedett fájl: $1"
	else if [ -d "$1" ]; then
			error "nem megengedett könyvtár: $1"
		else
			error "nem megengedett izé: $1"
		fi
	fi
}

must_be_dir ()
{
	if [ ! -d "$1" ]; then
		error "könyvtár kell legyen: $1"
	fi
}

must_be_file ()
{
	if [ ! -f "$1" ]; then
		error "fájl kell legyen: $1"
	fi
}

for file in *; do
	case "$file" in
	  acquire|compile|build-depends|doc|install|maintainer|\
	  options|release|sourcename|distribution|version|TODO|\
	  homepage|vendor|split-order|u2d)
		must_be_file "$file"
		;;
	  addons*|config|packages|patches|sources)
		must_be_dir "$file"
		;;
	  *)
		unknown "$file"
		;;
	esac
done

for file in packages/*; do
	must_be_dir "$file"
done

for file in packages/*/*; do
	case "${file##*/}" in
	  categories|conffiles|conflicts|depends|dirs |\
	  excludes|files|groups|obsoletes|options|perms|\
	  postinst |postrm |prerm  |priority|pre-depends|\
	  provides|replaces|section|users|suid_wrapper|libpath)
		must_be_file "$file"
		;;
	  description|summary|words)
		must_be_dir "$file"
		;;
	  *)
		unknown "$file"
		;;
	esac
done

for file in addons*; do
	if [ -d "$file" ]; then
		if ub_list "$file" -type l -print | grep '' >/dev/null; then
			error "az $file alatt nem lehet symlink!"
		fi
	fi
done

if [ -d patches ]; then
	while read file; do
		case "${file##*/}" in
			*.@(dif|diff|patch|patch0|patchR|tar|sh|skip|desc|txt)?(.@(gz|bz2|Z)));;
			*) illegal "$file" ;;
		esac
	done < <(ub_list patches -type f -print)
fi

#
# Először kipofozva átmásoljuk a fájlokat $UHUBUILD_SRC2DIR alá.
# Ez eléggé babra munka.
#

##############
### addons ###
##############
for dir in addons*; do
	if [ -d "$dir" ]; then
		mkdir -p "$UHUBUILD_SRC2DIR/addons"
		cp -a "$dir"/. "$UHUBUILD_SRC2DIR/addons/"
	fi
done

##############
### config ###
##############
if [ -d config ]; then
	mkdir -p "$UHUBUILD_SRC2DIR/config"
	cp -a config "$UHUBUILD_SRC2DIR/"
fi

###############
### patches ###
###############
if [ -d patches ]; then
	cp -a patches $UHUBUILD_SRC2DIR
	while read file; do
		case "$file" in
			*.@(gz|bz2|Z)) file="${file%.*}";;
		esac
	done < <(ub_list $UHUBUILD_SRC2DIR/patches -type f -print)
fi

#################################
### acquire, compile, install ###
#################################
for file in acquire compile install; do
	if [ -f $file ]; then
		pp < $file > $UHUBUILD_SRC2DIR/$file
		reformat 1 < $file > $UHUBUILD_SRC2DIR/$file
	fi
done

#####################
### build-depends ###
#####################
if [ -f build-depends ]; then
	pp < build-depends > $UHUBUILD_SRC2DIR/build-depends.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/build-depends.tmp | sort -u > $UHUBUILD_SRC2DIR/build-depends
	rm $UHUBUILD_SRC2DIR/build-depends.tmp
fi

####################
### distribution ###
####################
if check distribution; then
reformat 0 < distribution > $UHUBUILD_SRC2DIR/distribution
	if [ $(wc -l < $UHUBUILD_SRC2DIR/distribution) -ne 1 ]; then
		error "A distribution fájl nem egysoros!"
	fi
fi

###########
### doc ###
###########
if [ -f doc ]; then
	pp < doc > $UHUBUILD_SRC2DIR/doc.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/doc.tmp | no_leading_slash | no_trailing_slash | sort -u > $UHUBUILD_SRC2DIR/doc
	rm $UHUBUILD_SRC2DIR/doc.tmp
fi

################
### homepage ###
################
if [ -f "homepage" ]; then
	pp < homepage > $UHUBUILD_SRC2DIR/homepage.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/homepage.tmp > "$UHUBUILD_SRC2DIR/homepage"
	rm $UHUBUILD_SRC2DIR/homepage.tmp
	if [ "$(wc -l < "$UHUBUILD_SRC2DIR/homepage")" -ne 1 ]; then
		error "A homepage nem egysoros!"
	fi
	case "$(cat homepage)" in
		http://*|https://*)
			;;
		*)
			error "Ismeretlen URL formátum a homepage fájlban."
			;;
	esac
fi

##################
### maintainer ###
##################
if check maintainer; then
	reformat 0 < maintainer > $UHUBUILD_SRC2DIR/maintainer
	if [ $(wc -l < $UHUBUILD_SRC2DIR/maintainer) -ne 1 ]; then
		error "A maintainer fájl nem egysoros!"
	fi
fi

###############
### options ###
###############
if [ -f options ]; then
	pp < options
fi > $UHUBUILD_SRC2DIR/options.tmp
reformat 0 < $UHUBUILD_SRC2DIR/options.tmp | tr A-Z a-z | sort -u > $UHUBUILD_SRC2DIR/options
rm $UHUBUILD_SRC2DIR/options.tmp
while IFS='' read option; do
	case "$option" in
	  skip-strip)	;;
	  skip-optipng)	;;
	  *)
		error "Hibás érték az options fájlban: $option!"
		;;
	esac
done < $UHUBUILD_SRC2DIR/options

###############
### release ###
###############
if check release; then
	reformat 0 < release > $UHUBUILD_SRC2DIR/release
	if [ "$(wc -l < "$UHUBUILD_SRC2DIR"/release)" -ne 1 ]; then
		error "A release fájl nem egysoros!"
	fi
	if ! grep -q '^[0-9]*$' < $UHUBUILD_SRC2DIR/release; then
		error "A release fájl csak számot tartalmazhat!"
	fi
fi

##################
### sourcename ###
##################
if check sourcename; then
	reformat 0 < sourcename > $UHUBUILD_SRC2DIR/sourcename
	if [ "$(wc -l < "$UHUBUILD_SRC2DIR"/sourcename)" -ne 1 ]; then
		error "A sourcename fájl nem egysoros!"
	fi
fi

###############
### version ###
###############
if check version; then
	reformat 0 < version > $UHUBUILD_SRC2DIR/version
	if [ $(wc -l < $UHUBUILD_SRC2DIR/version) -ne 1 ]; then
		error "A version fájl nem egysoros!"
	fi
	if ! grep -q '^[0-9]' < $UHUBUILD_SRC2DIR/version; then
		error "A verzió csak számmal kezdődhet!"
	fi
#	allowed=".+~:"
	cp $UHUBUILD_SRC2DIR/version $UHUBUILD_SRC2DIR/version.tmp
	sed -i -e 's/[A-Za-z0-9.+~:]//g' $UHUBUILD_SRC2DIR/version.tmp
	if [[ `stat -c%s $UHUBUILD_SRC2DIR/version.tmp` > 1 ]]; then
		error "Nem megengedett karakter(ek) a version fájlban!"
	fi
	rm -f $UHUBUILD_SRC2DIR/version.tmp
fi

##############
### vendor ###
##############
if [ -f vendor ]; then
	reformat 0 < vendor > $UHUBUILD_SRC2DIR/vendor
	if [ $(wc -l < $UHUBUILD_SRC2DIR/vendor) -ne 1 ]; then
		error "A vendor fájl nem egysoros!"
	fi
fi

###################
### split-order ###
###################
if [ -f split-order ]; then
	reformat 0 < split-order > $UHUBUILD_SRC2DIR/split-order
	for i in $(<split-order); do
		if [ ! -d "packages/$i" ]; then
			error "Nem létező csomag a split-order-ben: packages/$i!"
		fi
	done
fi


##################
### packages/* ###
##################
ub_list packages -type d -mindepth 1 -maxdepth 1 -print | grep '' >/dev/null || \
	die "Nincs definiálva egy csomag sem!"
nofiles=0
for pkg in packages/*; do
	if [ ! -d "$UHUBUILD_SRCDIR/$pkg" ]; then
		continue
	fi

	mkdir -p "$UHUBUILD_SRC2DIR/$pkg"/{description,summary,words}

	#################################################
	### packages/*/{description,summary,words}/hu ###
	#################################################
	if [ -f $pkg/description/hu ]; then
		reformat 1 < $pkg/description/hu > $UHUBUILD_SRC2DIR/$pkg/description/hu
	else
		touch $UHUBUILD_SRC2DIR/$pkg/description/hu
	fi
	if check $pkg/summary/hu; then
		reformat 0 < $pkg/summary/hu > $UHUBUILD_SRC2DIR/$pkg/summary/hu
		if [ "$(wc -l < "$UHUBUILD_SRC2DIR/$pkg/summary/hu")" -ne 1 ]; then
			error "$pkg/summary/hu nem egysoros!"
		fi
		if cmp -s "$pkg"/{description,summary}/hu; then
			> "$UHUBUILD_SRC2DIR"/"$pkg"/description/hu
		fi
	fi
	if [ -f $pkg/words/hu ]; then
		reformat 0 < $pkg/words/hu | \
		  LC_ALL=hu_HU.UTF-8 awk '{ print tolower($LINE) }' | \
		  LC_ALL=hu_HU.UTF-8 sort -u
	fi > $UHUBUILD_SRC2DIR/$pkg/words/hu

	if [ "$(type -p hunspell)" != "" ]; then
		to_utf8_filter="cat"
		from_utf8_filter="cat"
		tmpfile tmpfile tmpfile2
		cat "$UHUBUILD_SRC2DIR/$pkg"/{description,summary}/hu | \
		tr -s '[:space:]' '\n' | grep -a -v '^&' | \
		  $to_utf8_filter | LC_ALL=hu_HU.UTF-8 hunspell -l | $from_utf8_filter | \
		  LC_ALL=hu_HU.UTF-8 sort | \
		  LC_ALL=hu_HU.UTF-8 comm -23 - /usr/share/uhubuild/words/hu >"$tmpfile"
		LC_ALL=hu_HU.UTF-8 comm -23 "$UHUBUILD_SRC2DIR/$pkg/words/hu" "$tmpfile" >"$tmpfile2"
		if [ -s "$tmpfile2" ]; then
			warn "Fölösleges szavak a $pkg/words/hu fájlban:"
			cat "$tmpfile2" | $to_utf8_filter >&2
		fi
		LC_ALL=hu_HU.UTF-8 comm -13 "$UHUBUILD_SRC2DIR/$pkg/words/hu" "$tmpfile" >"$tmpfile2"
		if [ -s "$tmpfile2" ]; then
			warn "Nyomdahiba a(z) $pkg leírásában!"
			cat "$tmpfile2" | $to_utf8_filter >&2
			echo "(Ami esetleg mégsem nyomdahiba, azt jelöld meg egy eléírt & jellel!)" >&2
		fi
	fi

	#############################
	### packages/*/categories ###
	#############################
	if [ -f $pkg/categories ]; then
		pp < $pkg/categories
	fi > $UHUBUILD_SRC2DIR/$pkg/categories.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/categories.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/categories
	rm $UHUBUILD_SRC2DIR/$pkg/categories.tmp

	############################
	### packages/*/conffiles ###
	############################
	if [ -f $pkg/conffiles ]; then
		pp < $pkg/conffiles > $UHUBUILD_SRC2DIR/$pkg/conffiles.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/conffiles.tmp | leading_slash | sort -u \
		  > $UHUBUILD_SRC2DIR/$pkg/conffiles
		rm $UHUBUILD_SRC2DIR/$pkg/conffiles.tmp
	fi

	#############################
	### packages/*/conflicts ###
	#############################
	if [ -f $pkg/conflicts ]; then
		pp < $pkg/conflicts
	fi > $UHUBUILD_SRC2DIR/$pkg/conflicts.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/conflicts.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/conflicts
	rm $UHUBUILD_SRC2DIR/$pkg/conflicts.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/conflicts fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/conflicts

	##########################
	### packages/*/depends ###
	##########################
	if [ -f $pkg/depends ]; then
		pp < $pkg/depends
	fi > $UHUBUILD_SRC2DIR/$pkg/depends.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/depends.tmp | sort -u \
	  > $UHUBUILD_SRC2DIR/$pkg/depends
	rm $UHUBUILD_SRC2DIR/$pkg/depends.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/depends fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/depends

	#######################
	### packages/*/dirs ###
	#######################
	if [ -f $pkg/dirs ]; then
		pp < $pkg/dirs
	fi > $UHUBUILD_SRC2DIR/$pkg/dirs.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/dirs.tmp | leading_slash | no_trailing_slash | sort -u \
	  > $UHUBUILD_SRC2DIR/$pkg/dirs
	rm $UHUBUILD_SRC2DIR/$pkg/dirs.tmp

	########################
	### packages/*/files ###
	########################
	if [ ! -f $pkg/files ]; then
		nofiles=$((nofiles+1))
	fi
	if [ -f $pkg/files ]; then
		pp < $pkg/files > $UHUBUILD_SRC2DIR/$pkg/files.tmp
		# Jajj, gány... :(
		grep '' $UHUBUILD_SRC2DIR/$pkg/files.tmp | while read a b; do
			if [ "$b" = "" ]; then
				echo "$a"
			else
				echo "$b" "$a"
			fi
		done > "$UHUBUILD_SRC2DIR/$pkg/files.tmp2"
		leading_slash < "$UHUBUILD_SRC2DIR/$pkg/files.tmp2" | \
		reformat 0 | sort -u | \
		sed -e 's|/\+|/|g' | sed -e 's|/\(\*\*/\)\+|/**/|g' \
		  > "$UHUBUILD_SRC2DIR/$pkg/files.tmp"
		while read a b; do
			echo "$b	$a"
		done < "$UHUBUILD_SRC2DIR/$pkg/files.tmp" > "$UHUBUILD_SRC2DIR/$pkg/files"
		rm "$UHUBUILD_SRC2DIR/$pkg/files.tmp" "$UHUBUILD_SRC2DIR/$pkg/files.tmp2"
	fi

	###########################
	### packages/*/excludes ###
	###########################
	if [ -f $pkg/excludes ]; then
		pp < $pkg/excludes > $UHUBUILD_SRC2DIR/$pkg/excludes.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/excludes.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/excludes
		rm $UHUBUILD_SRC2DIR/$pkg/excludes.tmp
	fi

	##########################
	### packages/*/libpath ###
	##########################
	if [ -f $pkg/libpath ]; then
		pp < $pkg/libpath
	fi > $UHUBUILD_SRC2DIR/$pkg/libpath.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/libpath.tmp | leading_slash | sort -u \
	  > $UHUBUILD_SRC2DIR/$pkg/libpath
	rm $UHUBUILD_SRC2DIR/$pkg/libpath.tmp
	if grep ':' $UHUBUILD_SRC2DIR/$pkg/libpath; then
		error "A $pkg/libpath file nem tartalmazhat kettőspontot!"
	fi

	#############################
	### packages/*/obsoletes ###
	#############################
	if [ -f $pkg/obsoletes ]; then
		pp < $pkg/obsoletes
	fi > $UHUBUILD_SRC2DIR/$pkg/obsoletes.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/obsoletes.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/obsoletes
	rm $UHUBUILD_SRC2DIR/$pkg/obsoletes.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/obsoletes fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/obsoletes

	##########################
	### packages/*/options ###
	##########################
	if [ -f $pkg/options ]; then
		pp < $pkg/options
	fi > $UHUBUILD_SRC2DIR/$pkg/options.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/options.tmp | tr A-Z a-z | sort -u \
	  > $UHUBUILD_SRC2DIR/$pkg/options
	rm $UHUBUILD_SRC2DIR/$pkg/options.tmp
	while IFS='' read option; do
		case "$option" in
		  skip-fhs|skip-autodepend)			;;
		  skip-postinst|skip-prerm|skip-postrm)		;;
		  *)
			error "Hibás érték a $pkg/options fájlban: $option!"
			;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/options

	########################
	### packages/*/perms ###
	########################
	if [ -f $pkg/perms ]; then
		pp < $pkg/perms
	fi > $UHUBUILD_SRC2DIR/$pkg/perms.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/perms.tmp | no_trailing_slash > $UHUBUILD_SRC2DIR/$pkg/perms.tmp2
	while read -r owner group mode file rest; do
		if [ "$owner" = "" ]; then
			group="-"
		fi
		if [ "$owner" = "" ]; then
			group="-"
		fi
		case "$mode" in
			[0-7][0-7][0-7])
				mode=" $mode";;
			0[0-7][0-7][0-7])
				mode=" ${mode:1:3}";;
			""|-)
				mode="   -";;
		esac
		if [ "${file:0:1}" != "/" ]; then
			file="/$file"
		fi
		printf "%-8s  %-8s  %s  %s\n" "$owner" "$group" "$mode" "$file"
	done < $UHUBUILD_SRC2DIR/$pkg/perms.tmp2 > $UHUBUILD_SRC2DIR/$pkg/perms.tmp
	sort -u < $UHUBUILD_SRC2DIR/$pkg/perms.tmp > $UHUBUILD_SRC2DIR/$pkg/perms
	rm $UHUBUILD_SRC2DIR/$pkg/perms.tmp{,2}

	##########################################
	### packages/*/{postinst,postrm,prerm} ###
	##########################################
	for file in postinst postrm prerm; do
		if [ -f $pkg/$file ]; then
			pp < $pkg/$file > $UHUBUILD_SRC2DIR/$pkg/$file
			reformat 1 < $pkg/$file > $UHUBUILD_SRC2DIR/$pkg/$file
		fi
	done

	###########################
	### packages/*/priority ###
	###########################
	if [ -f $pkg/priority ]; then
		pp < $pkg/priority > $UHUBUILD_SRC2DIR/$pkg/priority.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/priority.tmp > $UHUBUILD_SRC2DIR/$pkg/priority
		rm $UHUBUILD_SRC2DIR/$pkg/priority.tmp
		if [ $(wc -l < $UHUBUILD_SRC2DIR/$pkg/priority) -ne 1 ]; then
			error "Hibás $pkg/priority fájl! (nem egysoros)"
		else
			case $(cat $UHUBUILD_SRC2DIR/$pkg/priority) in
				required|important|standard|optional|extra)
					;;
				*)
					error "Hibás érték a $pkg/priority fájlban!"
					;;
			esac
		fi
	else
		echo "optional" >"$UHUBUILD_SRC2DIR"/"$pkg"/priority
	fi

	##############################
	### packages/*/pre-depends ###
	##############################
	if [ -f $pkg/pre-depends ]; then
		pp < $pkg/pre-depends
	fi > $UHUBUILD_SRC2DIR/$pkg/pre-depends.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/pre-depends.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/pre-depends
	rm $UHUBUILD_SRC2DIR/$pkg/pre-depends.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/pre-depends fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/pre-depends

	#############################
	### packages/*/provides ###
	#############################
	if [ -f $pkg/provides ]; then
		pp < $pkg/provides
	fi > $UHUBUILD_SRC2DIR/$pkg/provides.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/provides.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/provides
	rm $UHUBUILD_SRC2DIR/$pkg/provides.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/provides fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/provides

	#############################
	### packages/*/replaces ###
	#############################
	if [ -f $pkg/replaces ]; then
		pp < $pkg/replaces
	fi > $UHUBUILD_SRC2DIR/$pkg/replaces.tmp
	reformat 0 < $UHUBUILD_SRC2DIR/$pkg/replaces.tmp | sort -u > $UHUBUILD_SRC2DIR/$pkg/replaces
	rm $UHUBUILD_SRC2DIR/$pkg/replaces.tmp
	while read package op version; do
		case "${op#(}" in
			""|"<"|"<<"|"<="|"="|">="|">>"|">")
				;;
			*)
				error "Ismeretlen verzió-operátor ($op) a $pkg/replaces fájlban."
				;;
		esac
	done < $UHUBUILD_SRC2DIR/$pkg/replaces

	##########################
	### packages/*/section ###
	##########################
	if check $pkg/section; then
		pp < $pkg/section > $UHUBUILD_SRC2DIR/$pkg/section.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/section.tmp > $UHUBUILD_SRC2DIR/$pkg/section
		rm $UHUBUILD_SRC2DIR/$pkg/section.tmp
		if [ $(wc -l < $UHUBUILD_SRC2DIR/$pkg/section) -ne 1 ]; then
			error "Hibás $pkg/section fájl!"
		else
			sect="$(< $UHUBUILD_SRC2DIR/$pkg/section)"
			case $sect in \
				Applications | \
				Archiving | \
				Artwork | \
				Base | \
				Boot | \
				Compilers | \
				Configuration | \
				Daemons | \
				Data | \
				Database | \
				Development | \
				Documentation | \
				Editors | \
				Edutainment | \
				Emulators | \
				FileManagers | \
				FileSystems | \
				Firmware | \
				Fonts | \
				Games | \
				Graphics | \
				Headers | \
				Internet | \
				Interpreters | \
				Libraries | \
				Meta | \
				Multimedia | \
				Networking | \
				Office | \
				Perl | \
				Printing | \
				Python | \
				Scientific | \
				Servers | \
				Shells | \
				SoftwareManagement | \
				StaticLibraries | \
				System | \
				Tools | \
				WindowManagers | \
				X | \
				UHU | \
				ENLIGHTENMENT | \
				GNOME | \
				KDE | \
				LXQT | \
				MATE | \
				XFCE)
					;;
				*)
					error "Hibás vagy régi érték a $pkg/section fájlban: $sect"
					;;
			esac
		fi
	fi

	###############################
	### packages/*/suid_wrapper ###
	###############################
	if [ -f $pkg/suid_wrapper ]; then
		pp < $pkg/suid_wrapper > $UHUBUILD_SRC2DIR/$pkg/suid_wrapper.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/suid_wrapper.tmp | leading_slash | sort -u \
		  > $UHUBUILD_SRC2DIR/$pkg/suid_wrapper
		rm $UHUBUILD_SRC2DIR/$pkg/suid_wrapper.tmp
		while read line; do
			case "$line" in
				/sbin/* | /usr/sbin/*)
					;;
				*)
					error "Hibás fájlnév a $pkg/suid_wrapper fájlban!"
					;;
			esac
		done < $UHUBUILD_SRC2DIR/$pkg/suid_wrapper
	fi

	#################################
	### packages/*/{users,groups} ###
	#################################
	if [ -f $pkg/users ]; then
		pp < $pkg/users > $UHUBUILD_SRC2DIR/$pkg/users.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/users.tmp > $UHUBUILD_SRC2DIR/$pkg/users.tmp2
		while read -r name uid gid rest; do
			if [ "$name" = "" -o "$uid" = "" -o "$gid" = "" ]; then
				error "Hiányos $pkg/users fájl!"
			fi
			if [ "$uid" -ge "1000" -o "$gid" -ge "1000" ]; then
				error "$pkg/users: csak rendszer felhasználó / csoport adható meg!"
			fi
			printf "%-8s %4s %4s  %s\n" "$name" "$uid" "$gid" "$rest"
		done < $UHUBUILD_SRC2DIR/$pkg/users.tmp2 > $UHUBUILD_SRC2DIR/$pkg/users.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/users.tmp > $UHUBUILD_SRC2DIR/$pkg/users
		rm $UHUBUILD_SRC2DIR/$pkg/users.tmp{,2}
	fi
	if [ -f $pkg/groups ]; then
		pp < $pkg/groups > $UHUBUILD_SRC2DIR/$pkg/groups.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/groups.tmp > $UHUBUILD_SRC2DIR/$pkg/groups.tmp2
		while read -r name gid rest; do
			if [ "$name" = "" -o "$gid" = "" ]; then
				error "Hiányos $pkg/groups fájl!"
			fi
			if [ "$gid" -ge "1000" ]; then
				error "$pkg/groups: csak rendszer csoport adható meg!"
			fi
			printf "%-8s %4s  %s\n" "$name" "$gid" "$rest"
		done < $UHUBUILD_SRC2DIR/$pkg/groups.tmp2 > $UHUBUILD_SRC2DIR/$pkg/groups.tmp
		reformat 0 < $UHUBUILD_SRC2DIR/$pkg/groups.tmp > $UHUBUILD_SRC2DIR/$pkg/groups
		rm $UHUBUILD_SRC2DIR/$pkg/groups.tmp{,2}
	fi
done

if [ $nofiles -gt 1 ]; then
	error "Több mint 1 csomag van files fájl nélkül!"
fi

########################
### run uhubuild-u2d ###
########################
yellowecho "uhubuild-u2d futtatása..."

if [ -f "u2d" -a "$copy" != "yes" ]; then
	if ! grep 'noupdate' u2d; then
		newver="$(uhubuild-u2d)"
		current="$(cat version)"
			if [ -n "$newver" -a "$newver" != "$current" ]; then
				warn "new version available: $newver (current: $current)"
			fi
	fi
fi

#
# Készen vagyunk a másik könyvtárba kipofozott módon átmásolással.
#
# Ha "--copy" argumentum meg volt adva, akkor valószínűleg egy következő
# szkript ezt a könyvtárat használni fogja, ezért azt segítjük azzal, hogy
# adunk x bitet a szkriptekre, valamint csinálunk szimlinket a sources
# könyvtárra. Relatív szimlinket készítünk, mert jó fejek vagyunk,
# és akkor ez chrooton kívül/belül egyaránt jó. Szépen néz ki mc-ben :-)
#
# Ha nincsen "--copy" argumentum, akkor ez a könyvtár úgyis mindjárt
# törlődik, tehát x bitnek és szimlinkezésnek nem lenne értelme. Ekkor az a
# feladatunk, hogy visszamásoljuk a cuccokat.
#

cd "$UHUBUILD_SRC2DIR"

if [ "$copy" == "yes" ]; then
	for i in acquire compile install; do
		if [ -f "$i" ]; then
			chmod +x "$i"
		fi
	done
	p="$(pwd -P)"
	if [ "$p" == "/" -o "$p" == "//" ]; then
		p=""
	fi
	p="${p//[^\/]/}"
	p="${p//\//../}"
	if [ -d $UHUBUILD_SRCDIR/sources ]; then
		ln -s "$p${UHUBUILD_SRCDIR#/}/sources" .
	fi
else
	for file in $(find . -type f); do
		if [ -f "$UHUBUILD_SRCDIR/$file" ]; then
			if ! cmp -s "$file" "$UHUBUILD_SRCDIR/$file"; then
				cp -a "$file" "$UHUBUILD_SRCDIR/$file"
			fi
		fi
	done

	find "$UHUBUILD_SRCDIR" -type d -print0 | xargs -r0 chmod a+rx,u+w
	find "$UHUBUILD_SRCDIR" -type f -print0 | xargs -r0 chmod a+r,u+w,a-x

	# Kis rendrakás van még hátra
	cd "$UHUBUILD_SRCDIR"
	shopt -s nullglob

	# optional priority törlése
	for p in packages/*/priority; do
		if [ "$(<$UHUBUILD_SRC2DIR/$p)" == "optional" ]; then
			rm -f "$p"
		fi
	done

	# Esetleges fölösleges (0 méretű) fájlok törlése
	for f in acquire build-depends compile doc install options \
	  packages/*/{{description,words}/hu,depends,perms,conffiles,categories,options,suid_wrapper,users,groups \
					conflicts,obsoletes,pre-depends,provides,replaces}; do
		if [ -f "$f" -a ! -s "$f" ]; then
			rm -f "$f"
		fi
	done

	# Esetleges fölösleges könyvtárak törlése
	for d in addons packages/*/{description,words} patches; do
		if [ -d "$d" ]; then
			rmdir --ignore-fail-on-non-empty "$d"
		fi
	done
fi
