FROM node:20.11-buster AS builder

WORKDIR /ng-app

COPY package.json package.json
COPY yarn.lock ./

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN yarn install
ARG bamboo_sonarqube_api_token_secret
ENV bamboo_sonarqube_api_token_secret=$bamboo_sonarqube_api_token_secret

COPY . .
## Build the angular app in production mode and store the artifacts in dist folder

# Fix auth issue then uncomment
# RUN node sonar.js
RUN yarn run build-lint-all
RUN yarn run build-paligo-styles
RUN rm -rf ./node_modules
CMD ["/bin/cp", "-r", "/ng-app/dist/paligo-styles", "/tmp/styles-export"]

### STAGE 2: Setup ###


FROM nginx:1.25.4-alpine
ENV NGINX_ENVSUBST_OUTPUT_DIR=/tmp
ENV PDFTRON_ALLOWED_HOSTS=''
## Copy our default nginx config
COPY docker/common/nginx/nginx.conf /etc/nginx/nginx.conf
COPY docker/common/nginx/custom.conf /etc/nginx/templates/custom.conf.template

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist/apps/red-ui /usr/share/nginx/html/ui
COPY version.json /usr/share/nginx/html/ui/assets/version/version.json

#RUN mkdir /tmp/nginx
#RUN chmod a+rw -R /tmp/nginx
RUN chmod o+r -R /usr/share/nginx/html
RUN chmod g+r -R /usr/share/nginx/html

## Change permissions to enable openShift functionality
# RUN chmod -R g+rwx /var/cache/nginx /var/run /var/log/nginx /usr/share /etc/nginx

COPY docker/red-ui/09-set-application-config.sh docker-entrypoint.d/
COPY docker/red-ui/40-check-config.sh docker-entrypoint.d/
