
####
## Auto_Makefile by /Software/scripts/new_make
##    the Makefile templater
##



UNAME = $(shell uname|sed -e 's/HP\-UX/hpux/g')
MACHINE = $(shell uname -m|sed -e 's/0000[0-9A-F]*00/rs6000/g;s/\(i\)\([456]\)\(86\)/\1x\3/g')
UID = $(shell id |sed -e 's/[0-9][0-9]*/ & /g'|cut -f2 -d' ')
UMA = $(UNAME)-$(MACHINE)

ifeq (_${PREFIX}_,__)
ifeq (_$(UID)_,_0_)
PREFIX = /opt
# choose one of /Software, /public, /usr/local, /opt or whatever your prefix is 
else
PREFIX = ${HOME}/$(UMA)
# private users installation goes to your homedir
endif
else
PREFIX = ${PREFIX}
endif

TARGET =  rle2ppm
LIBS   =  
DEFS =  

EXTRA_INC =   
EXTRA_LIB =  

BINDIR = bin
# where to install, choose one of bin, sbin or libexec

CC	= gcc
OPTS	= -O3 # -funroll-loops 
WARN	= -Wall -Wno-unused
DEBUG	= -ggdb -DDEBUG
CODE	= -fpic # -fpcc-struct-return 

MAJOR =  0
MINOR =  1
PATCHLEVEL = 4

WITH_XAW =  
WITH_MOTIF =  
WITH_XFORMS =  
WITH_X = 
WITH_XINC = 

ifeq (_$(UNAME)_,_Linux_)
	TAR = tar
ifeq (_$(MACHINE)_,_sparc64_)
	OPTS += -msupersparc
	LIBS += -lNoVersion
else
	OPTS += -m486
endif
	INSTALL = install
else
	TAR = gtar
	INSTALL = ginstall
endif

ifeq (_$(UNAME)_,_SunOS_)
	EXTRA_INC += -I/usr/openwin/include # -I/usr/ucbinclude 
	LD      = /usr/ccs/bin/ld
	OPTS += -msupersparc
	LIBS += -lsocket -lnsl
endif

