#---------------------------------------------------------------------
#     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
#---------------------------------------------------------------------
#= Export source codes from cvsroot, and make debian pakage.
#
# * Yasuhiro MORIKAWA
#   * 2006/03/17 (Yasuhiro MORIKAWA) Create from Makefile.export
#
######################################################################
#
#== Settings
#
# [JAPANESE] 各種設定項目

INCLUDEFILE = Latest_CVS_Tag.mk
include $(INCLUDEFILE)  # Include file              (optional)
                        # [JA] インクルードファイル (任意)

CVS_ROOT = /GFD_Dennou_Club/ftp/arch/cc-env/dcreal/cvsroot
                        # cvsroot                     (necessary)
                        # [JA] cvsroot のディレクトリ (必須)

CURNAME  = $(PACKAGE)_current
                       # Latest symbolic link             (optional)
                       # [JA] 最新版につけるシンボリック
                       #      リンク名. 空の場合, シンボ
                       #      リックリンクは作成しない    (任意)

TGZLIST  =
#TGZLIST  = /GFD_Dennou_Club/ftp/arch/dcmodel/bin/tgzlist-html.rb
                       # "tgzlist-html.rb" command that create
                       # archive list HTML                      (optional)
                       # [JA] HTML のアーカイブリストを作成する
                       #      "tgzlist-html.rb" へのパス.
                       #      空の場合, リストは作成されない.   (任意)

OVERWRITE = 0
                       # Overwrite mode                (optional)
                       # [JA] 1 にすると, 既に DIR が
                       #      存在しても上書きする     (任意)

CLEANDIR = 0
                       # Clean directory                 (optional)
                       # [JA] 1 にすると, 展開したディレク
                       #      トリを削除する.            (任意)

CVS      = cvs
                       # cvs command         (necessary)
                       # [JA] cvs へのパス   (必須)

BUILDPKG = dpkg-buildpackage -uc -us -tc -rfakeroot
                       # dpkg-buildpackage command       (necessary)
                       # [JA] dpkg-buildpackage へのパス (必須)

GATE     = /usr/local/bin/gate-user-show
                       # Path to "gate-user-show"       (optional)
                       # [JA] "gate-user-show" へのパス (任意)
                       #      空の場合, SIGEN ファイルを作成しない

# End Settings  [JA] 設定項目ここまで
######################################################################
#== Directory in which files are located
DIR      = $(PACKAGE)-$(VERSION)

#== Suffix of archive
TGZ      = tar.gz

#== Generated tgz file name
TGZNAME  = $(PACKAGE)_$(VERSION).$(TGZ)

#== Generated debian package file
DEBNAME  = $(PACKAGE)_$(VERSION)_xxxx.deb

#== SIGEN file
#
SIGENDIR = $(DIR).SIGEN
SIGENTGZ = $(TGZNAME).SIGEN

######################################################################
#== Get Japanese name from gate-toroku-system
#
GATEJPNAME=`$(GATE) $(USER) | grep kname | cut -d":" -f 2`

######################################################################
#== Generate archive list by "tgzlist-html.rb"
#
TGZLIST_CMD = $(TGZLIST) \
		-o index --exclude $(CURNAME).$(TGZ),$(CURNAME)

######################################################################
#== Rules
#
all: debian


help:
	@echo ""
	@echo "  make [debian] :"
	@echo "             - Import information from $(INCLUDEFILE). "
	@echo "             - Export $(DIR) from cvsroot. "
	@echo "             - Expand source in" $(DIR) "."
	@echo "             - Compress them into" $(TGZNAME) "."
	@echo "             - Generate debian package" $(DEBNAME) "."
	@if [ "$(CURNAME)" != "" ] ; then \
	 echo "             - Create symbolic links for latest version." ;\
	fi
	@if [ "$(GATE)" != "" ] && [ "$(CLEANARCH)" != "1" ]; then \
	 echo "             - Create SIGEN files" $(SIGENDIR) $(SIGENTGZ) ;\
	fi
	@if [ "$(TGZLIST)" != "" ]; then \
	 echo "             - Update Archive List. " ;\
	fi
	@echo ""
	@echo "  make tgzlist :"
	@echo "             Only Update Archive List. "
	@echo ""

clean:
	rm -f *~

clean.all: clean

debian: varcheck tagcheck expand buildpkg symlink sigen tgzlist cleandir


