(*
    Script de copie d'un dictionnaire Kobo dans le(s) bon(s) dossier(s)
    @Ange - 16 Août 2023
    ! : pas de vérification qu'il s'agit bien d'un dictionnaire
    sources dico Wikitionniaire :
    * https://www.tiger-222.fr/?d=2020/04/17/22/14/21-un-dictionnaire-alternatif-et-complet-pour-votre-kobo
    * https://github.com/BoboTiG/ebook-reader-dict/tree/master/docs/fr
*)
-- La vraie fonction de copie
on install_dico(nom, nommodifie, chemin)
    try
        do shell script "cp " & nom & " " & chemin & nommodifie
        -- display dialog "cp " & nom & " " & chemin & nommodifie
    on error the error_message number the error_number
        display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
    end try
    -- display dialog "nom : " & nom
    set liste to every word of nom as list
    set nomcourt to item -2 of liste & "-" & item -1 of liste
    -- display dialog "nomcourt : " & nomcourt
    return nomcourt
end install_dico
-- fonctions pour modifier le nom
on findAndReplace(tofind, toreplace, TheString)
    set ditd to text item delimiters
    set text item delimiters to tofind
    set textItems to text items of TheString
    set text item delimiters to toreplace
    if (class of TheString is string) then
        set res to textItems as string
    else -- if (class of TheString is Unicode text) then
        set res to textItems as Unicode text
    end if
    set text item delimiters to ditd
    return res
end findAndReplace
on modifie_nom(nomdico)
    -- display dialog "nomdico: " & nomdico
    set nom_modifie to findAndReplace("-fr-fr", "-fr", nomdico)
    -- display dialog "nomdico: " & toto
    return nom_modifie
end modifie_nom
-- 0) Initialisation Valeurs
set chemincustom to "/Volumes/KOBOeReader/.kobo/custom-dict/"
set cheminbase to "/Volumes/KOBOeReader/.kobo/dict/"
-- 1) vérification présence Kobo
try
    do shell script "ls " & chemincustom
on error the error_message number the error_number
    display dialog "Erreur " & the error_number & " : aucune Kobo connectée, merci d'en connecter une !" buttons {"Quitter"} default button 1
    return
end try
-- 2) sélection du fichier
display dialog "Sélectionner le fichier dictionnaire (ex: dicthtml-fr-fr.zip)"
set nomdico to (choose file)
set dico to "\"" & POSIX path of nomdico & "\""
-- display dialog "nom ref : " & dico
-- vérification extension zip
set liste to every word of dico as list
set val to (count of liste)
set nomcourt to last item of liste
set listelettres to every character of nomcourt as list
set extension to item -3 of nomcourt & item -2 of nomcourt & item -1 of nomcourt
-- display dialog "val : " & val & " extension:" & extension
if extension is not "zip" then
    display dialog "Le fichier doit être un fichier zip" buttons {"Quitter"} default button 1
    return
end if
-- 3) Demander si installation du dico utilisateur(0) ou dico kobo(1) ou les deux (2)
set nomdicocourt to item -3 of liste & "-" & item -2 of liste & "-" & item -1 of liste
display dialog "Souhaitez-vous installer \"" & nomdicocourt & "\" :
  1) uniquement comme dictionnaire perso ?
  2) ou également comme dictionnaire de base Kobo ? " buttons {"1", "2", "Annuler"} default button 3
set the button_pressed to the button returned of the result
-- 4) Actions
set Info to ""
set InfoS to ""
if the button_pressed is "Annuler" then
    return
else if the button_pressed is "2" then -- dico Kobo
    set nommodifie to modifie_nom(nomdicocourt)
    set Info to " & " & install_dico(dico, nommodifie, cheminbase)
    set InfoS to "s"
end if
set nomcourt to install_dico(dico, "", chemincustom)
-- 5) Affichage résultat
display dialog "Dictionnaire" & InfoS & " " & nomdicocourt & Info & " installé" & InfoS buttons {"Quitter"} default button 1