FROM alpine:latest

#ENV PLAN9 /usr/lib/9base
ENV PLAN9 /usr/lib/plan9
ENV EDITOR /usr/bin/nvim

#RUN apk add 9base --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/
#RUN apk add plan9port
RUN apk add --update --no-cache \
	lighttpd \
	lighttpd-mod_auth \
	apache2-utils \
	curl \
	discount \
	shadow \
	imagemagick \
	aspell \
	aspell-en \
	neovim \
	git build-base linux-headers libucontext-dev libxt-dev libx11-dev fontconfig-dev libxext-dev perl \
  && rm -rf /var/cache/apk/*
RUN curl http://werc.cat-v.org/download/werc-1.5.0.tar.gz | tar xzf - && mkdir -p /var/www/ && mv ../werc-*/ /var/www/werc
RUN git clone https://github.com/9fans/plan9port /usr/lib/plan9
WORKDIR /usr/lib/plan9
RUN ./INSTALL
WORKDIR /

COPY etc/lighttpd/ /etc/lighttpd/
RUN sed 's/fltr_cache md2html\.awk/fltr_cache markdown/' /var/www/werc/etc/initrc > /var/www/werc/etc/initrc.local
COPY init.sh /usr/local/bin
RUN chmod +x /usr/local/bin/init.sh

RUN ln -s $PLAN9 /usr/local/plan9
RUN ln -s $PLAN9/bin/rc /bin/rc
RUN ln -s $PLAN9/bin/awk /bin/awk
#RUN cp /usr/bin/sha1sum $PLAN9/bin
ENV PATH="$PLAN9/bin:${PATH}"

EXPOSE 80

#RUN /var/www/werc/bin/contrib/fix-rc-scripts /var/www/werc/bin

RUN usermod -u 1000 lighttpd
RUN chown -R lighttpd:root /var/www
RUN chmod -R g+w /var/www/werc/sites
#RUN sed '1s|^.*$|#!/usr/lib/9base/bin/rc|' /var/www/werc/bin/werc.rc > /tmp/werc.rc.tmp && mv /tmp/werc.rc.tmp /var/www/werc/bin/werc.rc && chmod 775 /var/www/werc/bin/werc.rc
#RUN sed 's/formatter=(fltr_cache md2html\.awk)/formatter=(fltr_cache markdown)/' /var/www/werc/etc/initrc > /var/www/werc/etc/initrc.local
CMD ["init.sh"]
