# os with ssh
FROM ubuntu:20.04

# package procedure(update&upgrade&install)
RUN apt-get update && \
    apt-get upgrade -y
RUN apt-get install -y openssh-server
RUN apt-get install -y mariadb-server mariadb-client
RUN apt-get install -y vim net-tools apt-utils iputils-ping wget tar curl language-pack-ko openjdk-8-jdk maven subversion apache2 libapache2-mod-svn

#package clean unused
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Set up configuration for SSH
RUN mkdir /var/run/sshd
RUN echo 'root:!@#gds$%^' | chpasswd
RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config

# SSH login fix. Otherwise, user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# SVN repository creation
RUN mkdir -p /svn/repos
RUN svnadmin create /svn/repos/geumdo_repo

# Apache SVN configuration
RUN echo "<Location /svn>" >> /etc/apache2/sites-available/svn.conf && \
    echo "  DAV svn" >> /etc/apache2/sites-available/svn.conf && \
    echo "  SVNPath /svn/repos/geumdo_repo" >> /etc/apache2/sites-available/svn.conf && \
    echo "  AuthType Basic" >> /etc/apache2/sites-available/svn.conf && \
    echo "  AuthName \"Subversion Repository\"" >> /etc/apache2/sites-available/svn.conf && \
    echo "  AuthUserFile /etc/apache2/dav_svn.passwd" >> /etc/apache2/sites-available/svn.conf && \
    echo "  Require valid-user" >> /etc/apache2/sites-available/svn.conf && \
    echo "</Location>" >> /etc/apache2/sites-available/svn.conf

# SET LOCALE
RUN echo "LANG=ko_KR.UTF-8" >> /etc/default/locale && \
    echo "LC_MESSAGES=ko_KR.UTF-8" >> /etc/default/locale

# Create SVN repository user
RUN htpasswd -cmb /etc/apache2/dav_svn.passwd gds !@#gds$%^

# Enable Apache SVN module
RUN a2enmod dav
RUN a2enmod dav_svn

# wget tomcat8.5.latest(tomcat 사양에 따라 dist 변경 필요)
RUN wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.98/bin/apache-tomcat-8.5.98.tar.gz
RUN tar -xf apache-tomcat-8.5.98.tar.gz -C /usr/local
RUN rm apache-tomcat-8.5.98.tar.gz
RUN mv /usr/local/apache-tomcat-8.5.98 /usr/local/tomcat

# java and tomcat path
RUN export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 >> /etc/profile
RUN export CATALINA_HOME=/usr/local/tomcat >> /etc/profile
RUN export CLASSPATH=.:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/jsp-api.jar:$CATALINA_HOME/lib/servlet-api.jar >> /etc/profile
RUN export LANG=ko_KR.UTF-8 >> /etc/profile
RUN bash -c 'source /etc/profile'

#run script folder and copy script
RUN mkdir /service_script
COPY start_service.sh /service_script
COPY setting.sql /service_script
RUN chmod +x /service_script/start_service.sh

# Expose port
EXPOSE 22 80 3306 3690 8080 9908

# Run SSH,DB,TOMCAT,SVN
CMD ["/service_script/start_service.sh"]
