# Makefile for smartmontools
#
# Home page: http://smartmontools.sourceforge.net
#
# $Id: Makefile,v 1.1.1.1 2007-05-25 09:36:56 jick_nan Exp $
#
# Copyright (C) 2002-3 Bruce Allen <smartmontools-support@lists.sourceforge.net>
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
# 
# You should have received a copy of the GNU General Public License (for
# example COPYING); if not, write to the Free Software Foundation, Inc., 675
# Mass Ave, Cambridge, MA 02139, USA.
#
# This code was originally developed as a Senior Thesis by Michael Cornwell
# at the Concurrent Systems Laboratory (now part of the Storage Systems
# Research Center), Jack Baskin School of Engineering, University of
# California, Santa Cruz. http://ssrc.soe.ucsc.edu/
#-----------------------------------------------------
TOPDIR  :=  $(shell /bin/pwd)
TOPDIR  :=  $(TOPDIR)/..

include $(TOPDIR)/prerules.mk
#-----------------------------------------------------

# Debugging
#CFLAGS = -fsigned-char -Wall -g

# Build against kernel header files.  Change linux-2.4 to correct path for your system
# CFLAGS	= -fsigned-char -Wall -O2 -I./usr/src/linux-2.4/include

# Normal build NOTE: I have had reports that with gcc 3.2 this code
# fails if you use anything but -Os.  I'll remove this comment when
# this is resolved, or I am reminded of it! GCC GNATS bug report
# #8404.  If you are getting strange output from gcc 3.2 try
# uncommenting LDFLAGS -s below.  Stripping the symbols seems to fix
# the problem.
CFLAGS	 += -fsigned-char -Wall -O2 -I${COMM_INC_PATH}
CPPFLAGS = -DHAVE_GETOPT_H -DHAVE_GETOPT_LONG
LDFLAGS  =  -L${COMM_LIB_PATH} -llog -lprofile -lutility # -s

GZIP=/bin/gzip
INSTALL = install
INSTALL_PROGRAM = ${INSTALL}
INSTALL_DATA = ${INSTALL} -m 644

releasefiles=atacmds.c atacmds.h ataprint.c ataprint.h CHANGELOG COPYING \
  extern.h knowndrives.c knowndrives.h Makefile README scsicmds.c scsicmds.h \
  scsiprint.c scsiprint.h smartctl.8 smartctl.c smartctl.h smartd.8 smartd.c \
  smartd.h smartd.initd TODO WARNINGS VERSION smartd.conf smartd.conf.5 \
  utility.c utility.h examplescripts/

counter=$(shell cat VERSION)
pkgname=smartmontools-5.1
pkgname2=$(pkgname)-$(counter)

docdir=/usr/share/doc/$(pkgname)
es=examplescripts
examplesdir=$(docdir)/$(es)

all: sendidle changemode smartd smartctl 
#	@echo -e "\n\nSmartd can now use a configuration file /etc/smartd.conf. Do:\n\n\tman ./smartctl.8\n\tman ./smartd.8\n\tman ./smartd.conf.5\n"
#	@echo -e "to read the manual pages now.  Unless you do a \"make install\" the manual pages won't be installed.\n"

smartctl: smartctl.c atacmds.o ataprint.o scsicmds.o scsiprint.o utility.o knowndrives.o\
          smartctl.h atacmds.h ataprint.h scsicmds.h scsiprint.h \
          utility.h extern.h knowndrives.h VERSION Makefile
	$(CC) -DSMARTMONTOOLS_VERSION=$(counter) -o smartctl $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) smartctl.c \
                                      atacmds.o ataprint.o knowndrives.o scsicmds.o scsiprint.o utility.o
	$(STRIP) smartctl
	
sendidle: sendidle.c atacmds.o ataprint.o scsicmds.o scsiprint.o utility.o knowndrives.o\
          smartctl.h atacmds.h ataprint.h scsicmds.h scsiprint.h \
          utility.h extern.h knowndrives.h VERSION Makefile
	$(CC) -DSMARTMONTOOLS_VERSION=$(counter) -s -o sendidle $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) sendidle.c \
	                                      atacmds.o ataprint.o knowndrives.o scsicmds.o scsiprint.o utility.o
	$(STRIP) sendidle
		                                      
changemode: changemode.c atacmds.o ataprint.o scsicmds.o scsiprint.o utility.o knowndrives.o\
          smartctl.h atacmds.h ataprint.h scsicmds.h scsiprint.h \
          utility.h extern.h knowndrives.h VERSION Makefile
	$(CC) -DSMARTMONTOOLS_VERSION=$(counter) -s -o changemode $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) changemode.c \
                                      atacmds.o ataprint.o knowndrives.o scsicmds.o scsiprint.o utility.o

