# Makefile for sks (linux)
# gcc


CFLAGS = -DLINUX
VER=0.93
ES=0
EN=1
CC=gcc
#CC=gcc-3.4

# *******************************
# ***** START build configuration area
# ***** You may comment/uncomment definitions and flags
# ***** Some defs/flags have mandatory alternative, some not
# ******************************

CFLAGS	+= -s
CFLAGS	+=  -DVERSION='"$(VER)"'

INST = /usr/local/bin
#INST = $(HOME)/bin

# ******* If you don't have 'libtommath' installed
LTM = tfm

# ******* If you don't have 'libtomcrypt' installed
LTC = ltc

# ******** If you don't have 'zlib' installed
#GZ = zlib

# specify architecture here
#ARCH=i386
#ARCH=pentium4
ARCH=k8
CFLAGS += -march=$(ARCH)

# specify language ES, EN or input as command-line option i.e.: 'make SLANG=0'
SLANG=$(ES)

CFLAGS	+= -DSKS_LANG=$(SLANG)

# uses /dev/random or WinNT crypt-random
CFLAGS += -DDEVRANDOM

#warnings
CFLAGS += -Wall
#-Wsign-compare -W -Wno-unused -Wshadow -Werror

#  optimize for SPEED(don't include SIZE options)
CFLAGS += -O3 -funroll-loops
CFLAGS += -fomit-frame-pointer 
CFLAGS += -DFULLTABLES

#  optimize for SIZE (don't include SPEED options)
#CFLAGS += -Os -DSMALL_CODE

# *******************************
# ***** END build configuration area
# ******************************

ifeq ($(SLANG),$(ES))
	LANG=ES
	README=LEEME.txt
else
	LANG=EN
	README=README.txt
endif

default: sks

lang_test:
	echo $(SLANG);

# ******** If you don't have 'zlib' installed
ifdef GZ

gz: $(GZ)/libz.a

$(GZ)/libz.a:
	cd $(GZ); ./configure; make libz.a;

	
ZLIB = $(GZ)/libz.a
CFLAGS += -I$(GZ)
else

# ******** If you DO have 'zlib' installed:
ZLIB = -lz
endif

# ******* If you don't have 'libtommath' installed
ifdef LTM

LTMLIB=$(LTM)/mpi.o
CFLAGS += -I$(LTM)

else

# ******* If you DO have 'libtommath' installed:
LTMLIB = -ltommath
endif

# ******* If you don't have 'libtocrypt' installed
ifdef LTC

LTCLIB=$(LTC)/aes.o $(LTC)/tiger.o
CFLAGS += -I$(LTC)

else

# ******* If you DO have 'libtomcrypt' installed:
LTCLIB = -ltomcrypt
endif


ECC_OBJS= gflib.o eclib.o eccrypt.o
gflib.o: gflib.c gflib.h
eclib.o: eclib.c eclib.h gflib.h
eccrypt.o: eccrypt.c eccrypt.h eclib.h gflib.h

util.o: util.c sks.h
UTIL_OBJS=binasc.o util.o entropy.o hash.o symcrypt.o


KEY_OBJS=keyring.o

# *** Build the 'kernel'
LIBS = $(UTIL_OBJS) $(ECC_OBJS) $(KEY_OBJS) $(LTMLIB) $(LTCLIB) $(ZLIB)

sks.o: sks.c sks_msg_en.h sks_msg_es.h sks.h

DEPLIST = $(UTIL_OBJS) $(KEY_OBJS) $(ECC_OBJS) sks.o
ifdef LTM
DEPLIST += $(LTMLIB)
endif
ifdef LTC
DEPLIST += $(LTCLIB)
endif
ifdef GZ
DEPLIST += gz
endif

sks: $(DEPLIST)
	$(CC) -o $@ $@.o $(LIBS)

# *** Clean

