#!/usr/bin/env bash
set -xe

# usage: pack-magneto2-source <image repo> [output folder]
if [ $# -lt 1 ]; then
    echo "Usage: $0 <image repo> [output folder]"
    exit 1
fi
IMAGE_REPO=$1
OUTPUT_FOLDER=$(realpath "${2:-.}")

# echo "Packing Magento 2 source code from $IMAGE_REPO to $OUTPUT_FOLDER"
uid=$(id -u)

docker run -it --rm -v "$OUTPUT_FOLDER:/out" -e "HOST_UID=$uid" \
  "$IMAGE_REPO" \
  bash -c 'cd /app && \
    find /app -type f -iname "*test*" -exec rm -f {} \; && \
    rm -rf /app/node_modules && \
    rm -rf /app/system/modules/swagger/public/swagger-ui/* && \
    chown -R $HOST_UID /app && \
    tar -cvf /out/magneto2.tar . && \
    chown $HOST_UID /out/magneto2.tar'

# vim : set ft=sh :