smartd:  smartd.c atacmds.o ataprint.o scsicmds.o utility.o knowndrives.o \
         smartd.h atacmds.h ataprint.h knowndrives.h scsicmds.h utility.h extern.h VERSION \
         Makefile
	$(CC) -DSMARTMONTOOLS_VERSION=$(counter) -o smartd $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) smartd.c \
                                      atacmds.o ataprint.o knowndrives.o scsicmds.o utility.o 

atacmds.o: atacmds.c atacmds.h utility.h extern.h Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c atacmds.c 

ataprint.o: ataprint.c atacmds.h ataprint.h knowndrives.h smartctl.h extern.h utility.h \
            Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c ataprint.c 

scsicmds.o: scsicmds.c scsicmds.h extern.h Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c scsicmds.c

scsiprint.o: scsiprint.c extern.h scsicmds.h scsiprint.h smartctl.h utility.h Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c scsiprint.c

utility.o: utility.c utility.h Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c utility.c

knowndrives.o: knowndrives.c knowndrives.h utility.h atacmds.h ataprint.h Makefile
	$(CC) $(CFLAGS) $(CPPFLAGS) -c knowndrives.c



# This extracts the configuration file directives from smartd.8 and
# inserts them into smartd.conf.5
smartd.conf.5: smartd.8
	sed '1,/STARTINCLUDE/ D;/ENDINCLUDE/,$$D' < smartd.8 > tmp.directives
	sed '/STARTINCLUDE/,$$D'  < smartd.conf.5 > tmp.head
	sed '1,/ENDINCLUDE/D'   < smartd.conf.5 > tmp.tail
	cat tmp.head            > smartd.conf.5
	echo "\# STARTINCLUDE" >> smartd.conf.5
	cat tmp.directives     >> smartd.conf.5
	echo "\# ENDINCLUDE"   >> smartd.conf.5
	cat tmp.tail           >> smartd.conf.5
	rm -f tmp.head tmp.tail tmp.directives
clean:
	rm -f *.o smartctl smartd *~ \#*\# smartmontools*.tar.gz smartmontools*.rpm temp.* smart*.8.gz smart*.5.gz

install:
	$(STRIP) sendidle
	cp -af sendidle $(T_USBIN)	
	cp -af changemode $(T_USBIN)	
	cp -af smartctl $(T_USBIN)	
# perhaps for consistency I should also have $(DESTDIR) for the uninstall...
uninstall:
	rm -f /usr/share/man/man8/smartctl.8 /usr/share/man/man8/smartd.8 /usr/sbin/smartctl \
              /usr/share/man/man8/smartctl.8.gz /usr/share/man/man8/smartd.8.gz \
              /usr/share/man/man5/smartd.conf.5.gz /usr/sbin/smartd 
	rm -rf /usr/share/doc/smartmontools-5.1/
	if [ -f /var/lock/subsys/smartd -a -f /etc/rc.d/init.d/smartd ] ; then /etc/rc.d/init.d/smartd stop ; fi
	if [ -f /etc/rc.d/init.d/smartd ] ; then /sbin/chkconfig --del smartd ; fi
	if [ -f /etc/rc.d/init.d/smartd ] ; then  rm -f /etc/rc.d/init.d/smartd ; fi
	if [ -f /etc/smartd.conf.example ] ; then rm -f /etc/smartd.conf.example ; fi
	if [ -f /etc/smartd.conf ] ; then echo -e "\n\nWe have NOT REMOVED /etc/smartd.conf\n\n" ; fi


# Some of this mess is to automatically increment the release numbers.
# The number of the next release is kept in the file "VERSION"
release: smartd.conf.5
	if [ ! -f add -o ! -d CVS ] ; then echo "The make release target requires files checked out of CVS" ; exit 1 ; fi
	cat smartmontools.spec | sed '/Release:/d' > temp.spec
	echo "Release: " $(counter) > temp.version
	cat temp.version temp.spec > smartmontools.spec
	rm -f temp.spec temp.version
	. cvs-script && cvs commit -m "Release 5.1.$(counter)"
	. cvs-script && cvs tag -d "RELEASE_5_1_$(counter)" && cvs tag "RELEASE_5_1_$(counter)"
	rm -rf $(pkgname)
	mkdir $(pkgname)
	cp -a $(releasefiles) $(pkgname)
	rm -rf $(pkgname)/examplescripts/CVS
	tar zcvf $(pkgname).tar.gz $(pkgname)
	mv -f $(pkgname) $(pkgname2)
	tar zcvf $(pkgname2).tar.gz $(pkgname2)
	rm -rf $(pkgname2)
	mv -f $(pkgname).tar.gz /usr/src/redhat/SOURCES/
	rpm -ba smartmontools.spec
	mv /usr/src/redhat/RPMS/i386/$(pkgname)*.rpm .
	mv /usr/src/redhat/SRPMS/$(pkgname)*rpm .
	rm -f /usr/src/redhat/SOURCES/$(pkgname).tar.gz
	echo `hostname` | grep -q lap && echo $(shell ./add) > VERSION
