#!/bin/bash
# 2005-2006, 2009, 2014, 2015, 2016, 2017, 2020 (c) Etersoft www.etersoft.ru
# 2005-2016 Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
# GS - get source
#
# Скачивает исходники, автоматически выправляя ситуацию с gz/bz2/tgz/zip (tar для git, tar.bz2 для src.rpm)
# Параметры:
# - название спек-файла
# -a - get all source
# check for the same file with other compression

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common
load_mod rpm tarball web buildsrpm

WEXT=""
GETSOURCE=""
LOADALL=''

#############################
Usage="Usage: $name [-f] [spec] [new_version]"
function mygetopts()
{
name=${0##*/}
Descr="$name (Get Source) - get sources by spec / repository"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "You can run 'rpmgs 1.2' for set new version 1.2 and download it"
	echog "Use HEAD instead version for get latest git commit"
	echo
	echog "Options:"
#	echog "   -a  get all source (not only Source|Source0)"
	echog "   -f  force download and commit tarball(s) (remove source file before download)"
}

while getopts :hf opt; do
    case $opt in
    h) phelp; exit 0;;
#    a) LOADALL=1 ;;
    f) FORCEDOWNLOAD=-f ;;
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
    esac
done
 
# remove args that were options
[ "$OPTIND" -gt 0 ] && shift $(($OPTIND - 1))

LISTRPMARGS=$@

}

# Extract Source tarball from src.rpm file
# Args: SRCRPM TARGET
extract_from_srcrpm()
{
	local SRCRPM="$(realpath "$1")"
	local TARGET="$2"
	local TMPDIR
	TMPDIR=$(mktemp -d) || fatal "Can't create temp dir"
	info "Extracting Source tarball from $(basename "$SRCRPM") ..."
	docmd $ERCCMD -C "$TMPDIR" x "$SRCRPM" || { rm -rf "$TMPDIR" ; fatal "Can't extract src.rpm" ; }
	# find the source tarball inside extracted directory
	local TARBALL
	TARBALL=$(find "$TMPDIR" -name '*.tar*' -o -name '*.tgz' -o -name '*.zip' -o -name '*.7z' | head -1)
	if [ -z "$TARBALL" ] ; then
		rm -rf "$TMPDIR"
		fatal "No source tarball found in src.rpm"
	fi
	info "Found source tarball: $(basename "$TARBALL")"
	repack_tarball "$TARBALL" "$TARGET"
	local RET=$?
	rm -rf "$TMPDIR"
	rm -f "$SRCRPM"
	return $RET
}

repack_tarball()
{
	# TODO: move it into repack
	[ "$(realpath "$1")" = "$(realpath "$2")" ] && return
	# skip repack for the same ext
	if [ "$(get_ext "$1")" = "$(get_ext "$2")" ] ; then
		echog "The same ext $(get_ext "$1"), skip repack"
		mv -f "$1" "$2"
		return
	fi
	docmd $ERCCMD -f repack "$1" "$2" || return
	# remove original
	rm -f "$1"
}

check_tarball()
{
	[ -s "$1" ] || return
	$ERCCMD -q check "$1"
}

# Args: URL TARGET
git_to_tarball()
{
	local URL="$1"
	local TARGET="$2"
	local CHECKOUT=''

	info "Create tarball $(basename $TARGET) from $URL ..."

	# allow commit or version
	if echo "$URL" | grep -q "github\.com.*/tree/" ; then
		CHECKOUT="$(echo "$URL" | sed -e 's|.*/tree/||')"
		URL="$(echo "$URL" | sed -e 's|/tree/.*||')"
	fi

	# allow commit
	if echo "$URL" | grep -q "github\.com.*/commit/" ; then
		CHECKOUT="$(echo "$URL" | sed -e 's|.*/commit/||')"
		URL="$(echo "$URL" | sed -e 's|/commit/.*||')"
	fi

	local d="$(basename "$URL" .git)"
	git clone --recurse-submodules "$URL" "$d" || fatal

	if [ -n "$CHECKOUT" ] ; then
		cd "$d" || fatal
		git checkout $CHECKOUT || fatal
		cd - >/dev/null
	fi

	rm -rf "$d/.git/"
	# note: TARGET can be various
	docmd $ERCCMD pack "$TARGET" "$d" || fatal
	rm -rf "$d"
}

# Args: URL TARGET
download_to()
{
	local URL="$1"
	local TARGET="$2"
	pushd $(dirname $TARGET)
	# make tarball from remote git (with submodules)
	if echo "$URL" | grep -q "\.git$" || echo "$URL" | grep -q -E "github\.com.*/(tree|commit)/" ; then
		git_to_tarball "$URL" "$TARGET"
		local RET=$?
		popd
		return $RET
	fi
	# handle src.rpm: extract Source tarball from it
	if echo "$URL" | grep -q '\.src\.rpm$' ; then
		local DF="$(basename "$URL")"
		download_url "$URL" && DF="$DOWNLOADEDNAME" && extract_from_srcrpm "$DF" "$TARGET"
		local RET=$?
		[ "$RET" = "0" ] || rm -fv "$DF" "$TARGET"
		popd
		return $RET
	fi
		local DF="$(basename "$URL")"
		download_url "$URL" && DF="$DOWNLOADEDNAME" && repack_tarball "$DF" "$TARGET"
		local RET=$?
		# TODO: repack need remove origin
		#rm -fv "$DF"
		[ "$RET" = "0" ] || rm -fv "$DF" "$TARGET"
	popd
	return $RET
}

