#/***********************************************************************
#/ The contents of this file are subject to the Nishan Systems, Inc. Public 
#/ License Version 1.0 (the "License"); you may not use this file except in 
#/ compliance with the License included with the distribution. You may 
#/ obtain a copy of the License at www.NishanSystems.com.
#/
#/ Software distributed under the License is distributed on an "AS IS" 
#/ basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
#/ the License for the specific language governing rights and limitations 
#/ under the License.
#/
#/ The Original Software is Release 1.0 of the iSNS Example Client and 
#/ Server. Portions created by Nishan Systems are Copyright 2001 Nishan 
#/ Systems, Inc. All Rights Reserved. 
#/
#/ This Original Software is subject to a standard specification issued 
#/ by the IETF.  The Specification is titled iSNS Internet Storage Name 
#/ Service.
#/
#/ The Initial Contributor of the Original Software is Nishan Systems, Inc. 
#/ Revision Contributor(s) are listed below:
#/
#/            Submitted Revision Contributor Name and Date
#/
#/***********************************************************************/

SHELL = /bin/sh

ISNS_VERSION:=$(shell grep ISNS_VERSION include/isns.h | cut -d"\"" -f2 | cut -d"-" -f1 )
CWD:=$(shell pwd)
VENDOR = pyx

DISTRO:=$(shell grep -i red /etc/issue | awk '{ if ($$1 == "Red" && $$2 == "Hat") print "redhat" }')
ifeq ($(DISTRO),)
	DISTRO:=$(shell grep -i suse /etc/issue | awk '{ if ($$3 == "SUSE" || $$3 == "SuSE") print "suse" }')
endif
ifeq ($(DISTRO),)
	DISTRO:=$(shell grep -i debian /etc/issue | awk '{ if ($$1 == "Debian") print "debian" }')
endif
ifeq ($(DISTRO),)
        DISTRO:=$(shell grep -i fedora /etc/issue | awk '{ if ($$1 == "Fedora") print "fedora" }')
endif
ifeq ($(DISTRO),)
	DISTRO:=$(shell grep -i centos /etc/issue | awk '{ if ($$1 == "CentOS") print "centos" }')
endif
ifeq ($(DISTRO),)
	DISTRO:="other"
endif

ifeq ($(DISTRO), redhat)
RPMDIR = redhat
endif
ifeq ($(DISTRO), suse)
RPMDIR = packages
endif
ifeq ($(DISTRO), mandrake)
RPMDIR = RPMS
endif
ifeq ($(DISTRO), centos)
RPMDIR = redhat
endif
ifeq ($(DISTRO), fedora)
RPMDIR = redhat
endif


CC = cc -g
SRCDIR = ./src/
INCDIR = ./include/
INCDIR += -I/usr/include/

# CFLAGS = -Werror -Wall

SRCS = $(SRCDIR)comm.c
SRCS += $(SRCDIR)getarg.c  
SRCS += $(SRCDIR)iscsi.c  
SRCS += $(SRCDIR)isns.c  
SRCS += $(SRCDIR)main.c
SRCS += $(SRCDIR)menu.c  
SRCS += $(SRCDIR)parse.c
SRCS += $(SRCDIR)util.c
SRCS += $(SRCDIR)core-iscsi.c

CFLAGS = -DSNS_LINUX
CFLAGS += -DSNS_DEBUG
CFLAGS += -DSNS_VERSION=1

all : $(SRCS) 
	rm -rf isnsc
	$(CC) -I$(INCDIR) $(CFLAGS) -lpthread -o isnsc $(SRCS)

install: all
	/bin/cp isnsc /sbin/
	/bin/cp scripts/* /sbin/
	if [ ! -f /var/spool/isns ]; then	\
		mkdir -p /var/spool/isns;	\
	fi

tarball :
	@( rm -f isnsclient-v*.tar.bz2 isnsclient-v*.spec )
	@( cd .. ; cp -a isnsclien* isnsclient-$(ISNS_VERSION) )
	@( cd .. ; tar cpsjf isnsclient-v$(ISNS_VERSION).tar.bz2 isnsclient-$(ISNS_VERSION) )
	@( mv ../isnsclient-v$(ISNS_VERSION).tar.bz2 . )
	@( rm -rf ../isnsclient-$(ISNS_VERSION) )
	@( touch isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define isns_version $(ISNS_VERSION)" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "" >> isnsclient-v$(ISNS_VERSION).spec )
ifeq ($(DISTRO), redhat)
	@( echo "%define redhat 1" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 0" >> isnsclient-v$(ISNS_VERSION).spec )
endif
ifeq ($(DISTRO), suse)
	@( echo "%define redhat 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 1" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 0" >> isnsclient-v$(ISNS_VERSION).spec )
endif
ifeq ($(DISTRO), mandrake)
	@( echo "%define redhat 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 1" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 0" >> isnsclient-v$(ISNS_VERSION).spec )
endif
ifeq ($(DISTRO), debian)
	@( echo "%define redhat 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 1" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 0" >> isnsclient-v$(ISNS_VERSION).spec )
endif
ifeq ($(DISTRO), centos)
	@( echo "%define redhat 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 1" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 0" >> isnsclient-v$(ISNS_VERSION).spec )
endif
ifeq ($(DISTRO), fedora)
	@( echo "%define redhat 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define suse 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define mandrake 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define debian 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define centos 0" >> isnsclient-v$(ISNS_VERSION).spec )
	@( echo "%define fedora 1" >> isnsclient-v$(ISNS_VERSION).spec )
endif
	@( cat isnsclient.spec >> isnsclient-v$(ISNS_VERSION).spec )

rpms: tarball
	@( rm -f /usr/src/$(RPMDIR)/SOURCES/isnsclient-v*.tar.bz2 )
	@( rm -f /usr/src/$(RPMDIR)/SPECS/isnsclient-v*.spec )
	@( cp isnsclient-v$(ISNS_VERSION).tar.bz2 /usr/src/$(RPMDIR)/SOURCES/isnsclient-v$(ISNS_VERSION).tar.bz2 )
	@( cp isnsclient-v$(ISNS_VERSION).spec /usr/src/$(RPMDIR)/SPECS/isnsclient-v$(ISNS_VERSION).spec )
	@( rpmbuild -ba --with $(VENDOR) --with $(DISTRO) --target `uname -i` /usr/src/$(RPMDIR)/SPECS/isnsclient-v$(ISNS_VERSION).spec )

clean :
	@( rm -f isnsc )
	@( rm -rf ../isnsclient-$(ISNS_VERSION) )
	@( rm -rf isnsclient-v*.tar.bz2 )
	@( rm -rf isnsclient-v*.spec )

distclean: clean
