#!/bin/sh -

# USAGE: 
# without argument defaults to `code`
# with single argument giving the name of the clone of code. 
# Currently we use `codium` also.
# The application shall be named codeXXX or codiumXXX, 
# depending on the location of config files. 
# other names are not allowed. 

# 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.

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


case $# in 
  0)
  code="code"
  ;;
  1)
  code=$1
  ;;
  *)
  echo "Expected 0 or 1 arguments found $#."
  exit 1
esac
echo $code

case "$code" in
    code*)
        CONFIG_DIR="$HOME/.config/Code"
        ;;
    codium*)
        CONFIG_DIR="$HOME/.config/VSCodium"
        ;;
    *)
        echo "Error: Unknown application '$code'. Name must start with 'code' or 'codium'."
        exit 1
        ;;
esac

# 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 
    $code --force --install-extension "$ext" --disable-gpu
done



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"
