
#!/bin/bash
## This script is used to check if the local mirror matches the remote mirror
##  Otherwise it downloads the files and copy them in the coresponding directories

## 1. Retrieve the list of packets with their size declared in the Packages files
##       which are retrieved from the site 
## 2. We retrieve the files and their size locally in the file : localPackages 
## 3. We execute the program in python OfficielPackages.py to create the file remotePackages
## 4. We sort the 2 files : localPackages and RemotePackages:
##    sort -k 2 localPackages   > localPackages.sort 
##    sort -k 2  remotePackages > remotePackages.sort
## 5. The leader file in comparison  is remotePackages.sort, localPackages  file may be missing 
##     some packages  or have an invalid size, use the program in python  differences.py 
##     that gives Result.sort  which completes the missing part with a size 000 and the word "missing"
## 6. the grep and awk procedure selects  incorrect package or size
##        awk '($1! = $3) {print $2}' Result.sort >  toDown.txt
## 7. Retrieve the exact file path from fileName.txt for packages
##     then do a file upload, with the commands
##        site = "ftp.fr.dedbian.org/debian"
##        for i in $ (cat toDown.txt); do path=$(grep $ i FileName.txt);
##              echo $site/$path >> ForDownload.txt;
##       delete existing packages in toDown.txt but  in fileName.txt
##        awk -F/ '($3!="") {print $ 0}' ForDownload.txt> toDownload.txt
## 8. Automatic Download:
##      for i in $ (cat toDownload.txt); do wget -c $i; done
## 9. Copy the package in the corresponding directory ( this can be improved by using option in wget
##     awk -F / '{print "cp" $7"\t"$1"/"$2"/"$3"/"$4"/"​​$5"/"$6}' toDownload.txt> download.sh
## 10. bash download.sh
##
###   declare the root directory for the site
DIR="/home/ftp"

## Build the list of packages that SHOULD be installed



cat $DIR/ftp.fr.debian.org/debian/dists/*/*/binary-amd64/Packages | grep ^Version | awk '{print $ 2}'> Vers.txt
cat $DIR/ftp.fr.debian.org/debian/dists/*/*/binary-amd64/Packages | grep ^Package | awk '{print $ 2}'> Paq.txt
cat $DIR/ftp.fr.debian.org/debian/dists/*/*/binary-amd64/Packages | grep ^Archi | awk '{print $ 2}'> Arch.txt
cat $DIR/ftp.fr.debian.org/debian/dists/*/*/binary-amd64/Packages | grep ^Size | awk '{print $ 2}'> Size.txt


echo "create  files Arch.txt, Size.txt ...."


## Retrieve package path
cat $DIR/ftp.fr.debian.org/debian/dists/*/*/binary-amd64/Packages | grep ^Filename | awk '{print $ 2}'> fileName.txt

echo "create  file fileName.txt"


#### Retrieve the packets present in Local
## provides the packetfile
ls ftp.fr.debian.org/debian/pool/*/*/* -l | grep .deb $ | awk '{print $5 "\t\t" $9}'> localPackages


echo "file localPackages"


## build the package file in the appropriate form
## size name + version + arch.deb
## Provides RemotePackage File
Python3 OfficialPackages.py

echo "Create  remotePackage file "


### sorts the files to make the comparison

sort -k 2 localPackages   > localPackages.sort 
sort -k 2  remotePackages > remotePackages.sort

echo  "sort the files "

### Retrieve the list of differences in
### the Result.sort file
### execute
Python3 differences.py

#### on this todown.txt file
# Packages to download
awk '($1!= $3) {print $2}' Result.sort> toDown.txt

### on the download file
## to use wget
###
rm forDownload.txt
site = "ftp.fr.debian.org/debian"
for i in $(cat toDown.txt); do path=$(grep $i fileName.txt); echo $site/$path >> forDownload.txt; done
### delete existing packages in toDown.txt
# But not in fileName
awk -F/ '($3! = "") {print $0}' forDownload.txt> toDownload.txt


## make an automatic download
###  may be inproved to store diectly in the root of site
##  must be a root
##  recall $DIR is the root directory  of the site

for i in $ (cat toDownload.txt); do wget -P $DIR -c $i; done
echo "automatic download"







 
