Auto-éditer un wikilivre/Auto-référencer/INSTALLER.sh

Un livre de Wikilivres.
#! /bin/bash
VERSION=200604
#P Fichier : INSTALLER.sh
#P Syntaxe : INSTALLER.sh [ liste | pour moi | pour tous | pour admin ]
#P Exemple : './INSTALLER.sh annexer.list' (dans le répertoire ~/tmp)
#P      ou : lorsque les fichiers ont été téléchargés :
#P             './INSTALLER.sh pour moi' (dans le répertoire ~/tmp)
#P créé le : 25 mai 2020 par l'utilisateur wikilivres Goelette Cardabela.
#P modifié : le 2 juin 2020 pour ajouter la commande de copie dans le
#P             répertoire de l'utilisateur.
#P modifié : le mercredi 3 juin 2020 par GC pour corriger la syntaxe de la
#P             ligne de commande
#P modifié : le 4 juin 2020 Correction d'un défaut de filtrage qui supprime le
#P           caractère '\' devant '033[' code de couleur pour les lignes
#P           d'affichage. ligne 306 dans la version du 4 juin 2020.
#P 
#P Ce fichier exécutable permet d'installer l'aplication Annexer pour créer la
#P   page 'Annexe' des livres de Wikilivres.
#P Le fichier de liste contenant les liens vers les fichiers à télécharger est
#P   sensé exister dans le répertoire ~/tmp. (Annexer.prj ou Annexer.list)
#P 

#O Vider l'écran. (Clear screen)
clear

#P Pour les traductions '. gettext.sh' en tête du fichier.
#P . gettext.sh

#P Créer la fonction d'affichage 'syntaxe'.
function put_syntax #P peut-être à revoir ...
{
      echo "$(gettext ' Syntaxe : ')"
      echo "$(gettext ' INSTALLER.sh [ liste | pour moi | pour tous | pour admin ] ')"
      echo "$(gettext '    Exemples dans le répertoire d installation : ~/tmp ')"
      echo "$(gettext '    -> example ./INSTALLER.sh annexer.list, pour télécharger les articles depuis wikilivres. ')"
      echo "$(gettext '    -> example ./INSTALLER.sh pour moi, pour copier les codes dans ~/Annexer. ')"
      echo ""
      exit 0
}

