# Use the official Ubuntu 22.04 base image
FROM ubuntu:22.04

# Update
RUN apt-get -y update

# Install tzdata
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata

RUN apt-get install -y qtbase5-dev qtchooser qt5-qmake  qtbase5-dev-tools emacs vim xvfb net-tools iputils-ping tcl8.6

# Install PostgreSQL
RUN apt-get install -y postgresql

# Install SSH server (sshd)
RUN apt-get install -y openssh-server

# Install wget to download files
RUN apt-get install -y wget

# Install X Services
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y \
    xorg \
    && rm -rf /var/lib/apt/lists/*

# Create a PostgreSQL user with admin privileges
USER postgres
RUN /etc/init.d/postgresql start && \
    psql --command "CREATE USER admin WITH SUPERUSER PASSWORD 'admin1234';"

# Switch back to the root user
USER root

# Install quasard
COPY quasar-server_6.0.3_amd64.deb /tmp/quasar.deb
     
RUN apt install -y /tmp/quasar.deb && \
    rm /tmp/quasar.deb

# Install virtuald
COPY quasar-virtual-client_6.0.3_amd64.deb /tmp/virtual_clientd.deb
RUN apt install -y /tmp/virtual_clientd.deb && \
    rm /tmp/virtual_clientd.deb

# Install quasar tools
COPY quasar-tools_6.0.3_amd64.deb /tmp/quasar-tools.deb
RUN apt install -y /tmp/quasar-tools.deb && \
    rm /tmp/quasar-tools.deb

# Create a user 'epos' with password 'epos1234' and grant sudo access
RUN useradd -m -p $(openssl passwd -1 epos) epos && \
    usermod -aG sudo epos

# Configure SSH server
RUN mkdir /var/run/sshd && \
    echo 'root:root' | chpasswd && \
    sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config


ENV DISPLAY=:0

COPY license.lic /opt/quasar/license.lic
COPY postgresql.cfg /opt/quasar/config/postgresql.cfg
COPY quasard.sh /quasard.sh
RUN chmod +x /quasard.sh
COPY newcompany.sh /newcompany.sh
RUN chmod +x /newcompany.sh
COPY virtual_clientd.sh /virtual_clientd.sh
RUN chmod +x /virtual_clientd.sh
COPY sample_company.xml /opt/quasar/import/sample_company.xml


# Expose port 3599 for your application
EXPOSE 3599 22

# Start the SSH service and keep the container running
CMD service ssh start && \
    service postgresql start && \
    bash -c 'nohup /quasard.sh & > output & sleep 1' && \
    bash -c 'nohup /virtual_clientd.sh & > output & sleep 1' && \
    sleep 10 && \
    /newcompany.sh && \
    tail -f /dev/null


