#!/bin/sh

#       Name:    check
#       Version: 1
#       License:  GPL v3
#       https://gnu.org/licenses/gpl.html

#       The script was designed for UPLOS to download the list of SRPMS from the repository
#          32bit and 64bit and show the list where there are differences.

# script use:
# curl , lynx , awk , tput

#   Problems:
# - Eliminated kernel from the list, due to too long name (line 52 , 75)



#---Checking exist dirs to work --{
aaa=`pwd`/TMP/
if [ ! -d $aa ];
   then
   echo "TMP dir not found / will created."
   mkdir TMP
else
	rm -fvr TMP
	mkdir TMP
fi
#---------------------------------}


#----------------------{
aaaa=$(locale -a | grep en_US.utf8 | wc -l)

if [[ "$aaaa" == "0" ]]; then
	# 
	echo -e "Error: missing locale en_US.utf8, please install. \n - locale is needed for sort."
	exit 1
fi
#----------------------}








#------- CREATING THE LIST 64 BIT -------------------------------------------{
# Creating "one-dimensional array variables" ,
# here will list departments 64bit
DZIALY_64BIT=(SRPMS)

# number of all departments
LICZBA_DZIALOW_64BIT="${#DZIALY_64BIT[@]}"


echo " - Number of departments: $LICZBA_DZIALOW_64BIT "


	for i in `seq 1 $LICZBA_DZIALOW_64BIT`
	do
		NUMER=$(($i-1)) 
		DZIAL=${DZIALY_64BIT[$NUMER]}
		echo "$NUMER - $i - $DZIAL"

		# - Saved only name of the package and date
		# - Eliminated kernel from the list due to too long a name
		lynx   -nolist -dump -width 120   http://ftp.vim.org/ftp/os/Linux/distr/pclinuxos/pclinuxos/srpms/$DZIAL.pclos/ \
		| grep "\[   \]" | awk -F" " '{ print $3}' | grep -v "kernel" | sed -e 's/pclos.....nosrc.rpm//g' \
		-e 's/pclos.....src.rpm//g'  >> `pwd`/TMP/64bit.rpm.lista0
	done

echo " - LIST OF PACKAGES 64 READY :) (Changed lib64 to lib)"
#------- CREATING THE LIST  64 BIT -------------------------------------------}



#------- CREATING THE LIST 32 BIT ----------------------------------{
DZIALY_32BIT=(SRPMS)
LICZBA_DZIALOW_32BIT="${#DZIALY_32BIT[@]}"

echo " - Number of departments 32bit: $LICZBA_DZIALOW_32BIT "

	for i in `seq 1 $LICZBA_DZIALOW_32BIT`
	do
		NUMER=$(($i-1)) 
		DZIAL=${DZIALY_32BIT[$NUMER]}
		echo "$NUMER - $i - $DZIAL"

		lynx   -nolist -dump -width 120  http://ftp.vim.org/ftp/os/Linux/distr/uplos/srpms/$DZIAL.uplos/ \
		| grep "\[   \]" | awk -F" " '{ print $3}' | grep -v "kernel" | sed -e 's/pclos.....nosrc.rpm//g' \
		-e 's/pclos.....src.rpm//g' \
		-e 's/uplos.....src.rpm//g' >> `pwd`/TMP/32bit.rpm.lista0
	done

echo " - LIST OF PACKAGES 32 BIT IS READY :)"
#------- CREATING THE LIST 32 BIT ----------------------------------}






#------- CREATING THE LIST UPDATED --------------------------------------------{
#------------- 32bit --------------{
echo "- Creating columns for 32bit separated by a space:"

# Versions
rev `pwd`/TMP/32bit.rpm.lista0 | awk -F"-" '{ print $1 "-" $2 }' | rev | nl > `pwd`/TMP/32bit.rpm.lista1
# Names
rev `pwd`/TMP/32bit.rpm.lista0 | cut -d"-" -f 3- | rev | nl > `pwd`/TMP/32bit.rpm.lista2

# Join files (name version)
join `pwd`/TMP/32bit.rpm.lista2 `pwd`/TMP/32bit.rpm.lista1 | awk -F" " '{ print $2, $3 }'| sort > `pwd`/TMP/32bit.rpm.lista3


# Warning if there are duplicate packages
a1a=$(uniq -D `pwd`/TMP/32bit.rpm.lista3 | wc -l)

if [[ "$a1a" -gt "1" ]]; then
	uniq -D `pwd`/TMP/32bit.rpm.lista3 | uniq > `pwd`/TMP/1.Warning.32bit
	echo "-------------"
	echo -e "Warning: There is a duplicate package repo 32bit \n list is in `pwd`/TMP/1.Warning.32bit"
	echo "-------------"
fi


# This sorts versions and returns a list of only latest packages
sort -V -r `pwd`/TMP/32bit.rpm.lista3 | sort  -u -k1,1 | sort > `pwd`/TMP/32bit.rpm.lista4
#------------- 32bit --------------}


#------------- 64bit --------------{
echo "- Creating columns for 64bit separated by a space:"

# Versions
rev `pwd`/TMP/64bit.rpm.lista0 | awk -F"-" '{ print $1 "-" $2 }' | rev | nl > `pwd`/TMP/64bit.rpm.lista1
# Names
rev `pwd`/TMP/64bit.rpm.lista0 | cut -d"-" -f 3- | rev | nl > `pwd`/TMP/64bit.rpm.lista2

# Join files (name version)
join `pwd`/TMP/64bit.rpm.lista2 `pwd`/TMP/64bit.rpm.lista1 | awk -F" " '{ print $2, $3 }'| sort > `pwd`/TMP/64bit.rpm.lista3


# Warning if there are duplicate packages
a1a=$(uniq -D `pwd`/TMP/64bit.rpm.lista3 | wc -l)

if [[ "$a1a" -gt "1" ]]; then
	uniq -D `pwd`/TMP/64bit.rpm.lista3 | uniq > `pwd`/TMP/1.Warning.64bit
	echo "-------------"
	echo -e "Warning: There is a duplicate package repo 64bit \n list is in `pwd`/TMP/1.Warning.64bit"
	echo "-------------"
fi


# This sorts versions and returns a list of only latest packages
sort -V -r `pwd`/TMP/64bit.rpm.lista3 | sort  -u -k1,1 | sort > `pwd`/TMP/64bit.rpm.lista4



