# 
# Makefile for TSLinux's building of OpenSSH
#

# MontaVista Cross-compiler paths
CROSS_COMPILE = /opt/hardhat/devkit/ppc/8xx/bin/ppc_8xx-
CC	= $(CROSS_COMPILE)gcc
LD	= $(CROSS_COMPILE)ld
AR	= $(CROSS_COMPILE)ar
NM	= $(CROSS_COMPILE)nm
STRIP	= $(CROSS_COMPILE)strip --remove-section=.note --remove-section=.comment
OBJCOPY	= $(CROSS_COMPILE)objcopy
OBJDUMP	= $(CROSS_COMPILE)objdump
RANLIB	= $(CROSS_COMPILE)ranlib

# Utilities
I386_SSH_KEYGEN = /usr/bin/ssh-keygen
I386_AUTOCONF = /usr/bin/autoconf-2.53
I386_MAKEDEPEND = /usr/bin/X11/makedepend

# Results
FILES=ssh sshd scp ssh_config.out sshd_config.out ssh-keygen

MY_CFLAGS  := -I$(PROJ)/linux/include -I$(PAM_DIR)/libpam/include -I$(PAM_DIR)/libpam_misc/include -I$(PAM_DIR)/libpamc/include -I$(RAS_DIR) -I$(ADSAP2_DIR)
MY_CPPFLAGS  = -DCYCLADES -DPAM_STRERROR_NEEDS_TWO_ARGS -DUSE_PAM -DCY_DLA_ALERT
MY_LDFLAGS  = -s -L$(ADSAP2_DIR) -ladsap2 -L$(RAS_DIR)/src -lcycl -L$(PAM_DIR)/libpam -L$(PAM_DIR)/libpamc -L$(PAM_DIR)/libpam_misc -lpam -lpam_misc -L$(OPEN_SSL_DIR) -lssl -lcrypto 
#MY_LDFLAGS  = -s -L$(RAS_DIR)/src -lcycl -L$(PAM_DIR)/libpam -L$(PAM_DIR)/libpamc -L$(PAM_DIR)/libpam_misc -lpam -lpam_misc 

# Make alias
MAKE	= make

export CC LD AR NM STRIP OBJCOPY OBJDUMP RANLIB

all:	check_utilities Makefile
	$(MAKE)

install: all $(FILES)
	install -m 755  ./libssh.so    $(TARGET_DIR)/lib
	$(STRIP) $(TARGET_DIR)/lib/libssh.so
	install -m 755  ./openbsd-compat/libopenbsd-compat.so    $(TARGET_DIR)/lib
	$(STRIP) $(TARGET_DIR)/lib/libopenbsd-compat.so
	install -m 755  ./ssh-keygen    $(TARGET_DIR)/bin/ssh-keygen
	$(STRIP) $(TARGET_DIR)/bin/ssh-keygen
	install -m 755  ./sftp-server   $(TARGET_DIR)/bin/sftp-server
	$(STRIP) $(TARGET_DIR)/bin/sftp-server
	install -m 755  ./sftp          $(TARGET_DIR)/bin/sftp
	$(STRIP) $(TARGET_DIR)/bin/sftp
	install -m 755	./sshd			$(TARGET_DIR)/bin/sshd
	$(STRIP) $(TARGET_DIR)/bin/sshd
	install -m 755	./ssh			$(TARGET_DIR)/bin/ssh
	$(STRIP) $(TARGET_DIR)/bin/ssh
	install -m 755	./scp			$(TARGET_DIR)/bin/scp
	$(STRIP) $(TARGET_DIR)/bin/scp
	install -d 755				$(TARGET_DIR)/etc/ssh/
	install -m 644	./moduli.out		$(TARGET_DIR)/etc/ssh/moduli
	install -m 644	./ssh_config.out	$(TARGET_DIR)/etc/ssh/ssh_config
	install -m 644	./sshd_config.out	$(TARGET_DIR)/etc/ssh/sshd_config
	install -m 755	./contrib/ssh-copy-id	$(TARGET_DIR)/bin/ssh-copy-id
	test -c  $(TARGET_DIR)/dev/random || \
		 mknod $(TARGET_DIR)/dev/random c 1 8
	test -c $(TARGET_DIR)/dev/urandom || \
		mknod $(TARGET_DIR)/dev/urandom c 1 9
	test -f $(TARGET_DIR)/etc/ssh/ssh_host_key || \
		$(I386_SSH_KEYGEN) -t rsa1 -b 1024 -C RSA1 \
			-f $(TARGET_DIR)/etc/ssh/ssh_host_key -N ''
	test -f $(TARGET_DIR)/etc/ssh/ssh_host_rsa_key || \
		$(I386_SSH_KEYGEN) -t rsa -b 1024 -C RSA \
			-f $(TARGET_DIR)/etc/ssh/ssh_host_rsa_key -N ''
	test -f $(TARGET_DIR)/etc/ssh/ssh_host_dsa_key || \
		$(I386_SSH_KEYGEN) -t dsa -b 1024 -C DSA \
			-f $(TARGET_DIR)/etc/ssh/ssh_host_dsa_key -N ''

check_utilities:
	@test -x $(I386_SSH_KEYGEN) || \
		( echo "ERROR: The ssh-keygen utility was not found"; \
		  echo "Please install the openssh and openssh-clients packages in your machine"; \
		  exit 1 )
	@test -x $(I386_MAKEDEPEND) || \
		( echo "ERROR: The makedepend utility was not found"; \
		  echo "Please install the XFree86-devel package in your machine"; \
		  exit 1 )
	@test -x $(I386_AUTOCONF) || \
		( echo "ERROR: The autoconf utility was not found"; \
		  echo "Please install the autoconf package in your machine"; \
		  exit 1 )

Makefile: config.h.in configure Makefile.in ex_config
	rm -f config.cache
	CC=$(CC) RANLIB="$(RANLIB)" AR="$(AR)" LD="$(CC)" NM="$(NM)" OBJCOPY="$(OBJCOPY)" OBJDUMP="$(OBJDUMP)" MAKE="$(MAKE)" STRIP="$(STRIP)" \
	 ./configure --prefix=/ \
		--sysconfdir=/etc/ssh \
		--libexecdir=/bin \
		--with-ssl-dir=$(OPEN_SSL_DIR) \
		--with-openssl-src=$(OPEN_SSL_DIR) \
		--with-pam \
		--with-tcp-wrappers=no \
		--disable-utmpx --disable-wtmpx \
		--disable-lastlog \
		--host=powerpc-hardhat-linux \
		--build=i386 \
		--with-ldflags="$(MY_LDFLAGS)" \
		--with-cflags="$(MY_CFLAGS)" \
		--with-cppflags="$(MY_CPPFLAGS)" \
		--with-random=/dev/urandom 

configure: configure.ac
	@if [ ! -f configure.in ]; then ln -s configure.ac configure.in ; fi
	$(I386_AUTOCONF)

config.h.in: 
	@if [ ! -f configure.in ]; then ln -s configure.ac configure.in ; fi
	rm -f config.h
	autoheader-2.53
	touch configure.ac

clean:
	if [ -f Makefile ] ; then \
		$(MAKE) veryclean ; \
	fi

force: ;