#P Créer la fonction 'Nettoyage des répertoires' avec l'autorisation de l'utilisateur.
function make_clean
{
#O Afficher ATTENTION suppression du contenu du répertoire ~/tmp en rouge clignotant.
    echo -e "\033[5;31m ATTENTION !  Suppression du contenu du répertoire ~/tmp \033[0m "

#O Afficher " Ctrl-C pour quitter."
    echo ""
    echo " Ctrl-C pour quitter."
    
#O Attendre 'un' (un seul) caractère de réponse.
    read -n1 -p -s Inkey
    echo "Inkey = "$Inkey
    rm -f ~/tmp/*.tmp; rm -f ~/tmp/*.txt; rm -f ~/tmp/temp/*.tmp; rm -f ~/tmp/temp/*.txt; rm -f ~/tmp/temp/*.sh;
}

#O Si le premier paramètre de la ligne de commandes commence par ''pour''   
    if [ "$1" = "pour" ] 
#O alors
    then 
    {
#O   Si le deuxième paramètre est 'moi'   
      if [ "$2" = "moi" ]
      then 
      {
        echo -n "Cette option de tests 'pour moi' doit copier tous les fichiers dans le"
        echo  "répertoire 'Annexer', sous le répertoire racine de l'utilisateur."
        if test -w ~/Annexer; then echo -e "\033[5;33m Le répertoire 'Annexer' pré-existe, il doit être supprimé ou renommé. \033[0m "; exit -1; fi
        mkdir ~/Annexer ~/Annexer/bin; cp ~/tmp/bin/* ~/Annexer/bin/.; chmod 755 ~/Annexer/bin/*
        ln -s -v ~/Annexer/bin/annexer ~/Annexer/annexer
        mkdir ~/Annexer/src; cp ~/tmp/src/* ~/Annexer/src/.
        mkdir ~/Annexer/doc; cp -R ~/tmp/doc/* ~/Annexer/doc/.
        echo "Le nettoyage du répertoire '~/tmp' n'est pas envisagée avec l'option de tests 'pour moi'."
        exit 0
      }
      fi
     
      if [ "$2" = "tous" ]
      then 
      {
        echo "Version française."
        echo "Cette option 'pour tous' n'est pas encore en service." 
        # copie les fichiers dans les répertoires :"        
#w         echo "  /usr/local/bin "        
#w         echo "  /usr/local/src/annexer/*.sh "
#w           mkdir /usr/local/src/annexer; cp ~/tmp/src/*.sh /usr/local/src/annexer/.          
#w         echo "  /usr/local/src/doc/annexer/*.sh/* "
#w           mkdir /usr/local/src/doc/annexer; cp -R ~/tmp/doc/* /usr/local/src/doc/annexer/.          
#w        echo "  /usr/local/share/doc/annexer/copyright, README"        
#w        echo "  /usr/local/share/man/man1/annex.1.gz"        
#w        echo "  /usr/local/share/man/fr/man1/annex.1.gz"        
#w        echo "  /usr/local/share/man/es/man1/annex.1.gz"
        make_clean
        exit 0
      }
      fi
     
      if [ "$2" = "admin" ]
      then 
      {
        echo "Cette option 'pour administrateurs' n'est pas encore en service."
        make_clean
        exit 0
      }
      fi 
   }
#O Fin si
    fi   
   

#O Si le répertoire ~/tmp existe, alors: afficher sa présence, sinon: créer le
#O   répertoire.
if test -d ~/tmp; then echo "~/tmp existe et ne sera pas recréé."; else mkdir -p ~/tmp; fi

#O Si la commande est exécutée sans le premier paramètre, préciser la syntaxe.
#O   et quitter avec un retour à -1
    if ! test $1; then echo "Syntaxe : $0 [ liste | pour moi | pour tous | pour admin ]"; exit -1; fi
#P Version internationalisée: if ! test $1; then put_syntax; fi
    
#O Si le premier paramètre de la ligne de commandes commence par ''pour''   
    if [ "$1" = "pour" ] 
      then copier;
#O   Fin si
      fi

#O Lister le contenu du fichier entré en premier paramètre.
    echo "Catalogue du fichier $1 entré en premier paramètre. :"
    echo " Liste des fichiers à télécharger : "
    cat $1

#O Affichier " Retour-chariot pour continuer, Ctrl-C pour quitter."
    echo ""
    echo " Retour-chariot pour continuer, Ctrl-C pour quitter."
    
#O Attendre 'un' (un seul) caractère de réponse.
    read -n1 -p -s Inkey
    echo "Inkey = "$Inkey

#O Entrer dans le répertoire ~/tmp
    cd ~/tmp

#O Télécharger en version html, dans le répertoire courant, tous les
#O   répertoires et fichiers de la liste entrée en premier paramètre.
#P   Ne retélécharge pas si les fichiers sont déjà présents !
    wget -r -linf -k -p -E  -i $1 -o wget-log-$1.txt

#T Afficher le contenu du répertoire.
#T ls -R
#T exit

#T Afficher " Retour-chariot pour continuer, Ctrl-C pour quitter."
#    echo " Retour-chariot pour continuer, Ctrl-C pour quitter."
#T Attendre 'un' (un seul) caractère de réponse.
#    read -n1 -p -s Inkey
#    echo "Inkey = "$Inkey

#O Forcer la reconstruction du fichier 'html_data_conversions' après supression.
    rm html_data_conversions
#O Forcer la reconstruction du fichier 'html_url_conversions' après supression.
    rm html_url_conversions
    
#H tester la présence du fichier des 'html_data_conversions', si il n'existe
#H   pas; le créer.
    if test -a "html_data_conversions"
      then echo "Le fichier html_data_conversions est présent"
      else 
      {
        echo "s/&lt\;/</g; s/&gt\;/>/g; s/&amp\;/&/g; s/&quot\;/\"/g;" > html_data_conversions
        echo "s/&#39\;/'/g;" >> html_data_conversions
        echo "s/&eacute\;/é/g; s/&eacute\;/É/g; s/&egrave\;/è/g; s/&ecirc\;/ê/g;" >> html_data_conversions
        echo "s/&agrave\;/à/g;" >> html_data_conversions
        echo "s/&iuml\;/ï/g;" >> html_data_conversions
        echo "s/&ccedil\;/ç/g;" >> html_data_conversions
        echo "s/&ntilde\;/ñ/g;" >> html_data_conversions
        echo "s/&AElig\;/Æ/g;" >>html_data_conversions
        echo "s/&euro\;/€/g; s/&#8364\;/€/g;" >> html_data_conversions
        echo "s/&copy\;/©/g; s/&#169\;/©/g;" >> html_data_conversions
        echo "s/&reg\;/®/g; s/&#174\;/®/g;" >> html_data_conversions
        echo "s/&deg\;/°/g; s/&#176\;/°/g;" >> html_data_conversions
        echo "s/&ordm\;/º/g;" >> html_data_conversions
        echo "s/&laquo\;/«/g; s/&#171\;/«/g;" >> html_data_conversions
        echo "s/&raquo\;/»/g; s/&#187\;/»/g;" >> html_data_conversions
        echo "s/&micro\;/µ/g; s/&#181\;/µ/g;" >> html_data_conversions
        echo "s/&para\;/¶/g; s/&#182\;/¶/g;" >> html_data_conversions
        echo "s/&frac14\;/¼/g; s/&#188\;/¼/g;" >> html_data_conversions
        echo "s/&frac12\;/½/g; s/&#189\;/½/g;" >> html_data_conversions
        echo "s/&frac34\;/¾/g; s/&#190\;/¾/g;" >> html_data_conversions
        echo "s/&#156\;/œ/g;" >> html_data_conversions
        echo "s/<\/span>//g;" >> html_data_conversions
        echo "s/<span class=c1>//g;" >> html_data_conversions
        echo "s/<span class=\"c1\">//g;" >> html_data_conversions
        echo "s/<span class=nb>//g; s/<span class=nv>//g;" >> html_data_conversions
        echo "s/<span class=\"nb\">//g; s/<span class=\"nv\">//g;" >> html_data_conversions
        echo "s/<span class=s1>//g; s/<span class=s2>//g; " >> html_data_conversions
        echo "s/<span class=\"s1\">//g; s/<span class=\"s2\">//g; " >> html_data_conversions
        echo "s/<span class=k>//g;s/<span class=m>//g;s/<span class=o>//g;" >> html_data_conversions
        echo "s/<span class=\"k\">//g;s/<span class=\"m\">//g;s/<span class=\"o\">//g;" >> html_data_conversions
        echo "s/<span class=p>//g; s/<span class=\"p\">//g;" >> html_data_conversions
        echo "s/<span class=ch>//g; s/<span class=\"ch\">//g;" >> html_data_conversions
        echo "s/#!/'\n'#!/g; s/<pre><span>/'\n'/g;" >> html_data_conversions

        echo "s/%E2%80%99/\&#8217\;/g;s/%E2%82%AC/\&#8364\;/g;s/%2F/\//g;s/%3F/\?/g;" > html_url_conversions
        echo "s/%24/\$/g;s/%21/\!/g;s/%25/%/g;s/%2B/+/g;s/%3C/\\\&lt\;/g;s/%3E/\&gt\;/g;" >> html_url_conversions
        echo "s/%5C/\\\/g;s/%B0/°/g" >> html_url_conversions
        echo "s/%E0/à/g;s/%E2/â/g" >> html_url_conversions
        echo "s/%E8/è/g;s/%E9/é/g;s/%EA/ê/g;s/%EB/ë/g" >> html_url_conversions
        echo "s/%EE/î/g;s/%EF/ï/g" >> html_url_conversions
        echo "s/%F4/ô/g" >> html_url_conversions
        echo "s/%F9/ù/g;s/%FB/û/g" >> html_url_conversions
        echo 's/%2C/,/g;s/%21/!/g;s/%3F/?/g;s/%40/@/g;s/%23/#/g;s/%24/$/g' >> html_url_conversions
        echo "s/%25/%/g;" >> html_url_conversions
        echo "s/%26/\&#x26\;/g;" >> html_url_conversions
        echo "s/%28/(/g; s/%29/)/g;" >> html_url_conversions
        echo "s/%2B/+/g;" >> html_url_conversions
        echo "s/%3A/:/g;" >> html_url_conversions
        echo "s/%3D/=/g;" >> html_url_conversions
        echo "s/%5B/[/g;" >> html_url_conversions
        echo "s/%5D/]/g" >> html_url_conversions
        echo "s/%3B/;/g;s/%2F/\//g;s/%27/''/g" >> html_url_conversions
        echo 's/%22/"/g' >> html_url_conversions
        echo "s/%C3%A7/ç/g;s/%E7/ç/g" >> html_url_conversions
        echo "s/+/ /g;s/%7E/~/g;s/\\\/\&#92\;/g" >> html_url_conversions
        echo "s/%0D/\&#x0D\;/g;s/%0A/\&#x0A\;/g" >> html_url_conversions
        echo "s/%C3%A0/à/g;s/%C3%A2/â/g" >> html_url_conversions
        echo "s/%C3%A9/é/g;s/%C3%A8/è/g;s/%C3%AA/ê/g" >> html_url_conversions
        echo "s/%C3%AE/î/g;s/%C3%AF/ï/g" >> html_url_conversions
        echo "s/%C3%B4/ô/g" >> html_url_conversions
        echo "s/%C3%B9/ù/g;s/%C3%BB/û/g" >> html_url_conversions
        echo "s/%C2%B7/-/g;s/%C2%AB/«/g;s/%C2%BB/»/g" >> html_url_conversions
      }
    fi
    
#T echo "Les fichiers 'html_????_conversions' sont créé ou mis à jour."
#T cat html_data_conversions
#T cat html_url_conversions
#T exit
#T tests :
#T echo "Test, original : https://fr.wikibooks.org/wiki/Fichier:Commerce_de_communaut%C3%A9s_indig%C3%A8nes.JPG"
#T echo "Test, interprété : "
#T echo "https://fr.wikibooks.org/wiki/Fichier:Commerce_de_communaut%C3%A9s_indig%C3%A8nes.JPG"| sed -f html_url_conversions
#T exit
#T echo ""
#T echo "Test, le fichier html_data_conversion est créé ou mis à jour."
#T cat html_data_conversions
#T exit
#T 
#T echo "Test, original : <span class="c1">#P     grep -e -n &#39;#H&#39; -e &#39;#P&#39; -e &#39;#w&#39; header.sh</span>"
#T echo "Test, interprété : "
#T echo "<span class="c1">#P     grep -e -n &#39;#H&#39; -e &#39;#P&#39; -e &#39;#w&#39; header.sh</span>" | sed -f html_data_conversions
#T echo ""
#T echo "Test, original : <span class="c1">#P     ou : grep -E &quot;#H|#P|#w&quot; header.sh &gt; &lt;livre&gt;_doc/header.sh.HPw.doc</span>"
#T echo "Test, interprété : "
#T echo "<span class="c1">#P     ou : grep -E &quot;#H|#P|#w&quot; header.sh &gt; &lt;livre&gt;_doc/header.sh.HPw.doc</span>" |sed -f html_data_conversions
#T echo ""
#T echo "Test, original : <span class="nb">echo</span> <span class="s2">&quot;s/%24/\$/g;s/%21/\!/g;s/%25/%/g;s/%2B/+/g;s/%3C/\\\&amp;lt\;/g;s/%3E/\&amp;gt\;/g;&quot;</span> &gt;&gt; données_de_conversions"
#T echo "Test, interprété : "
#T echo "<span class="nb">echo</span> <span class="s2">&quot;s/%24/\$/g;s/%21/\!/g;s/%25/%/g;s/%2B/+/g;s/%3C/\\\&amp;lt\;/g;s/%3E/\&amp;gt\;/g;&quot;</span> &gt;&gt; données_de_conversions" | sed -f html_data_conversions

#T exit
echo "-----"
Srcdir=~/tmp/fr.wikibooks.org/wiki
Destdir=~/tmp
Datadir=~/tmp
echo " Répertoire source : "$Srcdir
echo " Répertoire de destination : "$Destdir
echo " Répertoire des données : "$Datadir
echo "-----"

#O entrer dans le répertoire des de travail Datadir.
cd $Datadir

#O Création du fichier de projet shell.prj dans le répertoire courant.
    cat $Datadir/$1 | grep -i '.sh' | sed -f $Destdir/html_url_conversions | cut -c9- > $Datadir/shell.prj
#O Créer le fichier de projet data.prj dans le répertoire courant
    cat $Datadir/$1 | grep -v -e '^$' -e '.sh' | sed -f $Destdir/html_url_conversions | cut -c9- > $Datadir/data.prj
 
#O Créer les répertoires temp, src, bin, doc, man dans $Destdir
    mkdir -p $Destdir/temp $Destdir/src $Destdir/bin $Destdir/doc $Destdir/man
    
#O Tant que l'on a des lignes dans la liste shell.prj enregistrer la ligne dans
#O   la variable $line.
    while read line
    do
#T    echo "Test, contenu du répertoire courant : "; ls
#T    echo "Test, lien du fichier à analyser, dernier champ de la ligne en cours (line.html): "$line.html
#O   Sélectionner et copier le nom du fichier dans la variable $Destfile
#T    echo $line
      echo $line | awk -F "/" '{print $NF}' > $Datadir/temp/file.tmp
      read Destfile < $Datadir/temp/file.tmp  
      echo "Destfile : "$Destfile
#O   Afficher la ligne complète du fichier html à analyser.
      echo "Fichier sélectionné : "$line.html

#O   Télécharger et sélectionner le contenu de la page wikibooks. 
      #cat $line.html | grep "span class" > $Destdir/temp/$Destfile.txt
      cat $line.html | grep "span class" > $Destfile.txt

#O   Réinitialiser le fichier $Destdir/temp/$Destfile avec un contenu nul
      echo -n '' > $Destdir/temp/$Destfile
#O   Recomposer toute la page en clair ... 
#O   Tant que l'on peut lire des lignes du fichier de liste Destfile.txt
#O     (qui est un fichier téléchargé du serveur wikibooks)
#P     sed /\\/\\\\/g pour éviter que le caractère '\' soit supprimé
#P     par html_data_conversion.
      while read txt
      do
        echo $txt | sed -f $Datadir/html_data_conversions | sed "s/033\[/\\\033\[/g"  >> $Destdir/temp/$Destfile
        cat $Destdir/temp/$Destfile | grep -v "div" | sed s/\'\'//g | sed s/\'#!/#!/g | sed '/^$/d' > $Destdir/src/$Destfile
      done < "$Destfile.txt"
 
#O   Création du répertoire du module et de la documentation de la commande.
      mkdir -p $Destdir/doc/$Destfile
      grep '#O' $Destdir/src/$Destfile > $Destdir/doc/$Destfile/$Destfile.algorithm
      grep '#D' $Destdir/src/$Destfile > $Destdir/doc/$Destfile/$Destfile.documentation
      grep -n -E "#H|#P|#T|#w" $Destdir/src/$Destfile > $Destdir/doc/$Destfile/$Destfile.maintainers
 
#O   Création de l'exécutable
      echo $Destfile | sed s/.sh//g > $Destdir/temp/$Destfile.tempo
      read Exename < $Destdir/temp/$Destfile.tempo
      echo "Url de l'exécutable : "$Destdir/bin/$Exename
      echo "#! /bin/bash" > $Destdir/bin/$Exename

#O   Initialiser le fichier $Destdir/doc/$Destfile/$Destfile.allcomments  
      echo -n '' > $Destdir/doc/$Destfile/$Destfile.allcomments
#O   Tant que l'on peut lire des lignes du fichier $Destdir/src/$Destfile
#O     (qui est le fichier de commandes téléchargé du serveur wikibooks)
      while read Script
      do
#O     Afficher la ligne
        if [ "${Script:0:1}" = '#' ] 
          then echo $Script >> $Destdir/doc/$Destfile/$Destfile.allcomments
          else echo $Script >> $Destdir/bin/$Exename
       fi
#O   Fin TQ Script
      done < $Destdir/src/$Destfile


#O Fin du tq shell.prj
    done < $Datadir/shell.prj
    
#O Supprimer tous les fichiers intermédiares *.txt du répertoire tmp
    rm *.txt

#O Afficher la syntaxe pour copier les fichiers dans les répertoires 
    put_syntax