# Args: URL target_file
download_any_tarball()
{
	local GETSOURCE="$1"
	local TARGET="$2"
	local FORMATS="tar.xz tar.bz2 tar.gz zip tgz 7z tbz2 tbz rar tar"
	local BASESOURCE="$GETSOURCE"

	local ORIGEXT=$(get_ext "$BASESOURCE")
	[ -n "$ORIGEXT" ] || fatal "Error with $GETSOURCE. Have no idea how to load files without extension"

	# first try download with original extension (exclude for tar)
	if [ "$ORIGEXT" != "tar" ] ; then
		FORMATS="$ORIGEXT $(estrlist exclude "$ORIGEXT" "$FORMATS")"
	fi

	BASESOURCE=$(dirname "$BASESOURCE")/$(basename "$BASESOURCE" .$ORIGEXT)

	local ext
	# try download by exts list
	for ext in $FORMATS ; do
		[ -z "$FORCEDOWNLOAD" ] && check_tarball "$TARGET" && { echo "$TARGET already exists, continue... " ; continue; }
		download_to "$BASESOURCE.$ext" "$TARGET" || continue
		return
	done
	fatal "Cannot retrieve $GETSOURCE"
}

__replace_first_line()
{
	# https://unix.stackexchange.com/questions/250603/replace-only-on-the-first-matching-line-with-sed
	sed -e "1,/$1/s/$1/$2/"
}

# param: spec name number (f.i., url for Source-url)
function source_ext()
{
	local GETSOURCEEXT=
	# %define SourceUrl ftp://updates.etersoft.ru/pub/Etersoft/WINE@Etersoft/last/sources/tarball/%name-%version.tar.gz
	#GETSOURCEURL=$(eval_spec $1 | grep -i "^%define ${2}Url${3} " | head -n 1 | sed -e "s/ *\$//g" | sed -e "s/^%define[ \t].*[ \t]//g")
	local SN="$3"
	#[ "$SN" = "0" ] && SN="$SN\?"
	if grep -q "^# $SN-$2:" "$1" ; then
		local TMPSPEC=$1.tmpurl
		local NEWSOURCE=$(grep --text "^# $SN-$2:" "$1" | sed -e "s/.*$SN-$2:[ \t]*//g" | tail -n1)
		test -n "$NEWSOURCE" || fatal "Can't extract URL from $SN-$2"
		# Fake replace for correct subst variables
		NEWSOURCE="$(echo "$NEWSOURCE" | sed -e 's|\&|\\&|g')"
		# TODO: move to separate function and rewrite
		# TODO: use special field before %build
		cat $1 | __replace_first_line "^Summary:.*" "" | sed -e "s|^\(# $SN-$2:.*\)|Summary: $NEWSOURCE\n\1|" > $TMPSPEC
		# TODO: replace only first entry
		#cat $1 | sed -e "s|^Summary:.*|Summary: $NEWSOURCE|1" > $TMPSPEC
		GETSOURCEEXT=$(eval_spec "$TMPSPEC" | get_var "Summary")
		#rhas "$GETSOURCEEXT" "%" && fatal "some macro unexpanded in URL. Check $TMPSPEC"
		rm -f "$TMPSPEC"
	fi
	
	echo "$GETSOURCEEXT"
	test -n "$GETSOURCEEXT"
}

# Get real URL from comment Source-xxx

# Source-svn: http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/Collection/
function get_source_svn()
{
	GETSOURCESVN=$(source_ext "$1" svn "$2")
}


