#!/bin/sh
# Installation script for Arch and Debian (also WSL)
# run script:
#   sh install.sh

# * Manual Installation
#   Install into =~/.config/emacs= (must be empty)
#     git clone http://gogs.weseng.de/daniel/emacs.git ~/.config/emacs

#   or copy all files of this directory into your ~/.config/emacs directory
#   - for Emacs version < 27 create symbolic link to the init file
#   - =ln -s ~/.config/emacs/init ~/.emacs= (or =~/.emacs.el=,
#     =~/.emacs.d/init.el=)

#   LaTeX sytling files: copy/merge the =texmf= directory into your home
#   directory =~/texmf=

#   See package requirements inside the code


# INTERNAL
distro=$(lsb_release -is)
if test $distro = "ArcoLinux"; then distro=Arch; fi
# echo $distro

echo "install"
case $distro in
  Arch)
    echo "Arch found"
    echo "Emacs installation"
    yay -Syu --needed --answerclean y --answerdiff n emacs-git \
        ttf-inconsolata ttf-opensans noto-fonts-cjk \
        aspell aspell-de aspell-en \
        hunspell hunspell-de hunspell-en_US \
        languagetool \
        biber bibtex2html texlive-bibtexextra texlive-core \
        texlive-fontsextra texlive-formatsextra texlive-langgreek \
        texlive-langjapanese texlive-latexextra texlive-pgf-pie \
        texlive-pgfopts texlive-pictures texlive-pstricks \
        texlive-publishers texlive-science texlive-slashbox \
        ledger \
        python-pylint \
        git nodejs aria2 libreoffice wmctrl xdotool jre-openjdk \
        python-pyqt5 python-pyqt5-sip python-pyqtwebengine python-qrcode \
        python-feedparser python-dbus python-pyinotify python-markdown \
        python-grip filebrowser-bin python-epc mupdf \
        ditaa plantuml r
    # workaround for python-pymupdf see below
    ;;

  Debian)
    echo "Debian found"
    echo "Emacs installation"
    sudo apt-get update
    # sudo apt-get install emacs  # version too old
    # emacs-snapshot https://www.emacswiki.org/emacs/EmacsSnapshotAndDebian
    # To enable adding a private repository.
    sudo apt-get install -y software-properties-common wget
    # To get the Apt signing key of emacs-snapshot
    wget -q http://emacs.ganneff.de/apt.key -O- | sudo apt-key add
    # Replace Buster with your version. check with: lsb_release -a; # repos are listed inside /etc/apt/sources.list
    sudo add-apt-repository "deb http://emacs.ganneff.de/ buster main"

    echo "Packages used by Emacs"
    # language tool via snap
    sudo apt-get update
    sudo apt-get install -y emacs-snapshot \
         fonts-inconsolata fonts-noto-cjk-extra \
         aspell aspell-en aspell-de hunspell \
         hunspell-en-us hunspell-de-de snapd \
         biber bibtex2html texlive-bibtex-extra texlive-base \
         texlive-fonts-extra texlive-formats-extra texlive-lang-greek \
         texlive-lang-japanese texlive-latex-extra texlive-luatex \
         texlive-pictures texlive-pstricks \
         texlive-publishers texlive-science \
         ledger \
         python3-pylint-common \
         git nodejs aria2 libreoffice wmctrl xdotool default-jre \
         python3-pyqt5 python3-pyqt5.qtwebengine python3-qrcodegen \
         python3-feedparser python3-dbus python3-pyinotify python3-markdown \
         python3-epc mupdf \
         ditaa plantuml r-base
    # TODO: not found: ttf-opensans
    #   texlive-pgf-pie texlive-pgfopts texlive-slashbox
    #   python-pyqt5-sip python-grip filebrowser-bin
    # TODO: snap not supported for wsl
    # https://wiki.ubuntuusers.de/LanguageTool/
    sudo snap install core
    sudo snap install languagetool
    # Update Alternatives to ensure it uses the snapshot version of emacsclient.
    echo "[NOTE] you may update alternatives of emacs and emacsclient."
    echo "To list run:"
    echo "  update-alternatives --list emacs"
    echo "  update-alternatives --list emacsclient"
    echo "To update run:"
    echo "  sudo update-alternatives --config emacsclient"
    ;;
  *)
    echo "unknown: \"${distro}\""
    ;;
esac

echo "config"
case $distro in
  Arch|Debian)
    folder="${HOME}/.config/emacs"
    mkdir -p $folder
    echo "git clone"
    # shallow clone
    if ! git clone --depth 1 http://gogs.weseng.de/daniel/emacs.git "${folder}" 2>/dev/null && test -d "${folder}" ; then
      echo "clone failed because the folder ${folder} exists"
    fi

    cp -r ~/.config/emacs/scripts/texmf ~/
    ;;
  *)
    ;;
esac

echo "post install"
case $distro in
  Arch)
    # workaround for python-pymupdf
    folder="${HOME}/.cache/yay/python-pymupdf.custom"
    rm -rf "$folder"
    mkdir -p "$folder"
    cd "$folder"
    git clone https://aur.archlinux.org/python-pymupdf.git .
    # same as, but will create a folder same as package: yay -G python-pymupdf
    patch --forward --strip=1 --input="${HOME}/.config/emacs/scripts/fix-pymupdf-library-linking-for-arcolinux-PKGBUILD.patch"
    # yay -S --mflags "-p PKGBUILD" --answerclean y --answerdiff n python-pymupdf
    makepkg -si
    ;;
esac


#
# Microsoft
#
# NOT NEEDED AT THE MOMENT
# kernel_release=$(uname -r)
# check if system is Microsoft and what distro is used
# if test $(echo $kernel_release | grep -i microsoft); then
#   # remove control characters ^@ ^M, (otherwise will be D^@e^@b^@i^@a^@n^@^M^@ ^@) see with: echo $distro | cat -v
#   # distro=$(/mnt/c/Windows/System32/wsl.exe --list --running -q | tr -d "[:cntrl:]")
#   distro=$(lsb_release -is)
# fi
# echo $distro
