Auto-éditer un wikilivre/Annexer (Version internationalisée)/users install annex.sh

Install this executable shell code in sbin ( /usr/local/sbin | ~/Add_appendix/sbin | ~/Annexer/sbin )
Installer ce code shell éxécutable dans sbin
espagnol ; Instale este código de shell ejecutable en sbin
#! /bin/bash
#H File users_install_annex.sh in ~/tmp or re-install_annex.sh in /usr/local/sbin directory
#H Codage UTF-8 sans BOM
#H Syntax : ~/Add_appendix/sbin/users_install_annex.sh [Install directory | ? ]
#H   The installation directory can be /usr/local or /usr or ~/Annexer or ~/Add_appendixby default
#P Creation date     : 2021/04/16 by GC
#P Modification date : 2021/04/18 by Clara
#P Modification date : 2021/04/25 by GC
#P Last test date    : 2021/04/19 by Clara
#P This program install or re-install the directory structure for the annex program of wikibooks.
#P

#O clean screen
    clear;
   
#O Version
    VERSION=210519
    if [ "$1" = "--v" ]; then echo "Version of users_install_annex.sh "$VERSION; exit 0; fi

#O If the first parameter '$1' is empty or '?' print syntax.  
    if [ "$1" = "?" ]; then echo "Syntax : $0 [ --v | ? | Add_appendix | Annexer | /usr/local ]"; exit 0; fi
    if [ -z $1 ]; 
    then 
      echo "Syntax ~/Tmp/users_install_annex.sh [ --v | ? | Add_appendix | Annexer | /usr/local ]"; exit 0
    fi

#P Do not use sudo
    if [ $USER = "root" ]; then echo "USER = $USER"; echo "Do not use 'sudo' please."; exit -1; fi
      
#O Go to home directory and print the current directory.
    cd ~
    echo""; echo -n "current directory = "; pwd; echo ""
    
#O If the first parameter is not empty  
    if [ $1 ];
    then 
      if [ "$1" = "/usr" ]; then Installdir=$1; echo "Installdir = /usr";
      elif [ "$1" = "/usr/local" ]; then  Installdir=$1; echo "Installdir = /usr/local";
      elif [ "$1" = "Annexer" ]; then Installdir=~/Annexer; echo "Installdir = "$Installdir
      elif [ "$1" = "Add_appendix" ]; then Installdir=~/Add_appendix; echo "Installdir = "$Installdir
      fi
    fi
    
#O Test if Install directory exist
    if [ -d $Installdir ];
    then echo " is ok, $Installdir is present."
    else echo " $Installdir not exist, i create it"; mkdir $Installdir;
    fi    

#O Create variables : Workdir, Bindir
    if [ $1 = "Add_appendix" ] && [ $1 != "Annexer" ]; then Workdir=~/Add_appendix; echo "Workdir = "$Workdir; fi
    if [ $1 = "Annexer" ] && [ $1 != "Add_appendix" ]; then Workdir=~/Annexer; echo "Workdir = "$Workdir; fi
    if [ $1 = "/usr" ] || [ $1 = "/usr/local" ]; then Workdir=~/Add_appendix; echo "Workdir = "$Workdir; fi 
    if [ -d $Workdir ]; then echo " $Workdir is present."; else echo " $Workdir is not present, i create it."; mkdir $Workdir; fi
    
    Bindir="$Installdir/bin"; echo "Bindir : "$Bindir; 
    if test -d $Bindir; 
    then echo " is ok."; 
    else echo " not present, create it"; mkdir $Installdir; 
    fi
#T Breakpoint exit 0

#O Create or recreate directory hierarchy
    cd $Workdir
    echo -n "The current directory to create $Workdir sub directories = "; pwd
    mkdir bin books datas lib man lib/src resources sbin share src temp tests vars
    cd share
    echo "$Workdir/share is the current directory to create sub share directories."; pwd
    mkdir man doc doc/html 
    mkdir -p man man/{man1,fr,fr/man1,fr.UTF8,fr.UTF8/man1,es,es/man1}
#T Breakpoint exit 0

#O If findbook.sh, ecfpb.sh, ecfpc.sh, exist in the lib/src directory, build the annex.lib file
    if test -e $Workdir/lib/src/findbook.sh && test -e $Workdir/lib/src/ecfpb.sh && test -e $Wordir/lib/src/ecfpc;
    then
      cat $Workdir/lib/src/findbook.sh | grep -v "#O\|#P\|#T" > $Workdir/lib/annex.lib
      cat $Workdir/lib/src/ecfpb.sh | grep -v "#O\|#P\|#T" >> $Workdir/lib/annex.lib
      cat $Workdir/lib/src/ecfpc.sh | grep -v "#O\|#P\|#T" >> $Workdir/lib/annex.lib
    else echo "annex.lib is not builded or not updated in $Workdir/lib/annex.lib"
    fi
     
#O If the install dir is '/usr/local' and if the number of files in /usr/local/src dir is not 9 files
#O   print copy all source files to appropriate directory
    rm -r ~/tmp; mkdir ~/tmp
    if [ "$1" = "/usr/local/" ] #if1
    then
      ls -l /usr/local/src > ~/tmp/src.txt
      wc -l ~/tmp/src.txt | cut -d ' ' -f1 > ~/tmp/nblines.txt
      read nblines < ~/tmp/nblines.txt