PIGS = *.o sks sks.sig skstest
ifdef LTM
PIGS += $(LTM)/mpi.o
endif
ifdef LTC
PIGS += $(LTC)/aes.o $(LTC)/tiger.o
endif
ifdef GZ
PIGS += $(GZ)/*.o $(GZ)/*.a
endif

clean: limpia
limpia:
	rm -f $(PIGS)
		
# Tests
skstest.o: skstest.c
skstest: skstest.o $(ECC_OBJS) $(UTIL_OBJS) $(KEY_OBJS)
	$(CC) $@.o  $(UTIL_OBJS) $(ECC_OBJS) $(KEY_OBJS) $(LTCLIB) $(LTMLIB) $(ZLIB) -o $@
	

SNAME=sks-source-$(VER)
source: #clean 
	cd ..; rm -rf sks-$(VER); mkdir sks-$(VER); \
	cp -R ./sks/*.c ./sks-$(VER)/; cp -R ./sks/*.h ./sks-$(VER)/; \
	cp -R ./sks/skspkr ./sks-$(VER)/; \
	cp -R ./sks/sks.1 ./sks-$(VER)/; \
	cp -R ./sks/Makefile ./sks-$(VER)/; \
	cp -R ./sks/Makefile.w32 ./sks-$(VER)/; \
	cp -R ./sks/*.txt ./sks-$(VER); cp -R ./sks/doc ./sks-$(VER)/;  \
	rm -f ./sks/tfm/mpi.o; 	cp -R ./sks/tfm ./sks-$(VER)/;  \
	rm -f ./sks/ltc/*.o; 	cp -R ./sks/ltc ./sks-$(VER)/;  \
	rm -f ./sks/zlib/*.o ./sks/zlib/*.a; cp -R ./sks/zlib ./sks-$(VER)/;  \
	tar -c -h sks-$(VER)/* | bzip2 -9vvc > $(SNAME).tar.bz2; \
	echo "md5 sum:" > $(SNAME).sum; \
	md5sum $(SNAME).tar.bz2 >> $(SNAME).sum; \
	echo "tiger sum:" >> $(SNAME).sum; \
	sks -r $(SNAME).tar.bz2 >> $(SNAME).sum; \
	zip -9 -r $(SNAME).zip sks-$(VER)/*; \
	echo "md5 sum:" >> $(SNAME).sum; \
	md5sum $(SNAME).zip >> $(SNAME).sum; \
	echo "tiger sum:" >> $(SNAME).sum; \
	sks -r $(SNAME).zip >> $(SNAME).sum; 
	

BNAME=sks-i386bin-$(LANG)-$(VER)
binary: clean sks
ifneq ($(ARCH),i386)
	@echo "Defined architecture is not portable. Define 'ARCH=i386'"
else
	./sks -s sks sks.sig
	./sks -v sks sks.sig
	tar -c *.sig $(README) ./sks skspkr ./doc/* | bzip2 -9vvc > ../$(BNAME).tar.bz2
	cd ..; \
	echo "md5 sum:" > $(BNAME).sum; \
	md5sum $(BNAME).tar.bz2 >> $(BNAME).sum; \
	echo "tiger sum:" >> $(BNAME).sum; \
	sks -r $(BNAME).tar.bz2 >> $(BNAME).sum;
#	zip -9 -r ../sks-i386bin-$(VER).zip *.s *.txt ./sks skspkr ./doc/*
#Falta 'readme'
endif

WNAME=sks-w32-$(LANG)-$(VER)
w32: # Makes the md5- and tiger-sums
	cp sks-$(LANG).exe sks.exe
	sks -s sks.exe sks.sig 
	sks -v sks.exe sks.sig 
	zip -9 -r ../$(WNAME).zip *.sig sks.exe skspkr *.txt ./doc/* 
	echo "md5 sum:" > ../$(WNAME).sum 
	md5sum ../$(WNAME).zip >> ../$(WNAME).sum 
	echo "tiger sum:" >> ../$(WNAME).sum 
	sks -r ../$(WNAME).zip >> ../$(WNAME).sum
	rm -f sks.exe
	
install: sks
	cp sks $(INST)

