
##########################################################
# 環境変数
##########################################################
CC=@CC@
INSTALL=@INSTALL@
CFLAGS=@CFLAGS@
LIBS=@LIBS@
LIBS2=@LIBS2@
OBJ=saphire_debug.o saphire_extra.o saphire_hash.o saphire_kanji.o saphire_list.o saphire_string.o saphire_vector.o saphire_main.o saphire_vm.o saphire_parser.o saphire_commands.o saphire_curses.o
CYGWIN_DLL=@CYGWIN_DLL@
SYSTEM_MIGEMODIR=@SYSTEM_MIGEMODIR@
LIBSAPHIRESO=@LIBSAPHIRESO@
LIBSAPHIRESO1=@LIBSAPHIRESO1@
DESTDIR=

#######################################################
# インストールするディレクトリ位置
#######################################################
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
datadir=@datadir@
datarootdir=@datarootdir@
mandir=@mandir@
libdir=@libdir@
sharedstatedir=@sharedstatedir@
sysconfdir=@sysconfdir@
includedir=@includedir@
docdir=@docdir@saphire

##########################################################
all: lib saphire sash saphire.sao
	rm -f install

lib: $(CYGWIN_DLL) $(LIBSAPHIRESO1) 
	rm -f install

lib-install:
	if [ -z "$(DESTDIR)" ]; then make lib-normal-install; else make lib-dest-install; fi

lib-normal-install:
	mkdir -p "$(libdir)"
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 libsaphire.so.1.0 "$(libdir)"; else install -s -m 755 libsaphire.so.1.0 "$(libdir)"; fi; else install -m 755 libsaphire.1.0.dylib "$(libdir)"; fi
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then ln -s -f libsaphire.so.1.0 "$(libdir)"/libsaphire.so.1; else ln -s -f libsaphire.1.0.dylib "$(libdir)"/libsaphire.1.dylib; fi
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then ln -s -f libsaphire.so.1.0 "$(libdir)"/libsaphire.so; else ln -s -f libsaphire.1.0.dylib "$(libdir)"/libsaphire.dylib; fi
	if [ -e "$(CYGWIN_DLL)" ]; then install -s -m 644 saphire.dll "$(libdir)"; install -s -m 644 saphire.dll "$(bindir)"; fi

lib-dest-install:
	mkdir -p "$(DESTDIR)/$(libdir)"
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 libsaphire.so.1.0 "$(DESTDIR)/$(libdir)"; else install -s -m 755 libsaphire.so.1.0 "$(DESTDIR)/$(libdir)"; fi; else install -m 755 libsaphire.1.0.dylib "$(DETDIR)/lib"; fi
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then ln -s -f libsaphire.so.1.0 "$(DESTDIR)/$(libdir)"/libsaphire.so.1; else ln -s -f libsaphire.1.0.dylib "$(DESTDIR)/$(libdir)"/libsaphire.1.dylib; fi
	if [ $(LIBSAPHIRESO) = libsaphire.so ]; then ln -s -f libsaphire.so.1.0 "$(DESTDIR)/$(libdir)"/libsaphire.so; else ln -s -f libsaphire.1.0.dylib "$(DESTDIR)/$(libdir)"/libsaphire.dylib; fi
	if [ -e "$(CYGWIN_DLL)" ]; then install -s -m 644 saphire.dll "$(DESTDIR)/$(libdir)"; install -s -m 644 saphire.dll "$(DESTDIR)/$(bindir)"; fi

clean:
	rm -fR saphire sash sash.exe sash.dSYM sash.exe.stackdump *.o $(CYGWIN_DLL) libsaphire* saphire.sao saphire.exe*

distclean:
	rm -fR saphire sash sash.exe sash.dSYM *.o $(CYGWIN_DLL) libsaphire* config.h Makefile saphire.sao saphire.exe*

saphire.sao: saphire.sa saphire
	env LD_LIBRARY_PATH=. ./saphire -rn -c 'compile saphire.sa'
#	env LD_LIBRARY_PATH=$(PWD) ./saphire -rn -c 'compile saphire.sa'

sash: config.h main.c readline.o $(LIBSAPHIRESO) $(CYGWIN_DLL)
	if echo $(CFLAGS) | grep -q pg; then $(CC) -o sash main.c -DENABLE_READLINE readline.o -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -DSYSTEM_MIGEMODIR=\"$(SYSTEM_MIGEMODIR)\" $(OBJ) $(LIBS) $(CFLAGS); else $(CC) -o sash main.c -DENABLE_READLINE readline.o -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -DSYSTEM_MIGEMODIR=\"$(SYSTEM_MIGEMODIR)\" -lsaphire $(LIBS) $(CFLAGS); fi

saphire: config.h main.c $(LIBSAPHIRESO) $(CYGWIN_DLL)
	if echo $(CFLAGS) | grep -q pg; then $(CC) -o saphire main.c -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" $(LIBS2) $(CFLAGS) $(OBJ); else $(CC) -o saphire main.c -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -lsaphire $(LIBS2) $(CFLAGS); fi