# Source-git: http://git.altlinux.org/people/lav/packages/rpm-build-fonts.git
function get_source_git()
{
	local SN="$2"

	GETSOURCEGIT=$(source_ext "$1" git "$2")
	[ -n "$GETSOURCEGIT" ] && return

	# hack for git url under Source
	local HGIT="$(grep -B1 "^$SN:" "$1" | head -n1 | grep "^# " | sed -e "s|#[[:space:]]*||" -e "s|[[:space:]]*$||")"
	if echo "$HGIT" | grep -q -E "(git|https|http)://.*\.git$" || echo "$HGIT" | grep -q "https://github.com/.*" ; then
		GETSOURCEGIT="$(echo "$HGIT" | sed -e "s|.*[[:space:]]\(.*://\)|\1|")" #"
	else
		return 1
	fi
}

# VCS: http://git.altlinux.org/people/lav/packages/rpm-build-fonts.git
function get_source_vcs()
{
	local SPEC="$1"
	local SN="$2"

	# only for first source
	[ "$SN" = "Source" ] || [ "$SN" = "Source0" ] || return 1

	local vcs="$(grep -i "^VCS:" $SPEC | head -n1 | sed -e 's|VCS:[[:space:]]*||I')"
	if [ -z "$vcs" ] ; then
		# try hack with Url:
		local vcs="$(grep -i "^Url:" $SPEC | head -n1 | sed -e 's|Url:[[:space:]]*||I')"
		[ -z "$vcs" ] && return 1
		GETSOURCEGIT="$vcs"
		echo "$vcs" | grep -q "\.git$" || GETSOURCEGIT="$vcs.git"
	fi
	GETSOURCEGIT="$vcs"
}

# Source-url: ftp://updates.etersoft.ru/pub/Etersoft/WINE@Etersoft/last/sources/tarball/%name-%version.tar.gz
function get_source_url()
{
	GETSOURCEURL=$(source_ext "$1" url "$2")
	#rhas "$GETSOURCEURL" "%" && fatal "some macro unexpanded in URL. Check $TMPSPEC"
}

# Source-script: .gear/update.sh
function get_source_script()
{
	GETSOURCESCRIPT=$(source_ext "$1" script "$2")
}

function print_error()
{
	echog "Can't find any spec file. It is possible you run this script not in git dir."
	echog "If you use old style build, run rpmgs with spec name as arg."
	echog "You can also use src.rpm URL in Source-url."
	exit 1
}

# tarball dirname [options]
gear_update_from_tarball()
{
	local CREATEFLAG=-f
	local TARBALL="$1"
	local CURNAME="$2"
	shift 2
	assert_var TARBALL CURNAME
	[ -d "$CURNAME" ] || CREATEFLAG=-c
	# TODO: check tarball ext. for unsupported arch and realize it here or in gear-update
	echo "Commit tarball '$TARBALL' to git subdir '$CURNAME'..."
	if ! docmd gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" ; then
		if a= gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" 2>&1 | grep -q "More than one subdirectory specified" ; then
			info "Try unpack as is"
			CREATEFLAG="$CREATEFLAG -a"
			docmd gear-update $CREATEFLAG $@ "$TARBALL" "$CURNAME" && return
		fi
	else
		return 0
	fi
	fatal "can't import tarball '$TARBALL'"
}

# if we have named rule
is_predownloaded_rule()
{
	cat $(get_root_git_dir)/.gear/rules | grep -q "^tar:.*\.gear/$1"
	#fatal "missed tar:.gear/gear-sources in $(get_root_git_dir)/.gear/rules"
}


# Args: tarball dir
# Uses: RPMSOURCEDIR FORCEDOWNLOAD
commit_tarball_to_dir()
{
	local TARBALL="$1"
	local EXTTARBALL="$2"
	local CURNAME="$3"

	if [ "$EXTTARBALL" = "copy" ] ; then
		#local OLDFILE=$(echo "$CURNAME")
		cp -v "$TARBALL" "$CURNAME"
		docmd git add "$CURNAME"
		if [ -n "$CURRENTURL" ] ; then
			docmd git commit -m "just import $CURRENTURL to $(basename $CURNAME) subdir with rpmgs script"
		else
			docmd git commit -m "just import $(basename $CURNAME) with rpmgs script"
		fi
		return 0
	fi

	if [ -f "$TARBALL" ] ; then
		gear_update_from_tarball "$TARBALL" "$CURNAME" $FORCEDOWNLOAD || { warning "Error with update tarball in repo" ; return 1 ; }
		# force commit ever files from .gitignore
		docmd git add -f "$CURNAME"
		if [ -s ./.gear/postdownload-hook ] ; then
			info "Detected ./.gear/postdownload-hook, running it in $CURNAME dir ..."
			cd $CURNAME || fatal
			sh -x ../.gear/postdownload-hook
			cd - >/dev/null
		fi
		local commit_msg
		if [ -n "$CURRENTURL" ] ; then
			commit_msg="just import $CURRENTURL to $(basename $CURNAME) subdir with rpmgs script"
		else
			commit_msg="just import $(basename $TARBALL) with rpmgs script"
		fi
		if ! docmd git commit -m "$commit_msg" ; then
			rm -fv "$TARBALL"
			fatal "Sources were not updated (nothing to commit). Check Source-url and version."
		fi
		rm -fv "$TARBALL"
	else
		warning "Skip missed $TARBALL tarball committing"
		return 1
	fi
	return 0
}

# TODO: rewrite for any tarball commit
# Arg: tarball
commit_tarball()
{
	local TARBALL="$1"
	local EXTTARBALL="$(get_ext $TARBALL)"
	local CURNAME
	CURNAME=$(get_tardir_from_rules "$EXTTARBALL" $(basename "$TARBALL"))
	if [ -z "$CURNAME" ] ; then
		#info "Can't get dir (no $EXTTARBALL: line in rules file), just commit $(basename "$TARBALL") file"
		CURNAME=$(get_tardir_from_rules "copy" $(basename "$TARBALL"))
		[ -n "$CURNAME" ] || fatal "There is no correct '$EXTTARBALL:' line nor 'copy:' in gear rules file for $(basename "$TARBALL"), needed for commit tarball"
		EXTTARBALL="copy"
	fi
	echo "$CURNAME" | grep -q "[\*\?]" && fatal "$CURNAME for $TARBALL got incorrectly"
	# FIXME:
	# use real path for download
	#is_gear_sources && CURNAME=
	#is_gear_sources && CURNAME=$(get_root_git_dir)/$BASENAME
	#fatal "FIXME: fail with is_gear_sources in commit_tarball"

	# hack: try detect dir for unpacking
	#test -d "$CURNAME" || CURNAME=$(get_root_git_dir)/$(get_tarballname "$spec")
	#test -d "$CURNAME" || CURNAME=$(get_root_git_dir)/$BASENAME

	# save first committed source dir
	[ -z "$FIRSTSOURCEDIR" ] && FIRSTSOURCEDIR="$(basename "$CURNAME")"

	commit_tarball_to_dir "$TARBALL" "$EXTTARBALL" "$CURNAME"
}

# check for tag for GSSETVERSION with some heuristics (see gear-remotes-uscan also)
get_tag_by_version()
{
	local GSSETVERSION="$1"
	local rc
	rc=1

	if [ "$GSSETVERSION" = "HEAD" ] ; then
		echo "upstream/master"
		return 0
	fi

	local i
	local encoded="$(version_to_tag "$GSSETVERSION")"
	alternate_tag1="$(echo "v$GSSETVERSION" | sed -e "s|\.|-|g")"
	alternate_tag2="$(echo "REL_$GSSETVERSION" | sed -e "s|\.|_|g")"
	alternate_tag3="$(echo "version_$GSSETVERSION")"
	for i in v$GSSETVERSION v$encoded $alternate_tag1 $GSSETVERSION $encoded $alternate_tag2 $alternate_tag3 ; do
		git rev-parse $i >/dev/null 2>/dev/null && rc=0 && break
	done

	if [ "$rc" = "1" ] ; then
		i="$(git tag | grep "$GSSETVERSION\$" | head -n1)"
		[ -n "$i" ] && git rev-parse $i >/dev/null 2>/dev/null && rc=0
	fi
	echo "$i"
	return $rc
}

# TODO: try gear-remotes-uscan here
# merge from guessed tag (by version) or from HEAD
update_master_branch_to()
{
	local GSSETVERSION="$1"
	if [ -z "$GSSETVERSION" ] ; then
		warning "Empty new version variable"
		return
	fi

	local tag="$(get_tag_by_version "$GSSETVERSION")"
	[ -n "$tag" ] || fatal "Can't find tag for $GSSETVERSION version"
	if [ -d "$(get_root_git_dir)/.gear/tags" ] ; then
		info "Using 'ours' merge strategy for .gear/tags-based package"
		docmd git merge -s ours $tag || fatal "Failed to merge tag $tag (ours strategy)"
	else
		docmd git merge $tag || fatal "Failed to merge tag $tag. Try 'git merge --allow-unrelated-histories $tag' manually."
	fi

	# TODO: it is more clean detect that dir
	if [ -d "$(get_root_git_dir)/.gear/tags" ] ; then
		docmd gear-update-tag -a
		cd $(get_root_git_dir)/.gear || fatal
		docmd git add tags/* -f
		docmd git commit -m "update .gear/tags"
		cd - >/dev/null
	fi
}

# update .gear/@name@-postsubmodules if needed
# uses: BASENAME VERSION
update_post_git_submodules()
{
	# TODO: check for rules
	local PSM=$(get_root_git_dir)/.gear/$BASENAME-postsubmodules
	[ -d $PSM ] || return

	info "Detected post git submodules hook"

	cat $(get_root_git_dir)/.gear/rules | grep -q "tar:.*-postsubmodules" || fatal "missed tar:.gear/$BASENAME-postsubmodules in $(get_root_git_dir)/.gear/rules"
	[ -s $(get_root_git_dir)/.gitmodules ] || fatal "there is no .gitmodules file in repo root"

	cd $(get_root_git_dir) || fatal
	docmd git submodule update --init --recursive || fatal

	rm -rfv $PSM/*
	# echo * do not catch .names really
	#cp -a $(echo * | grep -v -E "(.gear|.git)") $PSM/ || fatal #"
	#find $PSM -name ".git" -type f -delete -print
	#find $PSM -name ".gitmodules" -type f -delete -print
	local i
	for i in $(grep "path = " .gitmodules | sed -e "s|.*path = \(.*\)|\1|") ; do #"
		cp -al --parents $i $PSM/ || fatal
	done
	docmd git add -f $PSM
	# TODO: put short commit id in a description
	# git rev-parse --short HEAD
	docmd git commit $PSM -m "update source prepared with submodules for version $VERSION"
}

source_postupdate_hook()
{
	local RGD=$(get_root_git_dir)
	if [ -s $RGD/.gear/source-postupdate-hook ] ; then
    		info "Detected .gear/source-postupdate-hook, running it ..."
		sh $RGD/.gear/source-postupdate-hook $VERSION
	fi
}

# FIXME: run only for predownload, not for every source
# param: development, production
update_predownloaded()
{
	local MODE="$1"
	local SDNAME=predownloaded-$MODE
	is_predownloaded_rule $SDNAME || return
	PSM=$(get_root_git_dir)/.gear/$SDNAME

	local RGD=$(get_root_git_dir)

	CURNAME="$FIRSTSOURCEDIR"
	# hack for subdir??
	#CURNAME=$BASENAME

	info "Preparing sources in $PSM for mode $MODE ..."
	# TODO: add support for git
	rm -rf $PSM
	if [ -n "$FIRSTSOURCEGIT" ] || [ -n "$GETSOURCEUUPDATE" ] ; then
		# cp -a exclude .dir
		mkdir -p $PSM || fatal
		# For Source-git packages with separate packaging branch,
		# source files may not be in working tree. Extract from tag.
		local tag
		if [ -n "$FIRSTSOURCEGIT" ] ; then
			tag=$(get_tag_by_version "$VERSION" 2>/dev/null)
		fi
		if [ -n "$tag" ] && git -C "$RGD" rev-parse "$tag" >/dev/null 2>/dev/null ; then
			info "Extracting source from tag $tag for vendoring..."
			git -C "$RGD" archive "$tag" | tar -x -C $PSM || fatal
		else
			cp -a $RGD/* $PSM || fatal
		fi
	else
		[ -n "$CURNAME" ] || fatal "Main source is missed. Check Source: field."
		cp -a $RGD/$CURNAME $PSM || fatal
	fi

	pushd $PSM || fatal

	local RUNHOOK="sh"
	[ -n "$VERBOSE" ] && RUNHOOK="sh -x"

	# Track vendored directories for final cleanup
	local KEEP_DIRS=""
	local HAS_CARGO=""

	if [ -s $RGD/.gear/predownloaded-preinstall-hook ] ; then
		echo
		info "Detected .gear/predownloaded-preinstall-hook, running it ..."
		$RUNHOOK $RGD/.gear/predownloaded-preinstall-hook $MODE $VERSION
                COMMITMSG="update predownloaded-$MODE with a hook script"
	fi


	#### composer only part
	if [ -s "./composer.json" ] ; then
		local COMMITMSG=''
                local PRODUCTION=''

		[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

		info "Detected composer install hook, running ..."
		docmd composer install $PRODUCTION || fatal
		COMMITMSG="update php modules with composer install $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		KEEP_DIRS="$KEEP_DIRS vendor"
	fi
	#### end of composer only part


	#### go only part
	if [ -s "./go.mod" ] && [ ! -d "./vendor" ] ; then
		local COMMITMSG=''
		local PRODUCTION=''

		#[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

		info "Detected go.mod install hook, running ..."
		docmd go mod vendor $PRODUCTION || fatal
		COMMITMSG="update vendored go modules with go mod vendor $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		KEEP_DIRS="$KEEP_DIRS vendor"
	fi
	#### end of go only part


	#### cargo only part
	if [ -s "./Cargo.lock" ] && [ ! -d "./vendor" ] ; then
		local COMMITMSG=''
		local PRODUCTION=''

		#[ "$MODE" = "production" ] && PRODUCTION='--no-dev'

		info "Detected 'Cargo.lock' install hook ..."
		docmd cargo vendor --verbose $PRODUCTION || fatal
		COMMITMSG="update vendored cargo modules with cargo vendor $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		# TODO: can't just remove binaries (it is checked)
		info "Check news about windows target: https://github.com/rust-lang/cargo/issues/6179"
		#find vendor -name "*.a" -type f -delete -print
		#find vendor -name "*.so" -type f -delete -print
		#find vendor -name "*.dll" -type f -delete -print

		# can remove binaries from definitely unused packages
		rm -rv vendor/winapi-*-pc-windows-gnu/lib/{*.lib,*.a}
		rm -rv vendor/windows*/lib/{*.lib,*.a}

		HAS_CARGO=1
		KEEP_DIRS="$KEEP_DIRS vendor Cargo.lock"
	fi

	local i
	local rust_dir=''
	# search Cargo.lock at any depth (e.g. bindings/python/Cargo.lock for HuggingFace projects)
	for i in $(find . -name Cargo.lock -not -path "./vendor/*" 2>/dev/null | sed 's|^\./||') ; do
		[ -s "$i" ] || continue
		rust_dir=$(dirname $i)
		[ "$rust_dir" = "." ] && continue
		[ -d "./vendor" ] && continue

		local COMMITMSG=''
		# compute relative path from rust_dir back to current dir for vendor placement
		local depth=$(echo "$rust_dir" | tr '/' '\n' | wc -l)
		local vendor_rel=$(printf '../%.0s' $(seq 1 $depth))vendor

		info "Detected '$i' install hook ..."
		cd $rust_dir || fatal
		docmd cargo vendor --verbose $vendor_rel || fatal
		COMMITMSG="update vendored cargo modules in $rust_dir dir with cargo vendor for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		cd - >/dev/null

		# can remove binaries from definitely unused packages
		rm -rv vendor/winapi-*-pc-windows-gnu/lib/*.a

		HAS_CARGO=1
		KEEP_DIRS="$KEEP_DIRS vendor Cargo.lock"
	done
	#### end of cargo only part


	#### dotnet only part
	local sln_file
	sln_file=$(find . -maxdepth 2 -name "*.sln" -type f 2>/dev/null | head -1)
	if [ -n "$sln_file" ] && [ ! -d "./vendor" ] ; then
		local COMMITMSG=''

		# Remove upstream SDK version pinning to use system dotnet
		rm -f global.json

		info "Detected '$sln_file' dotnet solution, running dotnet restore ..."
		export DOTNET_CLI_TELEMETRY_OPTOUT=true
		export DOTNET_NOLOGO=1
		docmd dotnet restore "$sln_file" \
			--use-current-runtime \
			--packages vendor || fatal
		COMMITMSG="update vendored NuGet packages with dotnet restore for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		KEEP_DIRS="$KEEP_DIRS vendor"
	fi
	#### end of dotnet only part


	#### yarn only part
	if [ -s "./yarn.lock" ] ; then
		local COMMITMSG=''
		local PRODUCTION=''

		[ "$MODE" = "production" ] && PRODUCTION='--production'

		info "Detected yarn.lock install hook, running ..."
		docmd yarn install --frozen-lockfile --ignore-scripts $PRODUCTION || fatal
		COMMITMSG="update node_modules with yarn install $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		# remove build related modules we have in system
		rm -rfv node_modules/{npm,node-gyp}/ node_modules/.bin/{npm,npx,node-gyp}

		KEEP_DIRS="$KEEP_DIRS node_modules"

	#### npm only part
	elif [ -s "./package.json" ] ; then

		# CHECKME: drop postinstall due run dev scripts during install --production
		# replace with fake commands?

		local COMMITMSG=''
                local PRODUCTION=''

		[ "$MODE" = "production" ] && PRODUCTION='--omit=dev'

		info "Detected npm install hook, running ..."
		docmd npm install $VERBOSE --omit=optional --ignore-scripts $PRODUCTION || fatal
		COMMITMSG="update node_modules with npm install $PRODUCTION for $VERSION (see $SDNAME in .gear/rules)"

		if [ -s $RGD/.gear/predownloaded-postinstall-hook ] ; then
		        info "Detected .gear/predownloaded-postinstall-hook, running it ..."
			$RUNHOOK $RGD/.gear/predownloaded-postinstall-hook $MODE $VERSION
		fi

		# prune removes modules not listed in package.json
		# a= npm prune $PRODUCTION
		# dedup restores removed modules!
		#docmd npm dedup

		# remove build related modules we have in system
		#if [ -d node_modules/node-gyp/ ] ; then
		rm -rfv node_modules/{npm,node-gyp}/ node_modules/.bin/{npm,npx,node-gyp}
		#	ln -s /usr/lib/node_modules/node-gyp node_modules/
		#fi

		#epm assure jq || fatal
		#(cd node_modules && rm -rf $(jq -r -c '.devDependencies | keys[]' ../package.json))

		KEEP_DIRS="$KEEP_DIRS node_modules"
	fi
        ### end of yarn/npm part

	# Final cleanup: remove source files, keep only vendored directories
	if [ -n "$KEEP_DIRS" ] ; then
		# Remove binaries from vendored directories (skip cargo — it checks integrity)
		if [ -z "$HAS_CARGO" ] ; then
			info "Removing binaries from vendored directories ..."
			for i in $KEEP_DIRS ; do
				[ -d "$i" ] || continue
				find "$i" -name "*.a" -type f -delete -print
				find "$i" -name "*.so" -type f -delete -print
				find "$i" -name "*.dll" -type f -delete -print
			done
		fi

		info "Cleaning up, keeping:$KEEP_DIRS ..."
		local grep_args=""
		for i in $KEEP_DIRS ; do
			grep_args="$grep_args -e ^${i}$"
		done
		rm -rf $(ls -1 | grep -v $grep_args) .[a-zA-Z0-9]*
	fi

	# some modules contains own .git
	find -type d -name ".git" -print -exec rm -rvf "{}" \;
	popd

	docmd git add -f $PSM
	docmd git commit -m "$COMMITMSG"

}


will_commit()
{
    if [ -z "$GSSETVERSION" ] && [ -z "$FORCEDOWNLOAD" ]; then
        echog "Skip $FTB committing (run without new version or without -f)"
        return 1
    fi
    return 0
}



parse_cmd_pre_spec "$@"
mygetopts $LISTARGS

test -z "$VERBOSE" || echo "'$LISTNAMES' @ '$LISTRPMARGS'"

if [ -n "$LISTRPMARGS" ] ; then
	if [ -z "${LISTRPMARGS/*spec/}" ] ; then
		fatal "run with incorrect filename $LISTRPMARGS"
	fi
	if [ ! -f "$LISTNAMES" ] ; then
		fatal "set version permitted only for one file"
	fi
	if [ "${LISTRPMARGS/ /}" != "$LISTRPMARGS" ] ; then
		fatal "you run rpmgs with more than one version"
	fi
	GSSETVERSION=$LISTRPMARGS
fi

test -z "$LISTNAMES" && print_error

[ -z "$GSSETRELEASE" ] || GSSKIPADDCHANGELOG=1

for spec in $LISTNAMES
do
	if [ -n "${spec/*spec/}" ] ; then
		print_error
	fi

	DOWNLOADSOME=''
	set_specdir $spec

# Answer me:
# for empty GSSETVERSION we do autoupdate to the latest version
# or it is a reason to retrieve the latest version from spec?

# firstly try modern autoupdate if version is empty
if is_gear $SPECDIR && [ -z "$GSSETVERSION" ] && $EPMCMD assure gear-rules-verify perl-Gear-Rules ; then
    # need for gear-rules-verify
    cd $(get_root_git_dir)

    GEAR_RULES_VERIFY=gear-rules-verify
    docmd $GEAR_RULES_VERIFY
    if $GEAR_RULES_VERIFY 2>&1 | grep -q "gear-rules-verify should be run in clean repository" ; then
        warning "gear-rules-verify should be run in clean repository, skipping"
    elif $GEAR_RULES_VERIFY | grep -q "Ready for tarball update" ; then
        #[ -n "$GSSETVERSION" ] && warning "we will ignore version and update to the latest version from watch file"
        # TODO: rpm-uscan 0.20.2.17.11 or above
        $EPMCMD assure rpm-uscan
        $EPMCMD assure gear-uupdate
        if ls $(get_root_git_dir)/.gear | grep \.watch$ || ls $(get_root_git_dir) | grep \.watch$ ; then
            docmd rpm-uscan -v --force-action gear-uupdate
            GETSOURCEUUPDATE=1

            # update_post_git_submodules
            source_postupdate_hook

            # TODO: make plugins
            update_predownloaded development
            update_predownloaded production
            DOWNLOADSOME=1
        else
            warning "there are no .watch files in $(get_root_git_dir)/.gear, skip rpm-uscan"
        fi
    elif $GEAR_RULES_VERIFY | grep -Eq "Ready for external VCS update" ; then
        #[ -n "$GSSETVERSION" ] && warning "we will ignore version and update to the latest version from watch file"
        # TODO: rpm-uscan 0.20.2.17.11 or above
        # https://www.altlinux.org/Gear/remotes
        $EPMCMD assure gear-remotes-uscan perl-Gear-Remotes
        $EPMCMD assure gear-commit gear
        docmd gear-remotes-restore
        docmd gear-remotes-uscan
        docmd gear-commit
        GETSOURCEUUPDATE=1

        source_postupdate_hook

        # TODO: make plugins
        update_predownloaded development
        update_predownloaded production
        DOWNLOADSOME=1
    else
        warning "have no version, but skipped gear-rules-verify (Check if you need run gear-remotes-save)"
    fi
fi

	# TODO: some duplication
	# secondly check for new manner use separated upstream branch
	# Note: gear-uupdate don't want work if package version is already changed
	if is_gear $SPECDIR && [ -z "$DOWNLOADSOME" ] && [ -s $(get_root_git_dir)/.gear/upstream/remotes ] && $EPMCMD assure gear-remotes-restore perl-Gear-Remotes ; then
		# need for gear-rules-verify
		cd $(get_root_git_dir)
		$EPMCMD assure gear-commit gear
		$EPMCMD assure gear-uupdate
		docmd gear-remotes-restore
		docmd git fetch --tags upstream
		# hack: if separated branch
		#if grep -q "^diff:" $(get_root_git_dir)/.gear/rules ; then
		#	update_master_branch_to "$GSSETVERSION"
		#else
		[ -n "$GSSETVERSION" ] || fatal "GSSETVERSION is empty here"
		tag="$(get_tag_by_version "$GSSETVERSION")"
		[ -n "$tag" ] || fatal "Can't find appropriate tag for $GSSETVERSION version"
		GETSOURCEUUPDATE=1
		docmd gear-uupdate --upstream-version "$GSSETVERSION" --tag $tag
		docmd gear-commit

		update_post_git_submodules
		source_postupdate_hook

		# TODO: make plugins
		update_predownloaded development
		update_predownloaded production
		DOWNLOADSOME=1
		#fi
	fi

	# Set version if needed
	if [ -n "$GSSETVERSION" ] && [ "$GSSETVERSION" != "HEAD" ] ; then
		CURVER=$(get_version $spec)
		if [ "$CURVER" != "$GSSETVERSION" ] ; then

			CURRAWVER=$(get_raw_version $spec)
			if echo "$CURRAWVER" | grep -q "%" && echo "$CURRAWVER" | grep -v -E -q "^(%ver_major|%major)" ; then
				fatal "Can't override macros in Version field, check it manually. Only %ver_major and %major is supported"
			fi

			set_version $spec ${GSSETVERSION/-*/}
			set_release $spec $GSSETRELEASE
			if [ -n "$GSSETRELEASE" ] ; then
				echo "Set new $GSSETVERSION-$GSSETRELEASE version for $spec"
			else
				echo "Set new $GSSETVERSION version for $spec"
			fi
		else
			echo "Version $GSSETVERSION already is set"
			GSSKIPADDCHANGELOG=1
		fi
	fi

	# download from all possible sources
	LOADALL=1
	if [ -n "$LOADALL" ] ; then
		SOURCELIST=$(grep "^Source[0-9]*:" $spec | sed -e "s|:.*||g")
	else
		SOURCELIST=$(grep "^Source0\?:" $spec | sed -e "s|:.*||g")
	fi

    FIRSTSOURCEDIR=''
	if [ -z "$DOWNLOADSOME" ] ; then
	for SN in $SOURCELIST
	do
		CURRENTURL=''
		GETSOURCE=$(eval_spec $spec | get_var "$SN")
		[ -n "$GETSOURCE" ] || fatal "Problem with empty $SN"

		echo "Updating $SN: $GETSOURCE ..."

		# for get RPMSOURCEDIR and BASENAME/VERSION/RELEASE
		build_rpms_name $spec

		mkdir -p $RPMSOURCEDIR/ || fatal "Can't create/chdir..."
		FTB=$RPMSOURCEDIR/$(basename "$GETSOURCE")

		# TODO: do not use RPMSOURCEDIR for temp. tarballs
		[ -n "$FORCEDOWNLOAD" ] && rm -fv "$FTB"
		#[ -f "$RPMSOURCEDIR/$FTB" ] && { echog "Tarball $FTB already exists in $RPMSOURCEDIR dir, skipping." ; continue ; }

		if rhas "$GETSOURCE" "ps?://" ; then
			download_any_tarball "$GETSOURCE" "$FTB"
			will_commit || continue
			CURRENTURL="${GETSOURCE}"
			if is_gear ; then
				commit_tarball "$FTB" || fatal
				source_postupdate_hook
			fi
			DOWNLOADSOME=1
			echog "DONE with $FTB"
			continue
		fi

		# Test for eterbuild extensions like # Source-git
		# (will set GETSOURCEURL or GETSOURCESVN)
		get_source_url $spec $SN || get_source_git $spec $SN || get_source_svn $spec $SN || get_source_script $spec $SN || get_source_vcs $spec $SN
		#[ "$SN" = "Source1" ] && exit
		#if ! rhas "$GETSOURCE" ".tar$" ; then
		#	warning "It is recommended to use .tar tarballs for sources ($FTB now)"
		#fi

		if [ -n "${GETSOURCESVN}" ] ; then
			is_gear $SPECDIR || fatal "Source-svn works only with gear repo"
			will_commit || continue
			# clone svn repo to current dir
			# FIXME: need to clone in git root dir
			docmd git svn clone $GETSOURCESVN $(get_root_git_dir)
			echo "Run svn rebase from $GETSOURCESVN"
			docmd git svn rebase
			DOWNLOADSOME=1

		elif [ -n "${GETSOURCEGIT}" ] ; then
			is_gear $SPECDIR || fatal "Source-git works only with gear repo"
			will_commit || continue
			echog "Try to fetch ${GETSOURCEGIT} for $spec"
			#TODO error if incompatible
			HEAD_BEFORE=$(git rev-parse HEAD)
			docmd git remote add upstream $GETSOURCEGIT
			docmd git fetch --tags upstream
			update_master_branch_to "$GSSETVERSION"
			HEAD_AFTER=$(git rev-parse HEAD)
			if [ "$HEAD_BEFORE" = "$HEAD_AFTER" ] ; then
				fatal "Sources were not updated: HEAD unchanged after merge. Check upstream URL and tag."
			fi
			FIRSTSOURCEGIT=1

			update_post_git_submodules
			source_postupdate_hook
			DOWNLOADSOME=1

		elif [ -n "${GETSOURCESCRIPT}" ] ; then
			is_gear $SPECDIR || fatal "Source-script works only with gear repo"
			#[ -x "$GETSOURCESCRIPT" ] || fatal "Can't find executable $GETSOURCESCRIPT"
			bash -x $(get_root_git_dir)/$GETSOURCESCRIPT $GSSETVERSION "$FTB" || fatal "fatal with $GETSOURCESCRIPT"
			will_commit || continue
			CURRENTURL="${GETSOURCESCRIPT}"
			commit_tarball "$FTB" || fatal
			source_postupdate_hook
			DOWNLOADSOME=1

		# TODO: rewrite code to use original file format and temp. download dir
		elif [ -n "${GETSOURCEURL}" ] ; then
			if [ -z "$FORCEDOWNLOAD" ] && check_tarball "$FTB" ; then
				echog "$FTB already exists, skipping... "
			else
				echog "Try to load ${GETSOURCEURL} for $spec"
				download_to "$GETSOURCEURL" "$FTB" || fatal "Can't download $GETSOURCEURL"
				will_commit || continue
				# hack: override url from download_to
				CURRENTURL="${DOWNLOADEDURL}"
				if is_gear ; then
					commit_tarball "$FTB" || fatal
					source_postupdate_hook
				fi
				DOWNLOADSOME=1
			fi

		else
			warning "$SN $GETSOURCE has no URL. Skipping... "
		fi

	done

	# TODO: make plugins
	update_predownloaded development
	update_predownloaded production

	fi

	[ -n "$DOWNLOADSOME" ] || fatal "No upstream code is updated for new version $GSSETVERSION."

	if [ -z "$GSSKIPADDCHANGELOG" ] ; then
		# Write changelog if all done
		CURVER=$(get_version $spec)
		CURREL=$(get_release $spec)
		EGEARME=""
		[ -n "$ETERBUILD_SIGN_CHANGELOG" ] && is_gear && EGEARME=" (with rpmgs script)"
		if grep -q "^- new version $CURVER$" $spec ; then
			subst "s|- new version $CURVER$|- new version ($CURVER)$EGEARME via gear-uupdate|" $spec && git commit --amend $spec
		else
			add_changelog_helper "- new version ($CURVER)$EGEARME" $spec || echog "Changelog entry for $CURVER-$CURREL already exists"
		fi
	fi

done

exit 0

