#####################################################################
## 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-19.0.2
JAVAHOME = \Java\jdk-20
#GROOVYHOME = /etc/groovy-4.0.9
#GROOVYHOME = /etc/groovy-4.0.10
GROOVYHOME = /etc/groovy-4.0.12
##############################
  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-19
  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 *.log *.class *.groovy

### Windows only, コードページの切り替え（UTF-8、Code page switching）
#	UTF-8	65001
#	MS932	932
#	US-ASCII	20127
utf8 :
	chcp 65001
ms932 :
	chcp 932
##############################
hello :
	$(PLUS) sample/UTF8hello.awk sample/UTF8hello.txt こんにちわ 안녕하세요 你好

longPath :
	$(PLUS) sample/LongPath.awk sample/UTF8hello.txt
nano : longPath
	$(PLUS) sample/nanoTools.awk
nanoA :
	$(PLUS) sample/nanoAverage.awk ./Tave.tmp
nanoC :
# nanoCSV -v redirect file/wildcard
	$(PLUS) sample/nanoCSV.awk -v redirect=./Tcsv01.tmp	"X:/"
# csv -Fs -v OFS=s -v col=<c1...> [-v fmt=<formats>] file...
	$(PLUS) sample/csv.awk -F\t -v OFS=\t -v col=2,0 ./Tcsv01.tmp >./Tcsv02.tmp
# sort [-r] [-i] [-u] file...
	$(PLUS) sample/sort.awk -r	./Tcsv02.tmp >./Tcsv03.tmp
# csv -Fs -v OFS=s -v  -v del=<d1...> col=<c1...> file...
	$(PLUS) sample/csv.awk -F\t -v OFS=\t -v del=1 -v col=0	./Tcsv03.tmp >./Tcsv04.tmp
	$(PLUS) sample/cat.awk	./Tcsv04.tmp
nanoM :
# nanoMove.awk -v MAX_PATH=260　redirect
	$(PLUS) sample/nanoMove.awk -v MAX_PATH=19 ./Tmove01.tmp
nanoT :
	$(PLUS) sample/nanoTest.awk

nanoIE :	# (async copy)
	$(PLUS) sample/nanoAsyncIE.awk -v DISPLAY_WIDTH=35 -v USB=nano
nanoAC :	# (async copy)
	$(PLUS) sample/nanoAsyncC.awk -v DISPLAY_WIDTH=35
nanoAM :	# (async move)
	$(PLUS) sample/nanoAsyncM.awk -v DISPLAY_WIDTH=35
nanoAX :	# (async move)
	$(PLUS) sample/nanoAsyncX.awk -v DISPLAY_WIDTH=35
# nanoShell
main :	Main.class
	$(JAVA) Main ./
	$(JAVA) Main //princess7/m/ミュージック/

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 :
	chcp 932
	echo // UTF8 to SJIS
	$(PLUS) sample/code.awk -v stdout=file:///code1.tmp#SJIS sample/One.txt
	TYPE code1.tmp
code2 :
	chcp 65001
	echo // UTF8 to UTF8
	$(PLUS) sample/code.awk -v ORS="\r\n" -v stdout=file:///code1.tmp#UTF-8 sample/One.txt
	TYPE code1.tmp

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

# MATRIX I
matrix :
	$(PLUS) sample/matrix.awk -v part=1
matrixI :
	$(PLUS) sample/matrix.awk -v part=1 -v alarm=1
# MATRIX II
matrix45 :
	$(PLUS) sample/matrix.awk -v part=2 -v alarm=45
matrixII :
	$(PLUS) sample/matrix.awk -v part=2 -v alarm=1
# MATRIX html I
matrixH1 :
	$(PLUS) sample/matrixHTML.awk -v part=1 -v loop=100	>MATRIX1.html
# MATRIX html II
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

###
#	エンコーディングを行うためには、ファイル名を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)
cat1 :
	echo ## Read(UTF-8) -> STDOUT(Terminal): OS. Platform-dependent encoding.(e.g. MS932)
	$(PLUS) sample/One.awk sample/One.txt
cat2 :
	echo ## Read(UTF-8) -> STDOUT(Redirect, e.g. MS932)
	$(PLUS) sample/One.awk sample/One.txt >tmp/MS932.txt
cat3 :
	echo ## Read(MS932) -> Terminal(e.g. MS932)
	$(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
