#!/usr/bin/env bash
# ----------------------------------------------------------------------
#  INITIALIZE the HAMACS SYSTEM
# ----------------------------------------------------------------------

HAMACS_DIR=$(cd "$(dirname "$0")"; pwd)
HAMACS_DEST=$HOME/.emacs.d

mkdir -p $HAMACS_DEST

for LINK in snippets templates elisp
do
    rm -rf $HAMACS_DEST/$LINK
    ln -s --force $HAMACS_DIR/$LINK $HAMACS_DEST
done

cat > $HAMACS_DEST/init.el <<EOF
;;; init.el --- Hamacs Init -*- lexical-binding: t; -*-
;;
;;; Commentary:
;;
;; This is my Emacs Bootloader. Simply put, I initialize the package management
;; system, and then tangle my literate files. This simple idea came from
;; https://github.com/susamn/dotfiles
;;
;;; Code:

;; We'll be using straight. So, we don't want duplicated package loading:
(setq package-enable-at-startup nil)

;; While I would rather program my configurations, sometimes the Emacs
;; menu system is “good enough”, but I want it in its own file:

(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(when (file-exists-p custom-file)
  (load custom-file))

;; While this path should come from the shell, there seems to be a
;; condition where I need to set this so that Emacs can pull our my
;; compiled files.

(add-to-list 'exec-path "/usr/local/bin")

(setenv "PATH" (concat "/usr/local/bin:" (getenv "PATH")))
(defvar hamacs-source-dir "$HAMACS_DIR" "Where we be.")

;; Configure straight https://github.com/raxod502/straight.el#getting-started

(defvar bootstrap-version)

(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

(straight-use-package 'use-package)

;; While that enables the :straight t extension to use-package, let's just have that be the default:
(use-package straight
  :custom (straight-use-package-by-default t
           straight-default-vc 'git))

;; See the details in https://dev.to/jkreeftmeijer/emacs-package-management-with-straight-el-and-use-package-3oc8

;; Download and use the latest version of org:

(use-package org)  ; We'll configure this in ernest later.

;; Let's rock:
(org-babel-load-file "$HAMACS_DIR/bootstrap.org")

(provide 'init)
;;; init.el ends here
EOF

echo Created $HAMACS_DEST/init.el