varcheck:
	@if [ "$(INCLUDEFILE)" = "" ]; then \
	    echo -e "\n   Specify \"INCLUDEFILE\" in Makefile.debian \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(PACKAGE)" = "" ]; then \
	    echo -e "\n   Specify \"PACKAGE\" in $(INCLUDEFILE) \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(VERSION)" = "" ]; then \
	    echo -e "\n   Specify \"VERSION\" in $(INCLUDEFILE) \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(CVSTAG)" = "" ]; then \
	    echo -e "\n   Specify \"CVSTAG\" in $(INCLUDEFILE) \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(CVS)" = "" ]; then \
	    echo -e "\n   Specify \"CVS\" in Makefile.debian \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(CVS_ROOT)" = "" ]; then \
	    echo -e "\n   Specify \"CVS_ROOT\" in Makefile.debian \n" ;\
	    exit 1 ;\
	fi
	@if [ "$(BUILDPKG)" = "" ]; then \
	    echo -e "\n   Specify \"BUILDPKG\" in Makefile.debian \n" ;\
	    exit 1 ;\
	fi


tagcheck:
	@if [ -d "$(DIR)" ] && [ "$(OVERWRITE)" != "1" ]; then \
	    echo -e \
		"\n   Directory \"$(DIR)\" is already exist.\n" \
		"  Please change \"VERSION\" in \"$(INCLUDEFILE)\" to latest version.\n" \
		"  or remove \"$(DIR)\".\n" \
		"  And do \"make debian\" again.\n" ;\
	    exit 1 ;\
	fi
	@if [ -d "$(DIR)" ] && [ "$(OVERWRITE)" = "1" ]; then \
	    echo rm -rf $(DIR) ;\
	    rm -rf $(DIR) ;\
	fi


expand:
	$(CVS) -d $(CVS_ROOT) export $(CVSTAG) \
	       -d $(DIR)   $(PACKAGE) \
	|| \
	eval  " rm $(DIR)/CVS/*  ; \
		rmdir $(DIR)/CVS ; \
		rmdir $(DIR)     ; exit 1 "

buildpkg:
	cd $(DIR) ;\
	$(BUILDPKG)

symlink:
	@if [ "$(CURNAME)" != "" ] && [ "$(CLEANARCH)" != "1" ]; then \
	    if [ -L "$(CURNAME)" ] || [ -L "$(CURNAME).$(TGZ)"  ] ; then \
		echo rm -f $(CURNAME) $(CURNAME).$(TGZ)        ;\
		rm -f $(CURNAME) $(CURNAME).$(TGZ)        ;\
	    fi ;\
	    \
	    echo ln -s $(DIR)        $(CURNAME)         ;\
	    ln -s $(DIR)        $(CURNAME)         ;\
	    \
	    echo ln -s $(TGZNAME) $(CURNAME).$(TGZ)  ;\
	    ln -s $(TGZNAME) $(CURNAME).$(TGZ)  ;\
	fi

sigen: sigendir sigentgz

sigendir:
	@if [ "$(GATE)" != "" ] && [ "$(CLEANARCH)" != "1" ]; then \
	    echo -n "Generating $(SIGENDIR) ... " ;\
	    \
	    echo "Subject:	" $(DIR) "Source code directory" > $(SIGENDIR) ;\
	    echo "Maintainer:	" $(GATEJPNAME) >> $(SIGENDIR) ;\
	    echo "Description:	 This file is auto-generated form \"make debian -f Makefile.debian\"" >> $(SIGENDIR) ;\
	    echo "Note:		" >> $(SIGENDIR) ;\
	    echo "Update:		`date +%Y/%m/%d`" >> $(SIGENDIR) ;\
	    \
	    echo "done" ;\
	fi

sigentgz:
	@if [ "$(GATE)" != "" ] && [ "$(CLEANARCH)" != "1" ]; then \
	    echo -n "Generating $(SIGENTGZ) ... " ;\
	    \
	    echo "Subject:	" $(DIR) "tar.gz Package" > $(SIGENTGZ) ;\
	    echo "Maintainer:	" $(GATEJPNAME) >> $(SIGENTGZ) ;\
	    echo "Description:	 This file is auto-generated form \"make export -f Makefile.export\"" >> $(SIGENTGZ) ;\
	    echo "Note:		" >> $(SIGENTGZ) ;\
	    echo "Update:		`date +%Y/%m/%d`" >> $(SIGENTGZ) ;\
	    \
	    echo "done" ;\
	fi

tgzlist:
	@if [ "$(TGZLIST)" != "" ]; then \
	    echo $(TGZLIST_CMD) ;\
	    $(TGZLIST_CMD) ;\
	fi

cleandir:
	@if [ "$(CLEANDIR)" = "1" ] ; then \
	    echo rm -rf $(DIR) ;\
	    rm -rf $(DIR) ;\
	fi

######################################################################
#== Mode setting for Emacs
#Local Variables:
#mode: makefile
#End:
#
