export PhoneAppHome=/Users/ntheodoropoulos/Robotical/marty-react-native # <- replace this if you want to use the buildAllAndCpToWebApp function, which builds everything and copies the build files in the phoneapp
export WebappHome=/Users/ntheodoropoulos/Robotical/marty-web-app # <- replace this if you want to use the buildAllAndCpToWebApp function, which builds everything and copies the build files in the webapp
export MBHome=/Users/ntheodoropoulos/Robotical/MartyBlocks # <- replace this

export mblib=$MBHome/marty-blocks-lib/
export REPLACEMENTS=$MBHome/marty-blocks-lib/replacements
export blocks_original=$MBHome/scratch-blocks
export vm_original=$MBHome/scratch-vm
export gui_original=$MBHome/scratch-gui
export l10n_original=$MBHome/scratch-l10n
export NODE_OPTIONS="--max-old-space-size=5120"
alias cdhome='cd $MBHome'
alias sourceMBProf='cdhome && source martyBlocksCliProfile'
export PATH=$(pyenv root)/shims:$PATH
## install python 2 and add it to path
## install Java Runtime Environment (JRE)

nvm use v16.20.1

# removeNodeModules && restoreScratch && checkoutScratch && installScratch && installMBLib && npmlinkAll && cpWebWorker && buildAllAndStart

##################### Steps to set up the project #######################

# Step 1a: Clone repos
cloneScratch() {
    git clone -b develop https://github.com/llk/scratch-blocks.git MartyBlocks/scratch-blocks
    git clone -b develop https://github.com/llk/scratch-vm.git MartyBlocks/scratch-vm
    git clone -b develop https://github.com/llk/scratch-gui.git MartyBlocks/scratch-gui
    git clone -b master https://github.com/scratchfoundation/scratch-l10n.git MartyBlocks/scratch-l10n
}

fetchScratch() {
    cd $blocks_original && git fetch
    cd $vm_original && git fetch
    cd $gui_original && git fetch
    cd $l10n_original && git fetch
}

# Step 1b: Checkout appropriate commit
checkoutScratch() {
    # cd $blocks_original && git reset --hard f9107bf5d0479d632658f2b203995f5ae6d75363
    # cd $vm_original && git reset --hard 3b36a8e3ea7f3caa5b4bf4ae7b4f821a986a1378
    # cd $gui_original && git reset --hard 738c86bb58e336711280aec33c510d7aef79408e
    cd $blocks_original && git reset --hard f210e042988b91bcdc2abeca7a2d85e178edadb2
    cd $vm_original && git reset --hard 7313ce5199f8a3da7850085d0f7f6a3ca2c89bf6
    cd $gui_original && git reset --hard 530d1ed7b8c45560f9a0580d6a00c87ecbec4721
    cd $l10n_original && git reset --hard 21fe788aed29a37ab694ca5a6bbf2acd76260f1f
}

# Step 1c: Restore scratch repos (only relevant if you have made changes on the repos and want to restore them to their original state)
restoreScratch() {
    cd $blocks_original && git restore . && git clean -fd
    cd $vm_original && git restore . && git clean -fd
    cd $gui_original && git restore . && git clean -fd
    cd $l10n_original && git restore . && git clean -fd
}

# Step 1d: Remove package-lock.json and node_modules from scratch repos
removeNodeModules() {
    cd $blocks_original && rm -rf package-lock.json node_modules
    cd $vm_original && rm -rf package-lock.json node_modules
    cd $gui_original && rm -rf package-lock.json node_modules
    cd $l10n_original && rm -rf package-lock.json node_modules
}

# Step 2: Install scratch
installScratch() {
    cd $l10n_original && npm install
    cd $blocks_original && npm install 
    cd $vm_original && npm install && npm install soundtouchjs@0.1.30
    cd $gui_original && npm install && npm install prismjs@1.29.0 && npm install --save-dev terser-webpack-plugin@4.2.0
}

# Step 3: Install marty blocks library
installMBLib() {
    cd $mblib
    npm install
}

# Step 4: Link repositories
npmlinkAll() {
    # local type="${1:-all"}
    cd $l10n_original && npm link
    cd $mblib && npm link && npm link scratch-l10n
    cd $blocks_original && npm link && npm link marty-blocks-lib scratch-l10n
    cd $vm_original && npm link && npm link marty-blocks-lib scratch-blocks scratch-l10n
    cd $gui_original && npm link marty-blocks-lib scratch-blocks scratch-vm scratch-l10n
}

