#! /bin/bash
#H File download.sh
#H Syntax in console : "./bin/dowload <bookname>"
#H Example : "./download.sh LivreTest" in test mode in a terminal
#H Creation date : 28/03/2020
#H Modified : 26 april 2020 par GC
#H Modified : 30 may 2020 par GC for automatic insrallation
#H Modified : 27 may 2021 for Add_Appendix
#H Documentation version de la documentation on WikiLivres le : 26 april 2020
#H
#D download.sh documentation
#D header.inc is included and there is no need to recreate the environment
#D variables
#D Download creates or recreates a local html book with html subdirectories of all articles
#D Create a concatenated sub-list locale.list of the book from book articles-list
#D To document the book with its subpages :
#D Create a directory containing the main html page of the book and local directories
#D containing an html version of the articles
#D
VERSION=210619
#O . gettext.sh for translation
. gettext.sh
#O Include the file header.inc
if test -e ~/Add_appendix/bin/header.inc; then source ~/Add_appendix/bin/header.inc
else echo "$Bindir/header.inc not found. Execute sbin/cp-src2bin.sh"; exit -1
fi
#O Download the complete book structure in working directory
Projectdir=$Workdir/books/$1
cd $Projectdir
mkdir temp
if [ "$2" = "--t" ]; then echo "current dir : "; pwd; fi
echo "download all directories of the book '$1'"
wget -r -linf -k -p -E -i $1.list -o $Projectdir/temp/wget-log-download.txt
echo "----------"
#O Move the html pages into working sub-directories to document the pages and sub pages
#O create a local list to the downloaded directories $ Projectdir/$1.locale.list
echo "create the complete concatenated hierarchy of the directories of the book '$1'"
cat $Projectdir/$1.list | sed "s/https:\/\///g" | sed "s/\ /\\\ /g" | tr '\n' ',' > $Projectdir/$1.locale.list
echo " Concatenated local list $Projectdir/$1.locale.list :"
echo ""
cat $Projectdir/$1.locale.list
echo "----------"
#O Copy the html files to respective directories
#O Create a file with the pagename $Projectdir/$1.mainPage
echo "créer la page du lien local vers la page principale, 'le livre'."
echo "create the page from the local link to the main page, 'the book'"
cat $Projectdir/$1.locale.list | sed "s/ /\\ /g" | cut -d ',' -f1 > $Projectdir/$1.mainPage
echo "----------"
#O Initialize the variable $mainPage
read mainPage < $Projectdir/$1.mainPage
echo "variable mainPage = $mainPage"
#T cat $Projet/$1.mainPage | awk -F"/" '{print NF}' > nbchamps
#T read NbChamps < nbchamps
#T echo "Variable NbChamps = $NbChamps"
echo "----------"
#O Create a file of the working directories to be created.
ls "$mainPage" | sed "s/.html//g" > $Projectdir/$1.dirs
echo "sub-working-diectories : "
cat $Projectdir/$1.dirs
echo "----------"
#O Copy the html pages and subpages in the respective directories
while read line
do
echo "$line".html | sed "s/https:\/\///g" | tr '\n' ' ' > source
read Source < source
echo "Source = $Source"
echo "$line" | awk -F"/" '{ print $NF }'| tr '\n' '/' > destination
read dir < destination
mkdir $dir
echo "$line".html | awk -F"/" '{ print $NF }' >> destination
read Destination < destination
echo "Destination = $Destination"
echo "To copy : 'cp -f ./$Source $Destination'"
cp -f "./$Source" "$Destination"
done < $Projectdir/$1.list
rm source ; rm destination
#O Quitter le programme d'essai et passer à la création de la page "Annexe" des articles.
echo "----------"
# echo -en "\033[31mRetour-chariot une pour créer l'Annexe des articles, Ctrl-C pour quitter.\033[0m "
# read -p ">"
# "\033[31mRetour-chariot une pour créer l'Annexe, Ctrl-C pour quitter >"
# exit 0
# End download.sh