#!/bin/sh
# 2004-2008 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
#   src.rpm-     (  ALT Linux)     .
#        
#      apt-get source 
# -c - checkonline
# -n - do not install

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

# path to ALT Linux's repositories
REPOSITORY="files/SRPMS obsolete orphaned"

# loads repos file and fill SYSNAME and SYSURL arrays
load_systems_list()
{
	local IDX=0
	local line
	while read line ; do
		# skip comments
		echo $line | grep "^#" >/dev/null && continue
		SYSNAME[$IDX]=${line/ *.*/}
		#FTP[$IDX]=${line/.* \+/}
		SYSURL[$IDX]=$(echo $line | sed -e "s|.* \+||g")
		IDX=$(($IDX+1))
	done < $ETERBUILDETC/repos
}

list_systems()
{
	echo "Known systems:"
	for ((i=0; i < ${#SYSNAME[*]}; i++)) ; do
			printf "[%-17s] %s\n" ${SYSNAME[$i]} ${SYSURL[$i]}
	done
}

html_filter()
{
	grep "src.rpm" | sed -e "s|.*href=\"||g" | sed -e "s|\".*||g"
}

list_filter()
{
	sed -e "s|.src.rpm$||g"
}

# Using: git_list idx [force]
# set LIST variable to list file
get_list()
{
	[ -n "$VERBOSE" ] && echo "get_list for $1"
	local URL=${SYSURL[$1]}
	local FORCE=$2
	local SYS=${SYSNAME[$1]}
	local CURL=curl

	which $CURL 2>/dev/null >/dev/null || fatal "curl command needed for download"
	LIST=$OURTMPDIR/tmp-rpmgpall-$SYS
	if [ ! -f "$LIST" ] || [ "$FORCE" = "force" ] ; then
		echo "Get list for $SYS from $URL"
		#curl -l $URL/ >$LIST
		# suitable for ftp and http lists
		$CURL -l $URL/ | html_filter >$LIST
	else
		echo "List for $SYS"
	fi
}

get_system_idx()
{
	for ((i=0; i < ${#SYSNAME[*]}; i++)) ; do
		if [ ${SYSNAME[$i]} = "$1" ] ; then
			echo $i
			return 0
		fi
	done
	return 1
}

check_name()
{
	if [ -z "$1" ] ; then
		echo "Error: missed package param"
		exit 1
	fi
	NLIST=`cat $LIST | grep -i $1`
	if [ `cat $LIST | list_filter | grep -i $1 | wc -l` -gt 1 ] ; then
		#echo "Please type a full name of the package"
		print_list $NLIST
		return 1
	fi

	if [ -z "$NLIST" ] ; then
		echo "$1: Not found"
		return 1
	fi

	return 0
}

check_system()
{
	local IDX=$1
	local URL=${SYSURL[$IDX]}
	#echo "Check for $SYSTEM"
	get_list $IDX $FORCEUPDATE
	#ls -l $LIST
	check_name $NAME && wget -c $URL/$NLIST
}


if [ "$1" = "-h" ]; then
	echog "Using:"
	echog "rpmgp [-a -c -n] [system] pkgname - download src.rpm from 'system' repository to RPM/SRPMS and install it"
	echog " name - installed package name or src.rpm"
	echog " system - name of system (ALT Linux by default if -a missed too)"
	echog " -a  check for pkgname in all known repositories"
	echog " -c  check for exist the package in ALT Linux repository"
	echog " -b  install packages needed for build (installs with sudo apt-get)"
	echog " -l  lists packages needed for build (in local pkg system notation) (experimental)"
	echog " -n  do not install in RPM after download, download in current dir"
	echog " -r  refresh package list (download it again)"
	echog " -s  lists all known systems"
	exit 0
fi


TRYINST="1"
if [ "$1" = "-n" ] ; then
	shift
	TRYINST=""
fi

# list systems
if [ "$1" = "-s" ] ; then
	load_systems_list
	list_systems
	exit 0
fi

# update cache list
if [ "$1" = "-r" ] ; then
	load_systems_list
	FORCEUPDATE=force
	shift
	# if followed by system name
	IDX=`get_system_idx "$2"`
	if [ -n "$IDX" ] ; then
		get_list $IDX $FORCEUPDATE
		shift
		exit 0
	fi
fi

# check for all repos
if [ "$1" = "-a" ] ; then
	ALLSYSTEM=1
	shift
fi

test -z "$1" && fatal "Please run with spec/package name"

#
#   ,  ( ?),   
#  -    
#    
#
#  - [] 


# install binary packages
if [ "$1" = "-b" ] ; then
	shift
	parse_cmd_pre "$@"
	pack_src_rpm $LISTRPMARGS
	echog "Running apt-get for install needed packages for $LISTBUILT"
	cd $RPMDIR/SRPMS
	# FIXME: ALT Specific
	$SUDO apt-get build-dep $LISTBUILT
	cd -
	exit 0
fi

# lists packages for build
if [ "$1" = "-l" ] ; then
	shift
	print_target_buildreq $1
	exit 0
fi

# Check online
if [ "$1" = "-c" ] ; then
	shift
	CHECKONLINE=1
	if [ -f $1 ] ; then
		build_rpms_name "$1"
		SRCRPM=$NAMESRPMIN
	else
		echo $1 | grep rpm 2>/dev/null && SRCRPM=$1 ||
		SRCRPM=`rpm -q $1 --queryformat "%{SOURCERPM}\n" | tail -n 1` 
	fi
	RET="MISSED"
	for i in $REPOSITORY ; do
		echog "Checking in repository $i ..."
		rsync -n --progress $RSYNCSISYPHUS/$i/$SRCRPM  >/dev/null 2>&1 && { RET=$i; break; }
	done
	echog "Repository     Filename"
	printf "%-12s %30s\n" $RET $SRCRPM
	exit 0
fi

# Check concrete system
# two params: system, package
if [ -n "$2" ] ; then
	NAME=$2
	load_systems_list
	IDX=`get_system_idx "$1"`
	if [ -n "$IDX" ] ; then
		check_system $IDX
	else
		#fatal "Unknown system '$1', use rpmgp -s for get list"
		NAME=
	fi
	exit 0
else
	NAME=$1
fi

# Check all systems
if [ -n "$ALLSYSTEM" ] ; then
	load_systems_list
	# search throw all systems
	for ((i=0; i < ${#SYSNAME[*]}; i++)) ; do
		echo
		check_system $i
	done
	exit 0
fi


echo
echo "########################################"
if [ -z "${1/*src.rpm/}" ]
then
	# it is src.rpm package
	SRCRPM=$1
else
	if [ -z "${1/*rpm/}" ] ; then
		# it is rpm package (locale placed?)
		SRCRPM=`rpm -qp $1 --queryformat "%{SOURCERPM}\n"`
	else
		SRCRPM=`rpm -q $1 --queryformat "%{SOURCERPM}\n"`
	fi
fi
test -z "$SRCRPM" && fatal "Cannon find package for $1"
echog "Try to download $SRCRPM"

test -z ${TRYINST} || { mkdir -p $RPMDIR/SRPMS && cd $RPMDIR/SRPMS ; }
echog "Downloading in $(pwd)"

for i in $REPOSITORY
do
	echo "--------Check in $i-----------"
	rsync -vay --partial --checksum --copy-links \
		--delete-after --progress $RSYNCSISYPHUS/$i/$SRCRPM ./$SRCRPM && break
done

test -z ${TRYINST} && exit 0
echo
echog "Installing..."
rpm -iv $SRCRPM