#T 
echo "Nblines = $nblines"
      if [ "$nblines" != "11" ] #if2
      then 
        echo " Nuber of files '$nblines' in /usr/local/src is incorrect" 
        echo " Please copy all nine annex internationalized sources files in $Workdir/src directory to update $Installdir/bin";
      fi #if-2
    fi #if-1

#T Test if  working/src dir empty   
    ls -l $Workdir/src | grep total | cut -d ' ' -f 2 > ~/tmp/n.txt
    read n < ~/tmp/n.txt
#T  echo "n = $n"
    if [ "$n" = "0" ] #i1
    then 
#T    echo " Nb files in $Workdir/src is '$n'"
      echo; echo " Directory $Workdir/src is empty"
      echo " If you want install or update $Installdir/bin directory, copy all ten annex internationalized sources files in $Workdir/src directory";
    else
      ls -l $Workdir/src > ~/tmp/src.txt
      wc -l ~/tmp/src.txt | cut -d ' ' -f1 > ~/tmp/nblines.txt
      read nblines < ~/tmp/nblines.txt
      if [ "$nblines" != "11" ] #i2
      then
        echo " Number of files '$nbline' in /usr/local/src is incorrect" 
        echo " Please copy all ten annex internationalized sources files in $Workdir/src directory to update bin directory"
      else
        echo " Want you update $Bindir files ?"
        read -s -n1 -pinkey  inkey;echo;
        if [ "$inkey" = 'y' ]; #if3
        then echo;
#O     *** Copy src files from install directory to bin directory
#P     1-annex.lib,2-annex.sh,3-header.inc.sh,4-list.sh,5-dowload.sh,6-add_sclt.sh,7-add_sclic.sh,8-add_sclip.inc.sh,9-sclipco.sh,10-sclipwb.sh
#P     Make list files.pj 
        echo "Make files list from src directory in files.pj"
        ls $Installdir/src/*.sh > files.pj

#P     If files.pj is not empty copy all files *.sh to executable directory
        if [ -s "files.pj" ] #i4
        then
          echo "files in files.pj :"
          cat files.pj
        else
         echo "files.pj is empty, exit from cp-src2bin.sh"
        exit 1
        fi #i-4
#P     Copy copy without extension .sh, and make files executables
        while read line
        do
#T       echo "copy the ligne $line from files.pj '.sh' in the file exename without extension .sh"
         echo $line | sed s/.sh//g | awk -F "/" '{print $NF}'> exename.txt
#P     Affect the value in 'exename.txt' at the variable $Exename
        read Exename < exename.txt
        echo "copy $Installdir/src/$Exename.sh to $Installdir/bin/$Exename"
        cat $Installdir/src/$Exename.sh | grep -v "#O\|#P\|#T" >  $Installdir/bin/$Exename
        echo "Add mode 755 'executable' at file $Installdir/bin/$Exename"
        chmod 755 $Installdir/bin/$Exename
        echo "----"
        done < files.pj
#T Breakpoint exit 0

#P clean sbin directory
        rm files.pj pwd.txt exename.txt
       fi #i-3
      fi #i-2
    fi #i-1
      
    rm $Workdir/vars/installdir.var
    echo "Installdir=$Installdir" > $Workdir/vars/installdir.var
    echo "Workdir=$Workdir" >> $Workdir/vars/installdir.var    
    echo; echo "Content of file $Workdir/vars/installdir.var"
    cat $Workdir/vars/installdir.var
	
	if [ $1 = "Annexer" ] && [ $n = "0" ]; 
	then 
	  echo; echo "First param = $1"
	  echo; echo "Se connecter à l'url :"
	  echo "https://fr.wikibooks.org/wiki/Autoéditer_un_wikilivre/Annexer_(Version_internationalisée)#src.orig_en_français"
	  echo "Copier les pages de codes, les coller dans un éditeur de texte et les enregistrer dans les répertoires spécialisés"
	  echo "Les fichiers : "
	  echo " sources, dans le répertoire $1/src"
	  echo " de données dans $1/datas"
	  echo " de librairies, dans $1/lib"
	  echo " d'exécutables de maintenance, dans  $1/sbin (chmod 755 * dans sbin)"
	  echo " de documentation dans $1/share/doc"
	  echo " de manuels dans $1/share/man"
	  echo; echo "Finalement rééxécuter cette commande $1/sbin/users_install_annex.sh"
	fi
	  
	if [ "n" = "0" ] && [ $1 = "Add_appendix" ] || [ "$1" = "/usr/local" ]
	then 
	  echo; echo "First param = $1"
	  echo; echo "Login to url :"
	  echo "https://fr.wikibooks.org/wiki/Autoéditer_un_wikilivre/Annexer_(Version_internationalisée)#Codes_sources_(internationalized_source_code)"
      echo "Copy code pages in memory, paste them in a text editor and save in the specialized directory"
	  echo "The files :"
	  echo " sources in the directory Add_appendix/src"
	  echo " datas in Add_appendix/datas"
	  echo " library in Add_appendix/lib"
	  echo " executable for maintenance, in Add_appendix/sbin (chmod 755 * in sbin)"
	  echo " documentation in Add_appendix/share/doc"
	  echo " manuals in Add_appendix/share/man"
	  echo; echo "Finaly reexecute this command Add_appendix/sbin/users_install_annex.sh"
	  if [ "$1" = "/usr/local" ]; then echo "copy all files and directories to /usr/local :"; echo "  sudo cp -R Add_appendix/* $1/.";fi
	fi

#O Clean tmpfiles
    rm -r ~/tmp