#!/usr/bin/make -f


SHELL=/bin/sh


# Directories.
APT_CACHE=/var/cache/apt/archives


# Commands.
APTGET=/usr/bin/apt-get -q
APTGET_REM=$(APTGET) remove --purge --assume-yes
APTGET_INS=$(APTGET) install --assume-yes
DPKG=/usr/bin/dpkg
SED=/bin/sed -i -e


# Packages to be removed using apt-get. Note that some additional
# dependent packages will be automatically removed as well.
REM_MISC=diveintopython gaim gaim-data \
	screensaver-default-images sound-juicer totem-gstreamer \
	totem-mozilla totem rhythmbox linux-headers-2.6.17-10 \
	linux-headers-2.6.17-10-generic linux-headers-generic \
	xsane-common example-content

REM_GNOME=gnome-games gnome-games-data gnome2-user-guide

REM_OPENOFFICE=openoffice.org openoffice.org-base openoffice.org-calc \
	openoffice.org-common openoffice.org-core openoffice.org-draw \
	openoffice.org-evolution openoffice.org-gnome openoffice.org-gtk \
	openoffice.org-help-en-us openoffice.org-impress \
	openoffice.org-java-common openoffice.org-l10n-common \
	openoffice.org-l10n-en-gb openoffice.org-l10n-en-za \
	openoffice.org-math openoffice.org-style-default \
	openoffice.org-style-industrial openoffice.org-thesaurus-en-us \
	openoffice.org-writer python-uno

REM_EVOLUTION=evolution evolution-data-server evolution-data-server-common \
	evolution-exchange evolution-plugins evolution-webcal nautilus-sendto

REM_GIMP=gimp gimp-data gimp-print gimp-python libgimp2.0


# Packages to be added using apt-get. Some may already be installed,
# but that's OK, it won't hurt. It's safer than assuming they're there
# and getting it wrong! These will most likely install some other
# dependent packages as well.
ADD_EPRINTS=apache2 mysql-server perl libapache2-mod-perl2 \
	libunicode-string-perl libdbi-perl libdbd-mysql-perl \
	libxml-parser-perl wget tar gzip unzip poppler-utils wv lynx \
	tetex-bin imagemagick adduser findutils sendmail

ADD_WEBMIN=bash perl libnet-ssleay-perl openssl libauthen-pam-perl \
	libpam-runtime libio-pty-perl libmd5-perl

ADD_ECONFIG=perl libxml-dom-perl libppi-perl libstring-random-perl \
	libexpect-perl libunicode-string-perl libhtml-parser-perl \
	libmail-rfc822-address-perl libjson-perl

ADD_MISC=#openssh-server


# Local packages to be installed using dpkg.
DEB_EPRINTS=$(APT_CACHE)/eprints_3.0-0ubuntu2_all.deb

DEB_WEBMIN=$(APT_CACHE)/webmin-oarinz_1.340-0ubuntu1_all.deb

DEB_CONFIG=$(APT_CACHE)/oarinz-eprints-config_0.0.1-2_all.deb

DEB_MODULE=$(APT_CACHE)/webmin-eprints_0.0.1-1_all.deb



.PHONY: all add remove dpkg-add


all: remove add dpkg-add cleanup


remove:
	@echo
	@echo "=== Removing unnecessary packages."
	$(APTGET_REM) $(REM_OPENOFFICE)
	@rm -rf /etc/openoffice
	$(APTGET_REM) $(REM_EVOLUTION)
	$(APTGET_REM) $(REM_GIMP)
	$(APTGET_REM) $(REM_GNOME)
	$(APTGET_REM) $(REM_MISC)

add:
	@echo
	@echo "=== Adding prerequisite packages."
	@# Normally you'd just apt-get update here, but I've been having
	@# problems with the servers. Since it takes so long to get
	@# back to this point in the process, I've cheated and pre-generated
	@# the cache index files. They are copied in before this Makefile
	@# is called.
	@#$(APTGET) update
	$(APTGET_INS) $(ADD_EPRINTS)
	$(APTGET_INS) $(ADD_WEBMIN)
	$(APTGET_INS) $(ADD_ECONFIG)
#	$(APTGET_INS) $(ADD_MISC)


dpkg-add:
	@echo
	@echo "=== Adding EPrints packages."
	@# In order: eprints, webmin, oarinz-eprints-config, webmin-eprints
	$(DPKG) -i $(DEB_EPRINTS)
	$(DPKG) -i $(DEB_WEBMIN)
	$(DPKG) -i $(DEB_CONFIG)
	$(DPKG) -i $(DEB_MODULE)
	@# Patch the webmin module to fix the permissions problem
	@# (TEMPORARY HACK UNTIL A PERMANENT UPDATE ARRIVES).
	@$(SED) '61i system("chown -R eprints:eprints /opt/eprints3/archives/" . $$in{archive}); \ ' \
		/usr/share/webmin/eprints/create.cgi


cleanup:
	@echo
	@echo "=== Cleaning up."
	$(APTGET) clean
	$(APTGET) autoclean
	$(SED) 's/NO_START=1/NO_START=0/' /etc/default/apache2
	$(SED) 's/root@ou075136/root@localhost/' /etc/mail/submit.cf \
		/etc/cron.d/sendmail
