
#!/usr/bin/env bash

#
# this script is used to customize the created wp-env instance
#

WPENV_INSTALLPATH="$(realpath --relative-to $(pwd) $(pnpm run --silent wp-env install-path))"

# # build gutenberg for development (for debugging purposes - i.e. including source maps etc.)
# pushd "${WPENV_INSTALLPATH}/gutenberg"
# # compute path to our configured nodejs provided by pnpm environment
# NODE_PATH="$(dirname $(which node))/.."
# # calling the original npm delivered with the configured nodejs version is a bit quirky but works :-)
# $NODE_PATH/bin/node "$NODE_PATH/lib/node_modules/npm/bin/npm-cli.js" ci
# $NODE_PATH/bin/node "$NODE_PATH/lib/node_modules/npm/bin/npm-cli.js" run build
# popd

# # build gutenberg for development (for debugging purposes - i.e. including source maps etc.)
# pushd "${WPENV_INSTALLPATH}/gutenberg"
# # compute path to our configured nodejs provided by pnpm environment
# NODE_PATH="$(dirname $(which node))/.."
# # calling the original npm delivered with the configured nodejs version is a bit quirky but works :-)
# $NODE_PATH/bin/node "$NODE_PATH/lib/node_modules/npm/bin/npm-cli.js" ci
# $NODE_PATH/bin/node "$NODE_PATH/lib/node_modules/npm/bin/npm-cli.js" run build:packages
# # build gutenberg in development mode
# ./node_modules/.bin/wp-scripts start --no-watch
# # build static gutenberg storybook
# $NODE_PATH/bin/node "$NODE_PATH/lib/node_modules/npm/bin/npm-cli.js" run storybook:build
# popd

# remove dolly demo plugin
rm -f $WPENV_INSTALLPATH/{tests-WordPress,WordPress}/wp-content/plugins/hello.php

for prefix in '' 'tests-' ; do
  # this wp-cli configuration file needs to be created to enable wp-cli to work with the apache mod_rewrite module
  pnpm run wp-env run ${prefix}cli sh -c 'echo -e "apache_modules:\n  - mod_rewrite" > /var/www/html/wp-cli.yml'

  # The wp rewrite flush command regenerates the rewrite rules for your WordPress site, which includes refreshing the permalinks.
  pnpm run wp-env run ${prefix}cli wp --quiet rewrite flush
  # The wp rewrite structure command updates the permalink structure. --hard also updates the .htaccess file
  pnpm run wp-env run ${prefix}cli wp --quiet rewrite structure '/%postname%' --hard

  # activate our theme
  pnpm run wp-env run ${prefix}cli wp --quiet theme activate extended-global-styles

  # deactivate the deprecated cpt plugin
  pnpm run wp-env run ${prefix}cli wp --quiet plugin deactivate cpt

  # Updates an option value for example the value of Simple page is id = 2
  pnpm run wp-env run ${prefix}cli wp option update page_on_front 2
  # Update the page as front page by default.
  pnpm run wp-env run ${prefix}cli wp option update show_on_front page
done

function plugins {
  for PLUGIN in $(find packages/wp-content/plugins -mindepth 1 -maxdepth 1 -type d -printf '%f\n'); do
    echo "        \"/var/www/html/wp-content/plugins/${PLUGIN}\":\"\${workspaceFolder}/packages/wp-content/plugins/${PLUGIN}\","
  done
}

function themes {
  for THEME in $(find packages/wp-content/themes -mindepth 1 -maxdepth 1 -type d -printf '%f\n'); do
    echo "        \"/var/www/html/wp-content/themes/${THEME}\":\"\${workspaceFolder}/packages/wp-content/themes/${THEME}\","
  done
}

# generate launch configuration
cat << EOF > '.vscode/launch.json'
{
  // THIS FILE IS MACHINE GENERATED by .wp-env-afterStart.sh - DO NOT EDIT!
  // If you need to confgure additional launch configurations consider defining them in a vscode *.code-workspace file
  "version": "0.2.0",
  "configurations": [
    {
      "name": "wp-env",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "stopOnEntry": false, // set to true for debugging this launch configuration
      "log": false,         // set to true to get extensive xdebug logs
      "pathMappings": {
$(plugins)
$(themes)
      //  "/var/www/html/wp-content/plugins/gutenberg": "\${workspaceFolder}/${WPENV_INSTALLPATH}/gutenberg",
        "/var/www/html": "\${workspaceFolder}/${WPENV_INSTALLPATH}/WordPress",
      }
    }
  ]
}
EOF

# generate settings.json
cat << EOF > '.vscode/settings.json'
{
  "intelephense.files.exclude": [
    "**/.git/**",
    "**/.svn/**",
    "**/.hg/**",
    "**/CVS/**",
    "**/.DS_Store/**",
    "**/node_modules/**",
    "**/bower_components/**",
    "**/vendor/**/{Tests,tests}/**",
    "**/.history/**",
    "**/vendor/**/vendor/**",
    "**/dist/**",
    "**/buid/**",
  ],
  "search.exclude": {
    "**/node_modules": true,
    "**/build/**" : true,
    "**/build-module/**" : true
  },
  "intelephense.environment.includePaths": [
    "${WPENV_INSTALLPATH}/WordPress"
  ],
  "git.autoRepositoryDetection": false,
  "json.schemas": [
    {
      "fileMatch": ["jsonschema.json", "*schema.json"],
      "url": "https://json-schema.org/draft/2019-09/schema"
    },
    {
      "fileMatch": ["tsconfig.json"],
      "url": "https://json.schemastore.org/tsconfig"
    }
  ],
  "[html]": {
    "editor.formatOnSave": false
  }
}
EOF
