# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as 
# published by the Free Software Foundation here:
# (http://www.gnu.org/licenses/gpl-2.0.html)
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

export SRCPKG = openssh
export DEPENDS = glibc,openssl,zlib
export BUILDDEPS = file,gcc,linux-libc,openssl-dev
export ARCH = x86_64
export URL = http://www.openssh.com/
BRIEF = OpenSSH client and server
DESC = OpenSSH is the premier connectivity tool for remote login with the \
SSH protocol.
SNAPVER = 10

ARCHIVE := $(PWD)/SRC/$(shell ls SRC|egrep '(bz2|gz|tar|xz)$$'|tail -1)
TYPE := $(shell file -ib $(ARCHIVE)|cut -d';' -f1|tr -d '\n')
SRCDIR := $(shell tar -tf $(ARCHIVE)|head -1|sed 's/\/.*//')
PATCHDIR := $(PWD)/SRC/patches
export VERSION := $(shell echo $(SRCDIR)|egrep -o '\-[0-9].*'|\
	sed 's/^-//')-$(SNAPVER)

CLIENT := $(PWD)/openssh-client
CLIENTROOT := $(CLIENT)/ROOT
SERVER := $(PWD)/openssh-server
SERVERROOT := $(SERVER)/ROOT

$(SERVERROOT): $(CLIENTROOT)
	cp -v $(PWD)/SNAP/usher-server $(SERVER)/SNAP/usher && \
	cd $(SERVER) && make -f ../Makefile.server && mv *.snap ../

$(CLIENTROOT): $(SRCDIR)/sshd
	@install -v -d $(CLIENT)/SNAP && \
	if [ -d $(CLIENTROOT) ]; then \
		touch $(CLIENTROOT); \
	else \
		mkdir -v $(CLIENTROOT); \
	fi
	cd $(SRCDIR) && make install DESTDIR=$(CLIENTROOT) && \
	install -v -d -m 755 $(CLIENTROOT)/usr/share/doc/openssh && \
	install -v -d -m 755 $(CLIENTROOT)/usr/share/openssh && \
	install -v -m 755 contrib/ssh-copy-id $(CLIENTROOT)/usr/bin && \
	install -v -m 644 contrib/ssh-copy-id.1 \
		$(CLIENTROOT)/usr/share/man/man1 && \
	install -v -m644 INSTALL LICENCE OVERVIEW README* \
		$(CLIENTROOT)/usr/share/doc/openssh && \
	gzip $(CLIENTROOT)/usr/share/doc/openssh/* && \
	mv -v $(CLIENTROOT)/etc/ssh/ssh_config \
		$(CLIENTROOT)/usr/share/openssh && \
	install -v -d $(SERVER)/SNAP && \
	install -v -d $(SERVERROOT) && \
	install -v -d -m 755 $(SERVERROOT)/etc/{ssh,init.d} && \
	install -v -d -m 755 $(SERVERROOT)/usr/{libexec,sbin} && \
	install -v -d -m 755 $(SERVERROOT)/usr/share/man/man{5,8} && \
	install -v -d -m 755 $(SERVERROOT)/usr/share/openssh && \
	install -v -m 755 $(PWD)/SNAP/sshd.init \
		$(SERVERROOT)/etc/init.d/sshd && \
	mv -v $(CLIENTROOT)/etc/ssh/sshd_config \
		$(SERVERROOT)/usr/share/openssh/sshd_config && \
	mv -v $(CLIENTROOT)/usr/libexec/sftp-server \
		$(SERVERROOT)/usr/libexec/sftp-server && \
	mv -v $(CLIENTROOT)/usr/sbin/sshd \
		$(SERVERROOT)/usr/sbin/sshd && \
	mv -v $(CLIENTROOT)/usr/share/man/man5/sshd_config.5 \
		$(SERVERROOT)/usr/share/man/man5/sshd_config.5 && \
	mv -v $(CLIENTROOT)/usr/share/man/man8/sftp-server.8 \
		$(SERVERROOT)/usr/share/man/man8/sftp-server.8 && \
	cp -v $(PWD)/SNAP/usher-client $(CLIENT)/SNAP/usher && \
	cd $(CLIENT) && make -f ../Makefile.client && mv *.snap ../

$(SRCDIR)/configure: $(ARCHIVE)
	@if [ '$(TYPE)' == 'application/x-bzip2' ]; then \
		tar -jxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-gzip' ]; then \
		tar -zxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-tar' ]; then \
		tar -xf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-xz' ]; then \
		tar -xf $(ARCHIVE); \
	else \
		echo 'Unable to determine archive type'; \
		exit 1; \
	fi
	@touch $(SRCDIR)/configure

$(SRCDIR)/config.log: $(SRCDIR)/configure
	@cd $(SRCDIR) && \
	for patch in `find $(PATCHDIR) -name \*.patch|sort`; do \
		patch --verbose -Np0 -i $$patch; \
	done
	@cd $(SRCDIR); \
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc/ssh \
		--with-md5-passwords \
		--with-privsep-path=/var/run/sshd \
		--build=x86_64-snap-linux-gnu \
		--host=x86_64-snap-linux-gnu \
		--target=x86_64-snap-linux-gnu

$(SRCDIR)/sshd: $(SRCDIR)/config.log
	@cd $(SRCDIR) && make

test: $(CLIENTROOT)
	@cd $(SRCDIR); \
	make tests

clean:
	@rm -rvf $(ROOT) \
		$(CLIENT) \
		$(SERVER) \
		$(SNAPINFO) \
		$(MANIFEST) \
		$(FILES) \
		$(SRCDIR)

