#!/bin/sh -



# The following commands control vs code's extension manager: 
# Explanation 
# code --extensions-dir <dir>
#     Set the root path for extensions.
# code --list-extensions
#     List the installed extensions.
# code --show-versions
#     Show versions of installed extensions, when using --list-extension.
# code --install-extension (<extension-id> | <extension-vsix-path>)
#     Installs an extension.
# code --uninstall-extension (<extension-id> | <extension-vsix-path>)
#     Uninstalls an extension.
# code --enable-proposed-api (<extension-id>)
#     Enables proposed API features for extensions. 
#     Can receive one or more extension IDs to enable individually.



show_help()
{
    cat <<EOF
Install and update VS Code / VSCodium extensions for the invoking user.

Usage:
  $(basename "$0") [editor] [options]

Editor:
  code*       use a VS Code variant
  codium*     use a VSCodium variant

Options:
  --help
      show this help

  --node-options="OPTIONS"
      pass options to embedded Node.js runtime via NODE_OPTIONS as e. g. 
      "--trace-deprecation --trace-warnings --inspect"

Examples:
  $(basename "$0")
  $(basename "$0") codium
  $(basename "$0") codium --node-options="--trace-deprecation"
EOF
}

# default if not given explicitly as a parameter 
codeEditor="code"
node_options=""

while [ $# -gt 0 ]; do
  case "$1" in
    --help)
      show_help
      exit 0
      ;;

    --node-options=*)
      node_options="${1#*=}"
      ;;

    code*|codium*)
      codeEditor="$1"
      ;;

    -*)
      echo "Unknown option '$1'; try '$0 --help'."
      exit 1
      ;;

    *)
      echo "Unexpected argument '$1'; try '$0 --help'."
      exit 1
      ;;
    esac
    shift
done
# Here, codeEditor and node-options are set. 

echo "Installing/updating extensions for '$codeEditor'..."

# In the list 'extensions': 
# The label VSX means hat the according extension is also available on VSX 
# so without any problem can be installed by codium also. 

# The fully qualified name of an extension has the form `publisherId.extensionId`. 
# The extensionId is unique for that publisher. 
# Since the publisherId is also unique, so is the fully qualified name. 
# For a full identification only the <version> is needed. 
# This is either `latest` for the currently latest version 
# or it is a concrete <versionId>
# The according path has the form 
# $ROOT/<publisherId>/<extensionId>/<version>/file/<publisherId>.<extensionId>.vsix

# for the harvester: 
# curl -fL "https://open-vsx.org/api/$PUB/$EXT/$VER/file" -o "$PUB.$EXT.vsix"
# https://marketplace.visualstudio.com/_apis/public/gallery/publishers/$PUB/vsextensions/$EXT/$VER/vspackage
#ROOT="https://open-vsx.org/api/"


echo "latex and friends"


extensions=(
# latex and friends 
"james-yu.latex-workshop" # VSX small part works online only. 
#https://open-vsx.org/api/james-yu/latex-workshop/latest/file/james-yu.latex-workshop.vsix
# lua 
# [lua]: Couldn't find message for key config.runtime. ...
"sumneko.lua" # VSX
#code --force --install-extension LuaLS.lua-language-server

# perl (e.g. to configure latexmk)
#"d9705996.perl-toolbox"
"bscan.perlnavigator" # VSX

"andreaalberti.latex-log-syntax-highlighter" # asked at https://github.com/alberti42/latex-log-syntax-vscode/issues/1

# bib
#"phr0s.bib" # covered by james-yu.latex-workshop
"twday.bibmanager" # asked at https://github.com/twday/vscode-bibmanager/issues/28 seems to be inactive but wrote an email 
#"zfscgy.bibtex-helper"# covered by james-yu.latex-workshop and jabref 
# This is more like jabref.. so not usable offline. 
#"matthiasschedel.bibtex-manager" # asked at https://github.com/MattthiasSchedel/bibtex-manager/issues/1 and by email 


# nothing found for tikz
# metapost 
"fjebaker.vscode-metapost" # VSX
# gnuplot is separate below 
# TBD: zotero

# gnuplot
"marioschwalbe.gnuplot" # did not find contact maybe https://www.linkedin.com/in/mario-schwalbe-15a300161/

# svg
#"jock.svg" # VSX but very simple 
#"simonsiefke.svg-preview" # more mature that jock but archived 
"sndst00m.vscode-native-svg-preview" # VSX


# spell and grammar checker 
"ltex-plus.vscode-ltex-plus" # VSX
# the following is outdated 
#"valentjn.vscode-ltex"



# pdf
# incompatible with latex workshop
#"tomoki1207.pdf"
# compatible with latex workshop but superfluous 
#"mathematic.vscode-pdf"

# ps and eps 
"mxschmitt.postscript" # asked at https://github.com/mxschmitt/vscode-postscript/issues/4 and per email 
#"ahnafnafee.postscript-preview" # VSX pure previewer, not useful 
"yzane.markdown-pdf" # VSX
)

for ext in "${extensions[@]}"; do
  # --disable-gpu, to save load on X-Server 
  NODE_OPTIONS=$node_options "$codeEditor" --force --install-extension "$ext" --disable-gpu
done

echo "...Installed/update extensions for '$codeEditor'."


#DB_STATE="$CONFIG_DIR/User/globalStorage/state.vscdb"




# # TBD: move to general script. 
# case "$code" in
#     "code")
#         CONFIG_DIR="Code"
#         ;;
#     "codium")
#         CONFIG_DIR="VSCodium"
#         ;;
#     *)
#         echo "Error: Expected 'code' or 'codium' but found '$code'. "
#         exit 1
#         ;;
# esac
# # Here, or as a policy 
# # {
# #   "extensions.ignoreRecommendations": true,
# #   "extensions.showRecommendationsOnlyOnDemand": true,
# #   "workbench.startupEditor": "none"
# # }

# # Define the path for VSCodium's settings.json on Linux
# SETTINGS_FILE="$HOME/.config/$CONFIG_DIR/User/settings.json"

# # Ensure the directory exists
# mkdir -p "$(dirname "$SETTINGS_FILE")"

# # Create an empty JSON object if the file does not exist
# if [ ! -f "$SETTINGS_FILE" ]; then
#     echo "{}" > "$SETTINGS_FILE"
# fi

# # Use a temporary file to safely update the JSON
# # . + { ... } merges the existing settings with our new ones
# TMP_FILE=$(mktemp)

# jq '. + {
#   "extensions.ignoreRecommendations": true,
#   "extensions.showRecommendationsOnlyOnUsage": true,
#   "workbench.startupEditor": "none"
# }' "$SETTINGS_FILE" > "$TMP_FILE" && mv "$TMP_FILE" "$SETTINGS_FILE"
