# Use the codercom/code-server image
FROM codercom/code-server:latest
MAINTAINER steven velozo

VOLUME /home/coder/.config
VOLUME /home/coder/.vscode

RUN echo "...installing debian dependencies..."
RUN sudo apt update
RUN sudo apt install vim curl tmux -y

RUN echo "Building development image..."

RUN echo "...installing vscode extensions..."

# Mocha unit testing in the sidebar
RUN code-server --install-extension hbenl.vscode-mocha-test-adapter
RUN code-server --install-extension hbenl.test-adapter-converter
RUN code-server --install-extension hbenl.vscode-test-explorer

# Magic indentation rainbow
RUN code-server --install-extension oderwat.indent-rainbow
RUN code-server --install-extension dbaeumer.vscode-eslint

# Contextual git
RUN code-server --install-extension eamodio.gitlens

# SQL Tools
RUN code-server --install-extension mtxr.sqltools
RUN code-server --install-extension mtxr.sqltools-driver-mysql

# >> Other extensions (uncomment them to have them automagic, or run this from a terminal to install in the container):

# Microsoft's AI code completion
# RUN code-server --install-extension  VisualStudioExptTeam.vscodeintellicode

# Live server -- make sure to open up the port on the docker image
# RUN code-server --install-extension ritwickdey.LiveServer

# Quick link to required modules' documentation
# RUN code-server --install-extension bengreenier.vscode-node-readme

# Switch up fonts
# RUN code-server --install-extension evan-buss.font-switcher

# Icons
# RUN code-server --install-extension vscode-icons-team.vscode-icons
# RUN code-server --install-extension PKief.material-icon-theme

# Hover over CSS colors to see them previewed
# RUN code-server --install-extension bierner.color-info

# An easy on the eyes color theme
# RUN code-server --install-extension daylerees.rainglow

RUN echo "...configuring mariadb (mysql) server..."
RUN sudo apt install default-mysql-server default-mysql-client -y
RUN sudo sed -i "s|bind-address|#bind-address|g" /etc/mysql/mariadb.conf.d/50-server.cnf
# This grants root everything in our test db instance
COPY ./.config/MySQL-Security.sql /home/coder/MySQL-Configure-Security.sql
# This is the docker entrypoint that launches the SQL server in addition to the vscode stuff
COPY ./.config/MySQL-Laden-Entry.sh /usr/bin/MySQL-Laden-Entry.sh
RUN sudo chmod +x /usr/bin/MySQL-Laden-Entry.sh
# Launch the mysql server and execute the security grant with the client
RUN ( sudo mysqld_safe --skip-grant-tables --skip-networking & ) && sleep 5 &&  mysql -u root < /home/coder/MySQL-Configure-Security.sql

# Create the initial database ... you can change these luxurydb names to whatever you want
RUN sudo service mariadb restart && sleep 5 && mysql -u root -p"123456789" -e "CREATE DATABASE luxurydb;"

# You can follow these patterns below if you want to create a database and populate it with data.
#
# >> Put your table creation script here or change this, to import a database.
# RUN echo "...creating and populating database tables..."
# COPY ./.config/MySQL-Create-Database.mysql.sql /home/coder/MySQL-Create-Databases.sql
# RUN sudo service mariadb restart && sleep 5 && mysql -u root -p"123456789" luxurydb < /home/coder/MySQL-Create-Databases.sql
#
# >> If you have a data population script, put it here.
# COPY ./.config/MySQL-Populate-Database.mysql.sql /home/coder/MySQL-Populate-Database.sql
# RUN sudo service mariadb restart && sleep 5 && mysql -u root -p"123456789" luxurydb < /home/coder/MySQL-Populate-Database.sql
# RUN echo "...database creation and population complmete."

RUN echo "...mapping library specific volumes..."

# Volume mapping for code
VOLUME /home/coder/{~D:AppData.Package.name~}

SHELL ["/bin/bash", "-c"]
USER coder

RUN echo "...installing node version manager..."
# Because there is a .bashrc chicken/egg problem, we will create one here to simulate logging in.  This is not great.
RUN touch ~/.bashrc && chmod +x ~/.bashrc
RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

RUN echo "...installing node version 14 as the default..."
RUN . ~/.nvm/nvm.sh && source ~/.bashrc && nvm install 14
RUN . ~/.nvm/nvm.sh && source ~/.bashrc && nvm alias default 14

WORKDIR /home/coder/{~D:AppData.Package.name~}

ENTRYPOINT ["/usr/bin/MySQL-Laden-Entry.sh"]