#####################################################################
## AWK~Plus.	MakeFile: UTF-8
## Usage: make [-f makefile] [Target...]
#####################################################################
empty =
ifeq ("$(OS)", "Windows_NT")	# OS依存の変数を設定
  PS ?= ;
  FS ?= \$(empty)
##Please correct the Path ####
# JAVAHOME = \Java\jdk-17.0.4.1
JAVAHOME = \Java\jdk-18.0.2.1
# GROOVYHOME = /etc/groovy-4.0.4
GROOVYHOME = /etc/groovy-4.0.5
##############################
  JAVA =	$(JAVAHOME)\bin\java
  JAVAC =	$(JAVAHOME)\bin\javac
  GAWK =	bin\gawk
  DIFF =	FC
  isExist =	CMD /C 2>NUL DIR /B
  RM = 		CMD /C 2>NUL DEL /Q
  RMR = 	CMD /C 2>NUL 1>NUL DEL /S/Q
  RMDIR =	CMD /C 2>NUL RD /S/Q
else
  PS ?= :
  FS ?= /
  JAVAHOME = ~/Java/jdk-18.0.2.1
  GROOVYHOME = ~/etc/groovy-4.0.4
  JAVA =	java
  JAVAC =	javac
  GAWK =	gawk
  DIFF =	diff --strip-trailing-cr
  isExist =	ls 2>/dev/null
  RM = 		rm -f
  RMR = 	rm -f -r
  RMDIR =	rmdir
