;; license.el -- Pamhyr
;; Copyright (C) 2023-2026  INRAE
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;; -*- coding: utf-8 -*-

(defun pamhyr-current-filename ()
  (car (last (file-name-split (buffer-file-name)))))

(defun pamhyr-insert-license ()
  (interactive)
  (let ((filename (pamhyr-current-filename)))
    (insert (format
             "# %s -- Pamhyr
# Copyright (C) 2026  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-
" filename))))

(defun pamhyr-update-license ()
  (interactive)
  (let ((pos (point)))
    (goto-char (point-min))
    (while (re-search-forward "Copyright (C) 2023-2025  INRAE" nil t)
      (replace-match "Copyright (C) 2023-2026  INRAE"))
    (while (re-search-forward "Copyright (C) 2023  INRAE" nil t)
      (replace-match "Copyright (C) 2023-2026  INRAE"))
    (while (re-search-forward "Copyright (C) 2024  INRAE" nil t)
      (replace-match "Copyright (C) 2024-2026  INRAE"))
    (while (re-search-forward "Copyright (C) 2025  INRAE" nil t)
      (replace-match "Copyright (C) 2025-2026  INRAE"))
    (goto-char pos)))

(defun pamhyr-root-dir-rec-aux (dir)
  (if (or (equal dir "")
         (equal dir "/")
         (equal dir nil))
      nil
    (let ((ndir (directory-file-name (file-name-directory dir))))
      (if (file-directory-p (concat ndir "/.git"))
          ndir
        (pamhyr-root-dir-rec-aux ndir)))))

(defun pamhyr-root-dir ()
  (pamhyr-root-dir-rec-aux (buffer-file-name)))

(defun pamhyr--update-license (file)
  (message (format "Update: %s" file))
  (let ((buffer (find-file file)))
    (with-current-buffer buffer
      (pamhyr-update-license)
      (save-buffer))
    ;(kill-buffer buffer)
    ))

(defun pamhyr--insert-license (file)
  (message (format "License: %s" file))
  (let ((buffer (find-file file)))
    (with-current-buffer buffer
      (goto-char (point-min))
      (pamhyr-insert-license)
      (save-buffer))
    ;(kill-buffer buffer)
    ))

(defun pamhyr-update-all-license ()
  (interactive)
  (let* ((root (pamhyr-root-dir))
         (files-with-copyright
          (split-string
           (shell-command-to-string
            (format "git -C '%s' grep -e 'Copyright (C)'|cut -d ':' -f 1|uniq|grep [.]py"
                    root))
           "\n"))
         (files
          (split-string
           (shell-command-to-string
            (format "git -C '%s' ls-files | grep [.]py"
                    root))
           "\n"))
         (files-without-copyright
          (seq-filter (lambda (file)
                        (not (member file files-with-copyright)))
                      files)))
    (message (format "%s" files-with-copyright))
    (mapcar 'pamhyr--update-license (mapcar (lambda (file) (concat root "/" file))
                                            files-with-copyright))

    (message (format "%s" files-without-copyright))
    (mapcar 'pamhyr--insert-license
            (mapcar (lambda (file) (concat root "/" file))
                    files-without-copyright))))

(defvar pamhyr-mail-template "Dear users,

✨ The version @version of Pamhyr2 is available.

<NEWS>

---📄--Change-logs-------------@version---
@description
------------------------------------------

<FUTURE WORK>

---💡--Useful-links-----------------------
  Download this version :
    https://gitlab.irstea.fr/theophile.terraz/pamhyr/-/releases/@version

  The documentation :
    https://gitlab.irstea.fr/theophile.terraz/pamhyr/-/wikis/home

  Repport an issue :
    https://gitlab.irstea.fr/theophile.terraz/pamhyr/-/issues
  or by email at :
    <theophile.terraz@inrae.fr>
------------------------------------------

---⚠️--Warning---------------------------

  For Windows users: Some antivirus programs may detect Pamhyr2
  as a virus 🦠, but this is a false positive. The problem is
  known and comes from the executable generated by PyInstaller.
  We don't yet have a solution to this problem.  If this is your
  case, you'll need to add an exception to your antivirus
  software if you want to use Pamhyr2. Alternatively, you can use
  WSL (Windows Subsystem for Linux) and use the Linux version on
  Windows.

  Antivirus analysis : <TOTAL VIRUS REPORT>

  For Linux 🐧 users: Some systems are not compatible with the
  compiled version, in which case you'll have to go straight to
  the source code (see the documentation for more details).

------------------------------------------

Br,
")

(require 'web)
(require 'json)

(defun pamhyr-release-mail (release)
  (interactive "sRelease name: ")
  (web-http-get
   (lambda (httpc header my-data)
     (let* ((data (json-read-from-string my-data))
            (release (cdr (assoc 'tag_name data)))
            (description (cdr (assoc 'description data))))
       (let ((buffer (generate-new-buffer (format "* mail-%s *" release))))
         (with-current-buffer buffer
           (insert
            (string-replace "@description" description
                            (string-replace "@version" release
                                            pamhyr-mail-template)))
           (set-buffer buffer)))))
   :url (concat "https://gitlab.irstea.fr/api/v4/projects/2779/releases/" release)))