ifeq (_$(UNAME)_,_hpux_)
	SHARED_LIB = -b +b $(PREFIX)/lib -B immediate -B nonfatal                                       
	LINK_OPTS = -Wl,+b,$(PREFIX)/lib,-B,immediate,-B,nonfatal                                       
	MACHINE = $(shell uname -m|sed -e s///./g)
endif                                                                                           

PROGLIBPATH = $(PREFIX)/lib/$(TARGET)
DEFS += -D${UNAME} -DPREFIX=\"$(PREFIX)\" -DLIBPATH=\"$(PROGLIBPATH)\"

INCLDIR = -I$(PREFIX)/include $(EXTRA_INC)
LIBDIR	= -L$(PREFIX)/lib $(EXTRA_LIB)

ifeq ($(WITH_MOTIF),y)
  ifeq (_$(UNAME)_,_SunOS_)  
    INCLDIR += -I/usr/dt/include
    LIBDIR  += -L/usr/dt/lib
  else
    ifeq (_$(UNAME)_,_Linux_)  
      INCLDIR += -I/opt/LessTif-1.2/include
      LIBDIR  += -L/opt/LessTif-1.2/lib
    else
errortarget:
	echo not defined for $(UNAME)
    endif	
  endif

  LIBS += -lXm	
  WITH_X=y
endif

ifeq ($(WITH_XFORMS),y)
  LIBS += -lforms -lm -lXpm -lMesaGL
  WITH_X=y
endif

ifeq ($(WITH_XAW),y)
  LIBS += -lXmu -lXaw3d -lXt
  WITH_X=y
endif  


XINCLDIR =
XLIBDIR =
XLIBPATH =

ifeq (_$(UNAME)_,_SunOS_)  
  XINCLDIR += -I/usr/openwin/include/X11
  XLIBDIR  += -L/usr/openwin/lib
  XLIBPATH = /usr/openwin/lib/X11
else
ifeq (_$(UNAME)_,_Linux_)
  XINCLDIR += -I/usr/X11R6/include
  XLIBDIR  += -L/usr/X11R6/lib
  XLIBPATH = /usr/X11R6/lib/X11
else
# ifeq (_$(UNAME)_,_hpux_)                                                                         
  XINCLDIR += -I/usr/include/X11R5                                                              
  XLIBDIR  += -L/usr/lib/X11R5     
  XLIBPATH = /usr/lib/X11
# else
#  echo X-includes not defined for $(UNAME)
# endif
endif
endif


ifeq ($(WITH_X),y)
  WITH_XINC=y
 	
  LIBS += $(XLIBDIR)	
  LIBS += -lX11 # -lXext # -lXt -lXmu -lSM -lICE 

###### the following X-libs might be useful, so remember to edit 
#  LIBS	  += -lXm -lXaw3d -lXintl -lforms -lXpm -lMesaGL

  BINDIR_ = $(BINDIR)/X11
else
  BINDIR_ = $(BINDIR)
endif

ifeq ($(WITH_XINC),y)
  INCLDIR += $(XINCLDIR)
  DEFS += -DXLIBPATH=\"$(XLIBPATH)\"
endif

VERSION = $(MAJOR).$(MINOR).$(PATCHLEVEL)
REVISION = $(shell date +%Y/%m/%d,%H:%M)

SONAME = lib$(TARGET).so
LIBNAME = $(SONAME).$(VERSION)
INCNAME = $(TARGET)

OPA = objs/$(UMA)

include .headers
include .sources
include $(OPA)/.objects

include .defines
DEFS += $(DEFINES)

CFLAGS	= $(OPTS) $(WARN) $(DEFS) $(DEBUG_FLAGS) 

SUFFIX_LIST =  1

MAKE_T=$(TARGET)
      # only  $(TARGET)   or   $(LIBNAME)  allowed
      # $(LIBNAME) means we are compiling a library	

ifeq ($(MAKE_T),$(LIBNAME))
	CFLAGS += $(CODE)
	TARGETNAME = $(SONAME)
else
	TARGETNAME = $(TARGET)
endif

CFLAGS += -DVERSION=\"$(VERSION)\" 
CFLAGS += -DREVISION=\"$(REVISION)\" 
CFLAGS += -DTARGETNAME=\"$(TARGETNAME)\"



DEPEND=$(OPA)/.depend
O_MAKE_T=$(OPA)/$(MAKE_T)
O_TARGET=$(OPA)/$(TARGET)
O_LIBNAME=$(OPA)/$(LIBNAME)

all: $(DEPEND) $(O_MAKE_T) 
	@ls -l $(O_MAKE_T)          


ifeq ($(MAKE_T),$(TARGET)) 
    INSTALL_HELP = install_bin
else
    INSTALL_HELP = install_lib, static_lib
endif

help:
	@(echo "useful targets are"			;\
	echo "		$(O_MAKE_T) (default target)"	;\
	echo "		install:"			;\
	echo "		$(INSTALL_HELP):"		;\
	echo "		clean:"				;\
	echo "		tgz:"				;\
	echo "		depend:"			;\
	echo "		gdb:"				;\
	echo " press [ENTER] to continue:"		;\
	read A )


$(OPA)/%.dep: 
	@echo "refreshing dependency $@" 	
	@$(CC) $(CFLAGS) -DMODNAME=\"$*\" $(INCLDIR) -M $*  		|\
	sed -e "s/^.*\.o\:\ /objs\/$(UMA)\/&/g"  	>$@	

$(OPA)/%.o: 
	@echo compile $< 
	@$(CC) $(CFLAGS) -DMODNAME=\"$*\" $(INCLDIR) -c $< -o $@

.$(UMA)_o: 
	@(echo "making target directory $(UMA)" ;\
	 rm -rf $(OPA) ;\
	 mkdir -p $(OPA) ;\
	 touch .$(UMA)_o ;\
	 $(MAKE) help     )


cflag_info:
	@echo using flags $(CFLAGS) $(INCLDIR)
	@echo

clean:
	@rm -f a.out $(OPA)/*.o core $(OPA)/lib*.so* $(OPA)/lib*.a $(O_MAKE_T) 	

distclean: 
	rm -rf ./objs .*_o core a.out *.bck *.bak .*.bak .*.bck Makefile-* .hc~*~
	chmod 440 .headers .sources .defines


$(O_LIBNAME): cflag_info $(OBJS)
ifeq (_$(UNAME)_,_Linux_)
	$(CC) $(LIBDIR) -shared -Wl,-soname,$(SONAME) -o $(O_LIBNAME) $(OBJS)
else
ifeq (_$(UNAME)_,_SunOS_)
	$(LD) -B dynamic -G -h $(SONAME) -o $(O_LIBNAME) $(OBJS) 
else
ifeq (_$(UNAME)_,_hpux_)                                                                         
	$(LD) $(SHARED_LIB) -o $(O_LIBNAME) $(OBJS)                                               
else                                                                                            
ifeq (_$(UNAME)_,_AIX_)                                                                         
#	$(LD) -shared -r -h $(SONAME) -o $(O_LIBNAME) $(OBJS)                                               
else        
	echo dynamic linking not defined for $(UNAME)                                           
endif
endif                                                                                           
endif
endif

static_lib: cflag_info $(OBJS)
	ar -rc $(OPA)/lib$(TARGET).a $(OBJS)

$(OPA)/lib$(TARGET).a: static_lib

$(O_TARGET): cflag_info $(OBJS)
	$(CC) $(LINK_OPTS) $(LIBDIR) -o $(O_TARGET) $(OBJS) $(LIBS)	

ifeq (_$(UNAME)_,_AIX_)                                                                         
install_lib: $(OPA)/lib$(TARGET).a $(PREFIX)/lib
	ginstall -m 644 $(OPA)/lib$(TARGET).a $(PREFIX)/lib	
else        
install_lib: $(O_LIBNAME) $(PREFIX)/lib
	ginstall -m 755 $(O_LIBNAME) $(PREFIX)/lib
	@if test -r $(OPA)/lib$(TARGET).a ; then ginstall -m 644 $(OPA)/lib$(TARGET).a $(PREFIX)/lib ; fi
ifeq (_${LOGNAME}_,_root_)
	ldconfig
else
	(cd $(PREFIX)/lib;\
	 rm -f $(SONAME);\
	 ln -s $(PREFIX)/lib/$(LIBNAME) $(SONAME) )
endif
endif

install_inc: $(PREFIX)/include
	@(SRCPATH=`pwd` ;\
	  cd $(PREFIX)/include ; \
	  for I in $(HDRS) ;\
	    do rm -f $$I ;\
	    ln -s $$SRCPATH/$$I $$I ;\
	  done ) 		

compact: $(O_TARGET)
	strip $(O_TARGET)
	ls -l $(O_MAKE_T)
	
install_bin: compact  $(PREFIX)/$(BINDIR_)    # 
	$(INSTALL) -m 755 $(O_TARGET) $(PREFIX)/$(BINDIR_)

install_binlib: $(PREFIX)/lib/$(TARGET)
##	$(INSTALL) -m 644 xyz $(PREFIX)/lib/$(TARGET)/xyz

$(PREFIX)/$(BINDIR_):
	mkdir -m 755 -p $(PREFIX)/$(BINDIR_)

$(PREFIX)/lib:
	mkdir -m 755 -p $(PREFIX)/lib

$(PREFIX)/lib/$(TARGET):
	mkdir -p $(PREFIX)/lib/$(TARGET)

$(PREFIX)/include:
	mkdir -m 755 -p $(PREFIX)/include

$(PREFIX)/man:
	mkdir -m 755 -p $(PREFIX)/man

objs/$(UNAME):
	mkdir -m 755 -p objs/$(UNAME)

install_man: $(PREFIX)/man 
	@for I in $(SUFFIX_LIST) ;\
	 do mkdir -m 755 -p $(PREFIX)/man/man$$I ;\
	   for J in `/bin/ls *.$$I*` ;\
	   do $(INSTALL) -m 644 $$J $(PREFIX)/man/man$$I/$$J ;\
	   done ;\
	 done 



ifeq ($(MAKE_T),$(TARGET))
install: install_man install_bin install_binlib
	echo target was _$(MAKE_T)_ _$(TARGET)_ 
else
install: install_inc install_lib
	echo target was _$(MAKE_T)_ _$(TARGET)_
ifeq (_$(UNAME)_,_AIX_)                                                                         
else
endif
endif

tgz: 
	@($(MAKE) distclean	;\
	  inode=`ls -id . | awk '{print $$1}' `; \
	  cd .. ; \
	  ZIPDIR=`ls -ia | grep $$inode | awk '{ print $$2}' ` ; \
	  $(TAR) -cvzf $$ZIPDIR-$(VERSION).tgz $$ZIPDIR ) 	

ifeq ($(MAKE_T),$(TARGET))
gdb: 
	@$(MAKE) DEBUG_FLAGS="$(DEBUG)" WARN="-Wall" OPTS="" && gdb $(O_TARGET)
else
gdb: 
	@$(MAKE) DEBUG_FLAGS="$(DEBUG)" WARN="-Wall" OPTS="" && make install_lib
endif

gdball:  clean gdb


dep:	depend
depend:
	rm $(OPA)/*.dep $(OPA)/.depend
	$(MAKE) $(DEPEND)

$(DEPEND): .sources .headers .$(UMA)_o
ifeq (_$(SRCS)_,__)
	$(MAKE) $(DEPEND)
else	
	@(echo creating new dependency list for $(UMA)  >&2 ; \
	    for i in $(SRCS)                   ; \
	    do echo "$$i" >&2                  	; \
	    echo include objs/$(UMA)/$$i.dep	; \
	    done ) >$(DEPEND)
endif



$(OPA)/.objects: .sources .$(UMA)_o 
	@(echo creating .objects >&2; \
	cat .sources |\
	sed -e "s/\.[cC][c]*/\.o/g;s/SRCS/OBJS/g;s/[-0-9A-Za-z_]*\.o/objs\/$(UMA)\/&/g" >$(OPA)/.objects )

.sources:
	@(echo " " | awk '{printf("SRCS = ")}' 		;\
	ls *.cc *.C *.c 2>/dev/null	|		 \
	awk '{printf("\\"); printf("\n\t%s\t",$$1); }'	;\
	echo "	"	) >.sources 

.headers:
	@(echo " " | awk '{printf("HDRS = ")}'   	;\
	ls *.h *.H 2>/dev/null 		|		 \
	awk '{printf("\\"); printf("\n\t%s\t",$$1)}' 	;\
	echo "	"	) >.headers 


link:	$(OBJS) .rmlink $(O_TARGET)
.rmlink:
	rm -f $(O_TARGET)

forcedep: .rmdep $(DEPEND)
.rmdep:
	rm -f $(DEPEND)

srcs: .rmsrcs $(OPA)/.objects .headers forcedep
.rmsrc:
	rm -f .sources .headers	$(OPA)/.objects 

.defines: 		# $(HDRS) $(SRCS)
	@(echo scanning for additional defines >&2 ; \
	egrep -v '^//' *.[cChH] *.cc 2>/dev/null	|\
	egrep '\#define [A-Z][A-Z_]*[ \	]*(\"[0-9A-Za-z_/\\]*\"|[0-9][0-9]*[ ]*)$$' |\
	awk '{for (i=2; i<=NF; i++) { \
	if (i==2) printf("// DEFINES += -D%s=",$$i);	\
	else printf("%s",$$i); } printf("\n");}' 	|\
	sed -e 's/\"/\\&/g' >>.defines )


## dependencies follow

include $(DEPEND)