# Step 4.1: Copy web worker of marty machine into scratch gui static
cpWebWorker() {
    cp $mblib/node_modules/@robotical/marty-machine-lib/dist/assets/* $gui_original/static
}

# Step 5: build all repositories and start scratch gui
buildAllAndStart () {
    # arguments [all, blocks, vm, gui]
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    # setDevelopment
    # setProduction
    cpReplacements
    removeUnusedAssets
    buildScratchStart $type $NODE_ENV
    cd $gui_original
    NODE_ENV=$NODE_ENV npm start
}

# To make changes on the project, edit the appropriate files in the /replacements 
# folder, and then run the buildAllAndStart function again.
# Note: You only need to build the repositories that you made the changes on, and their parents (based on the npm linking)
# For example, if you made a change on scratch gui, then you need to build only scratch gui because it doesn't depend on vm or blocks.
# To do so, you can run 'buildAllAndStart gui'.
# For more details, look at the buildAllAndStart function definition, and how the type argument is used to conditionally build the appropriate repositories.

##################### Helper functions ####################
buildAllAndCpToWebApp () {
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    # setDevelopment
    cpReplacements
    removeUnusedAssets
    buildScratch $type $NODE_ENV
    cpToDist
    cpToWebapp
}

buildAllAndCpToPhoneApp() {
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    # setDevelopment
    cpReplacements
    removeUnusedAssets
    buildScratch $type $NODE_ENV
    cpToPhoneApp
}

buildAllAndCpToPublic() {
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    # setDevelopment
    cpReplacements
    removeUnusedAssets
    buildScratch $type $NODE_ENV
    cpToPublic
}

setProduction () {
    cd $mblib
    npx node ./set-production.js
}

setDevelopment () {
    cd $mblib
    npx node ./set-development.js
}

cpReplacements ()
{
    cp -r $REPLACEMENTS/* $MBHome
}

removeUnusedAssets () {
    cd $gui_original
    /bin/rm -f ./src/lib/libraries/decks/thumbnails/*
    /bin/rm -f ./src/lib/libraries/decks/steps/* 
}

buildScratch () {
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    if [ $type = all ]; then buildl10n $NODE_ENV; buildBlocks $NODE_ENV; buildVm $NODE_ENV; buildGui $NODE_ENV; fi
    if [ $type = l10n ]; then buildl10n $NODE_ENV; fi
    if [ $type = blocks ]; then buildBlocks $NODE_ENV; fi
    if [ $type = vm ]; then buildBlocks $NODE_ENV; buildVm $NODE_ENV; fi
    if [ $type = gui ]; then buildGui $NODE_ENV; fi
}

buildScratchStart () {
    local type="${1:-all}"
    local NODE_ENV="${2:-development}"
    if [ $type = all ]; then buildl10n $NODE_ENV; buildBlocks $NODE_ENV; buildVm $NODE_ENV; buildGuiStart $NODE_ENV; fi
    if [ $type = l10n ]; then buildl10n $NODE_ENV; fi
    if [ $type = blocks ]; then buildBlocks $NODE_ENV; fi
    if [ $type = vm ]; then buildBlocks $NODE_ENV; buildVm $NODE_ENV; fi
    if [ $type = l10n-gui ]; then buildl10n $NODE_ENV; buildGuiStart $NODE_ENV; fi
    if [ $type = gui ]; then buildGuiStart $NODE_ENV; fi
}

cpToWebapp () {
    setopt rm_star_silent
    setopt rm_star_wait
    rm -rf $WebappHome/public/MartyBlocks/*
    cp -r $gui_original/build/* $WebappHome/public/MartyBlocks
}

cpToPhoneApp () {
    setopt rm_star_silent
    setopt rm_star_wait
    rm -rf $PhoneAppHome/assets/www/scratch-build/*
    rm -rf $PhoneAppHome/node_modules/@robotical/martyblocks/dist/*
    cp -r $gui_original/build/* $PhoneAppHome/assets/www/scratch-build/
    cp -r $gui_original/build/* $PhoneAppHome/node_modules/@robotical/martyblocks/dist
}

cpToDist () {
    setopt rm_star_silent
    setopt rm_star_wait
    cdhome
    rm -rf dist/*
    cp -r $gui_original/build/* ./dist
}

cpToPublic () {
    setopt rm_star_silent
    setopt rm_star_wait
    rm -rf $MBHome/public/*
    cp -r $gui_original/build/* $MBHome/public
}

buildl10n() {
    local NODE_ENV="${1:-development}"
    cd $l10n_original && npm run build
}

buildBlocks() {
    local NODE_ENV="${1:-development}"
    cd $blocks_original && ln -s $(npm root)/google-closure-library ../closure-library; NODE_ENV=$NODE_ENV npm run prepublish
}

buildVm() {
    local NODE_ENV="${1:-development}"
    cd $vm_original && NODE_ENV=$NODE_ENV npm run build
}

buildGui() {
    local NODE_ENV="${1:-development}"
    cd $gui_original && NODE_ENV=$NODE_ENV npm run build
}

buildGuiStart() {
    local NODE_ENV="${1:-development}"
    cd $gui_original && NODE_ENV=$NODE_ENV npm run build
}

publishPackage() {
    cdhome;
    rm -rf dist;
    mkdir dist;
    cp -r $gui_original/build/* dist/;
    npm publish;
}

restoreScratch() {
    cd $blocks_original && git restore . && git clean -fd
    cd $vm_original && git restore . && git clean -fd
    cd $gui_original && git restore . && git clean -fd
}