endif
# Environment variable
export JAVA_HOME = $(JAVAHOME)
#
PLUSLIB = lib/awk-plus.jar
GENLIB = lib/*$(PS)$(GROOVYHOME)/lib/*
#
# Commands
PLUS =	$(JAVA) -jar $(PLUSLIB)
PLUSL =	$(JAVA) -jar $(PLUSLIB) -lib .
PLUSX =	$(JAVA) --add-exports=java.desktop/sun.awt=ALL-UNNAMED -jar $(PLUSLIB)
GEN =	$(JAVA) -cp $(GENLIB) plus.Gen
RUN =	$(GROOVYHOME)/bin/groovy -cp $(GENLIB) --encoding=utf-8
COMPILE = $(GROOVYHOME)/bin/groovyc -cp $(GENLIB) --encoding=utf-8
GO =	$(JAVA) -cp .$(PS)$(GENLIB)

INETSAMPLE = sample/inet
vpath %.awk sample test $(INETSAMPLE)
vpath %.java sample
vpath %.groovy sample

%.class : %.java
	$(JAVAC) -encoding utf-8 -d . $?
%.class : %.groovy
	$(COMPILE) $?
%.groovy : %.awk
	$(GEN) $<

all : info
clean :
	-$(RMR) tmp$(FS)*
	-$(if $(shell $(isExist) tmp$(FS)test*),@$(RMDIR) tmp$(FS)test,)
	-$(RM) *.tmp *.class *.groovy

##############################
csv1 :
	$(PLUS) sample/csv.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" sample/csvTest.csv
csv2 :
	$(PLUS) sample/csv.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" -v stdout=csvout1.tmp sample/csvTest.csv
	$(PLUS) sample/csv.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" -v stdout=file:///csvout2.tmp#SJIS sample/csvTest.csv
	$(PLUS) sample/csv.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" sample/csvTest.csv	>csvout3.tmp
csv3 :
	$(GAWK) -f sample/csvTrad.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" -v stdout=csvout4.tmp sample/csvTest.csv
	$(GAWK) -f sample/csvTrad.awk -v col=2,2,1 -v fmt="04d, 4d,-4s" sample/csvTest.csv	>csvout5.tmp

code :
	echo // UTF8 to SJIS
	$(PLUS) sample/code.awk sample/One.txt
	$(PLUS) sample/code.awk sample/One.txt >code1.tmp
	TYPE code1.tmp
	echo // SJIS to SJIS
	$(PLUS) sample/code.awk file:///code1.tmp#SJIS
	echo // SJIS to UTF8
	$(PLUS) sample/code.awk -v stdout=file:///code2.tmp file:///code1.tmp#SJIS
	TYPE code2.tmp

eval :		# eval: [-e] '<code>'
	$(PLUS) -e "BEGIN{ print 'Hello World' }"
	$(PLUS) "BEGIN{ print 'Hello World!!!' }"

matrix :
	$(PLUS) sample/matrix.awk -v part=1
# MATRIX I
matrix45 :
	$(PLUS) sample/matrix.awk -v part=2 -v alarm=45
matrixI :
	$(PLUS) sample/matrix.awk -v part=1 -v alarm=1
# MATRIX II
matrixII :
	$(PLUS) sample/matrix.awk -v part=2 -v alarm=1

# MATRIX I
matrixH1 :
	$(PLUS) sample/matrixHTML.awk -v part=1 -v loop=100	>MATRIX1.html
# MATRIX III
matrixH2 :
	$(PLUS) sample/matrixHTML.awk -v part=2 -v loop=100 -v speed=1	>MATRIX2.html
	
cawsay :
	$(PLUS) sample/CowsayESC.awk -v eye="o-" "Hello World"
pom :
	$(PLUS) sample/pom.awk https://apod.nasa.gov/apod/
pipe :
	$(PLUS) sample/RSS(S1).awk https://www.nikkansports.com/rss/baseball/professional/atom/tigers.xml | \
	$(PLUS) sample/sort.awk - | \
	$(PLUS) sample/RSS(S2).awk -

##############################
# チュートリアル(Tutorial & Test)
Ti :	T1i T2i T3i T4i T5i T6i T7i T8i T9i T10i
Tg :	T1g T2g T3g T4g T5g T6g T7g T8g T9g T10g

# ユーティリティ(Utility)
Ui :	T11i
Ug :	T11g

# 応用(Application)
Ai :	1i 20i atomi atom2i imgi
Ag :	1g 20g       atom2g imgg

T1i :
	$(PLUS) sample/T1Classical.awk
T1g :	T1Classical.groovy
	$(RUN) T1Classical.groovy
T2i :
	$(PLUS) sample/T2SideEffects.awk
T2g :	T2SideEffects.groovy
	$(RUN) T2SideEffects.groovy
T3i :
	$(PLUS) sample/T3Property.awk
T3g :	T3Property.groovy
	$(RUN) T3Property.groovy
T4i :
	$(PLUS) sample/T4Closure.awk
T4g :	T4Closure.groovy
	$(RUN) T4Closure.groovy
T5i :
	$(PLUS) sample/T5Mutable.awk
T5g :	T5Mutable.groovy
	$(RUN) T5Mutable.groovy
T6i :
	$(PLUS) sample/T6VarArgs.awk
T6g :	T6VarArgs.groovy
	$(RUN) T6VarArgs.groovy
T7i :
	$(PLUS) sample/T7Fibonacci.awk
T7g :	T7Fibonacci.groovy
	$(RUN) T7Fibonacci.groovy
T8i :
	$(PLUS) sample/T8GroovyType.awk -F\t -v S1=' 12' -vS2=345 -vN1=1.5 -vN2='67X' ./T8=.txt FS= './T8= .txt'
T8g :	T8GroovyType.groovy
	$(RUN) T8GroovyType.groovy -F\t -v S1=' 12' -vS2=345 -vN1=1.5 -vN2='67X' ./T8=.txt FS= './T8= .txt'
T9i :
	$(PLUS) sample/T9Escape.awk DISESC=1 sample/T9Escape.txt
	$(PLUS) sample/T9Escape.awk sample/T9Escape.txt
T9g :	T9Escape.class
	$(GO) T9Escape DISESC=1 sample/T9Escape.txt
	$(GO) T9Escape sample/T9Escape.txt
T10i :
	$(PLUS) sample/T10Progress.awk
T10g :	T10Progress.groovy
	$(RUN) T10Progress.groovy

### Windows only, コードページの切り替え（UTF-8、Code page switching）
#	UTF-8	65001
#	MS932	932
#	US-ASCII	20127
utf8 :
	chcp 65001
###
#	エンコーディングを行うためには、ファイル名をURL形式で指定します。URLパスに、# を含む場合は、エンコーディングを省略できません.
#	To encode, specify the file name in URL format.
#	If the URL path contains #, the encoding cannot be omitted.

#	Encoding: UTF-8 -> UTF-16 -> Terminal(Platform dependent, UTF-8)
T11i :
	$(PLUS) sample/T11Encode.awk OUT=file:///tmp/#UTF-16.txt#UTF-16 file:///sample/One.txt#UTF-8
	$(PLUS) sample/T11Encode.awk file:///tmp/#UTF-16.txt#UTF-16
T11g :	T11Encode.class
	$(GO) T11Encode OUT=file:///tmp/#UTF-16.txt#UTF-16 file:///sample/One.txt#UTF-8
	$(GO) T11Encode file:///tmp/#UTF-16.txt#UTF-16

### デフォルトエンコーディングの確認(Check the default encoding)
# Read(UTF-8) -> STDOUT(Terminal): OS. Platform-dependent encoding.(e.g. MS932)
cat1 :
	$(PLUS) sample/One.awk sample/One.txt
# Read(UTF-8) -> STDOUT(Redirect, e.g. MS932)
cat2 :
	$(PLUS) sample/One.awk sample/One.txt >tmp/MS932.txt
# Read(MS932) -> Terminal(e.g. MS932)
cat3 :
	$(PLUS) sample/One.awk file:///tmp/MS932.txt#MS932

###
atomi :
	$(PLUS) sample/Atomic.awk
atom2i :
	$(PLUS) sample/Atomic2.awk
atom2g :	Atomic2.groovy
	$(RUN) Atomic2.groovy
20i :
	$(PLUS) sample/EratosthenesP.awk 20
20g :	EratosthenesP.groovy
	$(RUN) EratosthenesP.groovy 20

# Bench mark
bench :	EratosthenesJ.class Eratosthenes.groovy EratosthenesP.groovy
	$(GAWK) -f sample/Eratosthenes.awk 100000 >tmp/1.log
	$(PLUS) sample/Eratosthenes.awk 100000 >tmp/2.log
	$(PLUS) sample/EratosthenesP.awk 100000 >tmp/3.log
	$(JAVA) EratosthenesJ 100000 >tmp/4.log
	$(RUN) Eratosthenes.groovy 100000 >tmp/5.log
	$(RUN) EratosthenesP.groovy 100000 >tmp/6.log
	@$(DIFF) tmp$(FS)1.log tmp$(FS)2.log
	@$(DIFF) tmp$(FS)1.log tmp$(FS)3.log
	@$(DIFF) tmp$(FS)1.log tmp$(FS)4.log
	@$(DIFF) tmp$(FS)1.log tmp$(FS)5.log
	@$(DIFF) tmp$(FS)1.log tmp$(FS)6.log

# イメージビュアー(woman)
# (Interpreter)
imgi :
	$(PLUSX) sample/ImageViewer.awk sample/picture/cosPlay 24
# (Generate&GO)
imgg :	ImageViewerG.groovy
	$(RUN) ImageViewerG.groovy sample/picture/cosPlay 24
# (Generate&Compile&GO)
imgc :	ImageViewerG.class
	$(GO) ImageViewerG sample/picture/cosPlay 24

# 世界で一番短いコード '1' (Cat)
0 :	# Ctrl+Z(linuxは、Ctrl+D)で終了
	$(PLUS) sample/One.awk -
1i :
	$(PLUS) sample/One.awk sample/One.txt sample/One2.txt
1g:	One.groovy
	$(RUN) One.groovy sample/One.txt sample/One2.txt

## Web ##########################
# AWK~Plusのファイル名は、httpを指定できます
https :
	$(PLUSL) sample/One.awk "https://www.google.com/"
https2 :
	$(PLUSL) sample/One.awk https://news.yahoo.com/
http :
	$(PLUS) sample/One.awk "http://awk4j.osdn.jp/home.html"
# RSS(XML)を整形します
rss :
	$(PLUS) sample/RSS.awk http://www.nikkansports.com/rss/baseball/professional/tigers.rdf
rss2 :
	$(PLUS) sample/RSS2.awk http://www.nikkansports.com/rss/baseball/professional/tigers.rdf
# TCP/IPでローレベルアクセスをします
query :
	$(PLUS) $(INETSAMPLE)/Query.awk www.google.co.jp /search?q="AWK~Plus"

## INET #########################
# 公開 daytime サーバに接続
daytime :
	$(PLUS) $(INETSAMPLE)/Daytime.awk /inet/tcp/0/time.xmission.com/daytime/#US-ASCII
daytimeg :	Daytime.groovy
	$(RUN) Daytime.groovy /inet/tcp/0/time.xmission.com/daytime/#US-ASCII
# UDPサーバを先に起動しておく
udps :
	$(PLUS) $(INETSAMPLE)/DaytimeServer.awk /inet/udp/0/localhost/8813
# UDPクライアント 受信待ち: 15秒
udp :
	$(PLUS) $(INETSAMPLE)/Daytime.awk /inet/udp/8813/localhost/0/15/#US-ASCII
tcps :
# TCP/IPサーバを先に起動しておく
	$(PLUS) $(INETSAMPLE)/DaytimeServer.awk /inet/tcp/8813/localhost/0
# TCP/IPクライアント
tcp :
	$(PLUS) $(INETSAMPLE)/Daytime.awk /inet/tcp/0/localhost/8813

# Echo UDPサーパ
udpes :
	$(PLUS) $(INETSAMPLE)/EchoServer.awk /inet/udp/8807/localhost/0
# Echo UDPクライアント
udpe :
	$(PLUS) $(INETSAMPLE)/Echo.awk /inet/udp/0/localhost/8807
# Echo TCP/IPサーバ
tcpes :
	$(PLUS) $(INETSAMPLE)/EchoServer.awk /inet/tcp/8807/localhost/0
# Echo TCP/IPクライアント
tcpe :
	$(PLUS) $(INETSAMPLE)/Echo.awk /inet/tcp/0/localhost/8807

## バージョンの確認 ###################
info:
	@echo ."----------------------------------------------------------------------------"
	@echo $(PATH)
	-$(JAVA) -version
	@echo .
	-$(GROOVYHOME)/bin/groovy -v
