#!/bin/bash
set -e

## Ensure script is run from the root directory
if [ ! -d "debian" ]; then
    echo "Error: This script must be run from the root directory of the project (debian folder not found)"
    exit 1
fi

## Ensure that patches in debian/patches are applied
quilt applied | grep debian-vue-apps.diff || {
    echo "Error: The patch debian-vue-apps.diff has not been applied. Please apply it using quilt before running this script."
    exit 1
}

## Helper function to download javascript files with error handling
download_javascript() {
    local url="$1"
    local file="$2"
    file="${file%.min.js}"
    file="${file%.js}"
    rm -f ${file}.js
    rm -f ${file}.min.js
    curl "${url}" -Lo ${file}.js
    if head -n1 "${file}.js" | grep -q '^Not found'; then
        echo "Error: Failed to download ${file}.js from ${url}."
        exit 1
    fi
}

## Helper function to ask for confirmation
confirm() {
    # Usage: confirm "Your message"
    # Returns 0 (true) on yes, 1 (false) otherwise.

    local prompt="$1"
    local reply

    read -r -p "$prompt [y/N] " reply

    case "$reply" in
        [yY]|[yY][eE][sS]) return 0 ;;
        *) return 1 ;;
    esac
}

## Print version of npm
echo "Checking npm version (if an error occurs here, please run apt install npm first)"
npm --version

## Work on vue2-app
rm -rf debian/vue-apps/vue2-app
pushd vue2-app

# Extract coi-serviceworker versions
COI_SERVICEWORKER_VERSION=$(head -1 public/coi-serviceworker.min.js | grep "coi-serviceworker v" | awk '{sub(/^v/, "", $3); print $3}')
echo "coi-serviceworker version: $COI_SERVICEWORKER_VERSION"
confirm "Does this version make sense?"

# Unminify javascript dependencies
download_javascript https://unpkg.com/coi-serviceworker@${COI_SERVICEWORKER_VERSION}/coi-serviceworker.js public/coi-serviceworker.js

# Drop public/vue.global.js, which will be replaced by vue.min.js from node-vue
rm public/vue.global.js

# Build app, as reported in the README.md file
npm install
npm run build

# End vue2-app work
popd

## Work on vue3-app
rm -rf debian/vue-apps/vue3-app
pushd vue3-app

# Extract vue and coi-serviceworker versions
VUE_VERSION=$(head -3 public/vue.global.js | grep "vue v" | awk '{sub(/^v/, "", $3); print $3}')
COI_SERVICEWORKER_VERSION=$(head -1 public/coi-serviceworker.min.js | grep "coi-serviceworker v" | awk '{sub(/^v/, "", $3); print $3}')
echo "Vue version: $VUE_VERSION"
echo "coi-serviceworker version: $COI_SERVICEWORKER_VERSION"
confirm "Do these versions make sense?"

# Unminify javascript dependencies
download_javascript https://unpkg.com/vue@${VUE_VERSION}/dist/vue.global.js public/vue.global.js
download_javascript https://unpkg.com/coi-serviceworker@${COI_SERVICEWORKER_VERSION}/coi-serviceworker.js public/coi-serviceworker.js

# Build app, as reported in the README.md file
npm install
npm run build

# End vue3-app work
popd

## Update version placeholder
UPSTREAM_VERSION=$(dpkg-parsechangelog -S Version | cut -d- -f1)
echo "Updating version to $UPSTREAM_VERSION"
echo "$UPSTREAM_VERSION" > debian/vue-apps/UPSTREAM_VERSION

## Undo changes to source repository
git checkout -- vue2-app/public/coi-serviceworker.min.js \
vue2-app/public/vue.global.js \
vue3-app/public/coi-serviceworker.min.js \
vue3-app/public/vue.global.js
rm vue2-app/public/coi-serviceworker.js \
vue3-app/public/coi-serviceworker.js
rm -rf vue2-app/dist/ vue2-app/node_modules/ \
vue3-app/dist/ vue3-app/node_modules/

## Remove unnecessary image files
rm debian/vue-apps/vue2-www/logo.png
rm debian/vue-apps/vue3-www/logo.png

## All done
echo "All vue apps have been updated and built successfully."
