;;  -*- lexical-binding: t -*-

;; emacs-symfony - emacs support for symfony framework
;; Copyright (C) 2025 Mikhail Kirillov <@w96k>

;; This file is not part of GNU Emacs

;; 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/>.

(defvar symfony-cli-binary "symfony"
  "Symfony binary file name")

(defvar symfony-cli-path "~/.emacs.d/"
  "Symfony cli path")

(defvar symfony-cli-repo "https://github.com/symfony-cli/symfony-cli"
  "Symfony cli git repository")

(defvar symfony-cli-version "v5.12.0"
  "Symfony cli version")

(defvar symfony-cli-arch "amd64"
  "Symfony cli cpu architecture")

(defvar symfony-cli-platform "linux"
  "Symfony cli cpu architecture")

(defvar symfony-cli-download-format ".tar.gz"
  "Symfony cli cpu architecture")

(defun symfony-cli--get-download-url ()
  (concat symfony-cli-repo "/releases/download/" symfony-cli-version
	  "/symfony-cli_" symfony-cli-platform
	  "_" symfony-cli-arch symfony-cli-download-format))

(defun symfony-cli--get-archive-filename ()
  (concat symfony-cli-binary symfony-cli-download-format))

(defun symfony-cli--get-archive-path ()
  (concat symfony-cli-path (symfony-cli--get-archive-filename)))

(defun symfony-cli--unpack-archive (archive-path unpack-path)
  (shell-command (concat "tar -xzf " archive-path " --directory " unpack-path "/ " symfony-cli-binary)))

(concat "tar -xzvf " (symfony-cli--get-archive-path) " " symfony-cli-binary " --directory " symfony-cli-path)

(defun symfony-cli-download ()
  "Download ready to use symfony-cli binary"
  (interactive)

  (let ((symfony-cli-url (symfony-cli--get-download-url)))
    (url-retrieve
     symfony-cli-url
     #'eww-download-callback
     (list (symfony-cli--get-archive-path) "/tmp/")))

  (symfony-cli--unpack-archive (symfony-cli--get-archive-path) symfony-cli-path))

(defun symfony-cli-compile ()
  "Download symfony-cli source and compile it"
  (interactive)
  (message "TODO"))
