# First we download an image from the Docker universe
# Ubuntu 16.04
# And we will set the Maintainer to be the Eval Demo group
FROM ubuntu:16.04
MAINTAINER Eval Demo Group of Perforce Software


# Next we need to ensure that the OS package index is updated and we need to
# install some packages
RUN apt-get update && apt-get install -y wget curl unzip dnsutils

# We need to install some Helix packages. So need the public package key
RUN wget -q https://package.perforce.com/perforce.pubkey -O - | apt-key add -

# Add the Perforce repository to the list of package sources
RUN echo 'deb http://package.perforce.com/apt/ubuntu trusty release' > /etc/apt/sources.list.d/perforce.list

# Update the package index again
RUN apt-get update

# Install Helix Versioning Engine
RUN apt-get install -y helix-p4d helix-cli

# The Linux "perforce" user gets created when you install p4d. The following commands
# sets a password for the perforce user. You may want to change it here so that your
# installation is a little more secure.
RUN echo perforce:perforce | /usr/sbin/chpasswd

# Add the perforce user to the sudo group.
RUN usermod -G sudo -a perforce
RUN mkdir -p /home/perforce/perforce1666
RUN usermod -d /home/perforce perforce
RUN chown -R perforce:perforce /home/perforce

# Setup a shared volume. This will only work when you have Shared folders enabled in 
# VMware Workstation or Fusion or VirtualBox. In VMware Fusion the Docker host VM will
# need to be configured to mount the shared folder and then it will possible to mount th
# shared folder in the container.
# Be sure to correctly apply the correct shared folder name below
# For example: mine is /Users/perforce/perforce1666 and it is mounted in the same location
# in the container. However, this instruction only creates the mount point directory.
# Nothing gets mounted until you perform a 'docker run' command with the '-v' parameter.
# VOLUME ["/p4/1/"]


# Expose port 1666 of the container. You will have to have port forwarding configured in 
# your VM environment.
EXPOSE 1666 

# Create the Helix root directory and change ownership to the perforce user
# WORKDIR /opt/perforce
# RUN mkdir -p /opt/perforce/perforce1666 
RUN mkdir -p /p4/1/root
RUN touch /p4/1/p4d.log
RUN chown -R perforce:perforce /p4

# Setup the server with a config file for p4dctl
ADD ./main.conf /etc/perforce/p4dctl.conf.d/
ADD ./start_p4d.sh /
RUN chmod 755 /start_p4d.sh
RUN chown perforce:perforce /start_p4d.sh

# Set the basic environment for Helix
ENV NAME="p4d" \
	P4CONFIG=".p4config" \
	P4ROOT="/p4/1/root" \
	P4PORT="1666" \
	P4USER="perforce" \
	PATH="/bin:/usr/bin:/usr/sbin:/usr/local/bin:/opt/bin:/opt/perforce/bin:" \
	P4LOG="/p4/1/p4d.log"

USER perforce
WORKDIR /home/perforce

CMD ["/start_p4d.sh"]