FROM debian:buster

MAINTAINER Abram Connelly <abram@clementinecomputing.com>

RUN \
  apt-get update && \
  apt-get -y upgrade && \
  apt-get clean

RUN \
  apt-get install -y gcc perl \
    git openssh-server sqlite3 libdbd-sqlite3-perl \
    default-mysql-server apache2 \
    netcat telnet wget \
    mariadb-server

RUN \
  apt-get install -y libswitch-perl libdate-calc-perl

RUN \
  useradd -m bus && \
  echo 'bus:bus' | chpasswd

RUN \
  su bus -c " cd /home/bus && \
    git clone https://tree.clementinecomputing.com/clementinecomputing/popufare && \
    mkdir -p /home/bus/.ssh && chmod 0700 /home/bus/.ssh && \
    mkdir -p /home/bus/bin && \
    mkdir -p /home/bus/data && \
      sqlite3 /home/bus/data/bus.sqlite < /home/bus/popufare/server/sql_schema/create_tables.sqlite && \
      sqlite3 /home/bus/data/bus.sqlite < /home/bus/popufare/server/testing/db/setup-testdb.sqlite "

COPY setup-testdb /root/setup-testdb

RUN bash /root/setup-testdb

## setup test ssh access
##
## WARNING: this MUST be changed for anything other than testing purposes
##
COPY ./snakeoil_id_rsa.pub /home/bus/.ssh/authorized_keys.snakeoil
RUN su bus -c \
  " cat /home/bus/.ssh/authorized_keys.snakeoil >> /home/bus/.ssh/authorized_keys && \
    chmod 640 /home/bus/.ssh/authorized_keys "

# non-essential but helpful for debuggin/admin
#
RUN \
  apt-get install -y vim

RUN \
  mkdir -p /etc/ridelogic && \
  mkdir -p /etc/ridelogic/logs && \
  mkdir -p /var/log/ridelogic && \
  mkdir -p /var/run/ridelogic && \
  cp -f /home/bus/popufare/server/install/etc/ridelogic/api.conf /etc/ridelogic && \
  cp -f /home/bus/popufare/server/install/etc/ridelogic/daemon.conf /etc/ridelogic && \
  cp -f /home/bus/popufare/server/scripts/RideLogic.pm /usr/share/perl5 && \
  cp -f /home/bus/popufare/server/scripts/RideLogicDBI.pm /usr/share/perl5 && \
  cp -f /home/bus/popufare/server/scripts/RideLogicACL.pm /usr/share/perl5 && \
  cp -f /home/bus/popufare/server/scripts/RideLogicAPIQueryWrapper.pm /usr/share/perl5

# main startup scripts
#
COPY ./startup_and_persist.sh /root/startup_and_persist.sh

RUN \
  chmod u+x /root/startup_and_persist.sh

EXPOSE 80 22 443 6055 2857 7277 2455 3556 8377

CMD ["/root/startup_and_persist.sh"]
