FROM ubuntu:22.04

# Set timezone
ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN sed -i -e 's/:\/\/(archive.ubuntu.com\|security.ubuntu.com)/old-releases.ubuntu.com/g' /etc/apt/sources.list
RUN apt-get -q update

# Install basic utilities and dependencies
RUN apt-get -qy install tmux nginx curl gunicorn3 git
RUN apt-get install software-properties-common -y

# Install Python 3.9 and related tools
RUN add-apt-repository -y ppa:deadsnakes/ppa
RUN apt-get update && apt install -y python3-pip python3.9 python3.9-dev libpq-dev
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10
RUN apt-get install -y python3.9-distutils
RUN pip3 install --upgrade setuptools && pip3 install --upgrade pip && pip3 install --upgrade distlib

# Set non-interactive installation mode
ARG DEBIAN_FRONTEND=noninteractive

# Install wkhtmltopdf and necessary packages
RUN apt-get update && apt-get install -y \
    wget \
    xfonts-75dpi \
    xfonts-base \
    libxrender1 \
    fontconfig \
    libfontconfig1 \
    libxext6 \
    libx11-6 \
    wkhtmltopdf

# Install fonts required for multiple Indian languages
RUN apt-get install -y fonts-dejavu-core fonts-dejavu-extra \
    fonts-lohit-deva-marathi \
    fonts-lohit-deva \
    fonts-lohit-guru \
    fonts-lohit-beng-bengali \
    fonts-lohit-gujr \
    fonts-lohit-beng-assamese

# Create necessary directories
RUN mkdir -p /home/code /home/data

WORKDIR /home/code

# Set locale environment variables
ENV LANG C.UTF-8
ENV LANGUAGE en_US:en
ENV LC_LANG en_US.UTF-8
ENV LC_ALL C.UTF-8
ENV ACCEPT_EULA Y

# Install Python dependencies
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
RUN pip3 install "uvicorn[standard]" gunicorn
RUN pip3 install psycopg2
RUN pip3 install pandas
RUN pip3 install asyncpg
RUN pip3 install nltk
RUN pip3 install passlib bcrypt
RUN pip3 install paramiko
RUN pip3 install aiofiles

# Download NLTK data
RUN python3 -m nltk.downloader punkt

COPY . .

# Make the auto_run.sh script executable
RUN chmod 755 /home/code/auto_run.sh

ENTRYPOINT ["/home/code/auto_run.sh"]
