#!/bin/ash
#160220 sfs
if [ "$1" = "-r" ];then
    shift ; r=1; repl="`egrep ''$1'$' /etc/sfs-get/www.list |egrep -v '^#|^$'`" 
else repl="`egrep -v '^#|^$' /etc/sfs-get/www.list`"
fi
#echo $repl ;exit

wget1="sudo wget"
[ "$1" = "Введите" ] && exit
[ "$1" = "--help" ] && echo "Поиск модуля в репозитории $repl
Use: $0 часть_имени_модуля
-nn - nn самых свежих" && exit

NONET() { ntf -e "Нет подключения" "к интернет"  && exit; }

GET() {
up=1
[ -f list ] && mv list "`date '+%y%m%d-%H%M'`list"
$wget1 -q --no-remove-listing "$rep" -O index.html # >/dev/null 2>&1
#wt "$wget1 -q --no-remove-listing "$rep" -O index.html" "Обновление базы"
	    status=$?
    	    if [ ${status} -eq 0 ]; then
		#ntf -i "$rep" "База модулей обновлена" &
#		m1="$m1
#$rep"
		if [ "`egrep '<li>' index.html`" ];then
		    tr -d '\r' < index.html | awk -F">" '/^<li><a href=/ {print $3" 0 0 ?M"}'|sed 's/<\/a//' > list && rm -f index.html
		else
		    tr -d '\r' < index.html | awk -F">" '/^<a href=/ {print $2" "$3}'|sed 's/<\/a//' > list && rm -f index.html
		fi
    	    else 
    		ntf -a "ОШИБКА!" "Не удалось обновить список модулей\n$rep" && exit 
	    fi
}

GETLIST() {
repd="`echo "$1"| sed 's/\//+/g'`"
repdt="/tmp/sfs-get/$repd"
[ -d "$repdt" ] || mkdir -p "$repdt"  
cd "$repdt"
[ "`find . -type f -mtime -1 -name "list"`" ]  || GET  #>1 day
}

[ "`echo "$repl" |egrep -v '^#|^$' `" = "" ] && (ntf -e "$repl" "пуст" &) && exit 

GETREPOLDXXX(){
`which sudo` ping -c 1 -W 1 8.8.8.8 >/dev/null 2>&1 || NONET
wt -start "обновление списков модулей.pfs" &
for rep in $repl ;do
    GETLIST "$rep"
    [ "$1" = "" ] && continue #exit #GET && exit || exit
    lists="$repdt/list"
[ "`echo "$1"|egrep '^-[0-9][0-9]$'`" ] && cat "$lists" |awk '{print $2" "$3" "$4" "$5" "$1}'  |sed 's/\/tmp\/sfs-get\///' |sed 's/\/list:/ /' |sort -M |head "$1" && exit
w="`cat "$lists" |egrep -i "$1"  |awk '{print $2" "$3" "$4" "$5" "$1}' |sed 's/\/tmp\/sfs-get\///' |sed 's/\/list:/ /' |sort -M `"
ww="$ww$w"
echo "$w"| awk '/.pfs$/ {print "'$rep'/"$4}'
done

wt -kill; kill `pgrep -f "sleep 99999999999"`
[ "$up" ] && ntf -i "Базы модулей обновлены" "$m1" &
}

getrepolist || { ntf -a "Не удалось обновить" "список модулей.pfs репозитория. Проверьте подключение к интернет" ;exit; }
[ "$1" ] && gawk 'BEGIN{IGNORECASE=1} $1~/'$1'[^/]*$/ {print $1}' /tmp/mod_update/repolist.txt