readline.o: readline.c
	$(CC) -c readline.c -DDATADIR=\"$(datadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" -DDOCDIR=\"$(docdir)\" -DSYSTEM_MIGEMODIR=\"$(SYSTEM_MIGEMODIR)\" $(CFLAGS)

saphire_main.o: saphire_main.c saphire.h saphire_inner.h config.h
	$(CC) -c saphire_main.c -DSYSCONFDIR=\"$(sysconfdir)\" -DDOCDIR=\"$(docdir)\" $(CFLAGS)

########################################################
# saphire ライブラリ
########################################################
libsaphire.so.1.0: $(OBJ)
	gcc -shared -o libsaphire.so.1.0 $(OBJ) -lc $(LIBS2) $(CFLAGS)

libsaphire.so: libsaphire.so.1.0
	cp libsaphire.so.1.0 libsaphire.so.1
	cp libsaphire.so.1.0 libsaphire.so

########################################################
# saphire ライブラリ(Darwin)
########################################################
libsaphire.1.0.dylib: $(OBJ)
	gcc -dynamiclib -o libsaphire.1.0.dylib $(OBJ) -lc $(LIBS2) $(CFLAGS)

libsaphire.dylib: libsaphire.1.0.dylib
	cp libsaphire.1.0.dylib libsaphire.1.dylib
	cp libsaphire.1.0.dylib libsaphire.dylib

#########################################################
# cygwin dll
#########################################################
$(CYGWIN_DLL): $(OBJ)
	gcc -shared -o saphire.dll $(OBJ) $(LIBS2) $(CFLAGS)

#########################################################
# インストール
#########################################################
install:
	if [ -z "$(DESTDIR)" ]; then make normal-install; else make dest-install; fi

normal-install:
	mkdir -p "$(bindir)"
	mkdir -p "$(sysconfdir)"
	mkdir -p "$(libdir)"
	mkdir -p "$(includedir)"
	mkdir -p "$(docdir)"
	install -m 644 saphire_curses.h "$(includedir)"
	install -m 644 saphire_debug.h "$(includedir)"
	install -m 644 saphire_extra.h "$(includedir)"
	install -m 644 saphire_hash.h "$(includedir)"
	install -m 644 saphire_kanji.h "$(includedir)"
	install -m 644 saphire_list.h "$(includedir)"
	install -m 644 saphire_string.h "$(includedir)"
	install -m 644 saphire_vector.h "$(includedir)"
	install -m 644 saphire.h "$(includedir)"
	install -m 644 USAGE.ja.txt "$(docdir)"
	install -m 644 USAGE.en.txt "$(docdir)"
	install -m 644 README.ja.txt "$(docdir)"
	install -m 644 README.en.txt "$(docdir)"
	install -m 644 CHANGELOG.txt "$(docdir)"
	rm -f $(bindir)/saphire
	if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 saphire "$(bindir)"; else install -s -m 755 saphire "$(bindir)"; fi;
	rm -f "$(bindir)/sash"
	if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 sash "$(bindir)"; else install -s -m 755 sash "$(bindir)"; fi;
	install -m 644 saphire.sa "$(sysconfdir)"
	install -m 644 saphire.sao "$(sysconfdir)"

dest-install:
	mkdir -p "$(DESTDIR)/$(bindir)";
	mkdir -p "$(DESTDIR)/$(sysconfdir)";
	mkdir -p "$(DESTDIR)/$(libdir)"
	mkdir -p "$(DESTDIR)/$(includedir)"
	mkdir -p "$(DESTDIR)/$(docdir)"
	install -m 644 saphire_curses.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_debug.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_extra.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_hash.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_kanji.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_list.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_string.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire_vector.h "$(DESTDIR)/$(includedir)"
	install -m 644 saphire.h "$(DESTDIR)/$(includedir)"
	install -m 644 USAGE.ja.txt "$(DESTDIR)/$(docdir)"
	install -m 644 USAGE.en.txt "$(DESTDIR)/$(docdir)"
	install -m 644 README.ja.txt "$(DESTDIR)/$(docdir)"
	install -m 644 README.en.txt "$(DESTDIR)/$(docdir)"
	install -m 644 CHANGELOG.txt "$(DESTDIR)/$(docdir)"
	rm -f "$(DESTDIR)/$(bindir)/saphire"
	if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 saphire "$(DESTDIR)/$(bindir)"; else install -s -m 755 saphire "$(DESTDIR)/$(bindir)"; fi;
	rm -f "$(DESTDIR)/$(bindir)/sash"
	if echo $(CFLAGS) | grep -q MDEBUG; then install -m 755 sash "$(DESTDIR)/$(bindir)"; else install -s -m 755 sash "$(DESTDIR)/$(bindir)"; fi;
	install -m 644 saphire.sa "$(DESTDIR)/$(sysconfdir)"
	install -m 644 saphire.sao "$(DESTDIR)/$(sysconfdir)"

