From f0513dce170c0d605536f68a27fd4f8fa91e9da9 Mon Sep 17 00:00:00 2001
From: Luca Bilke <bilke@tralios.de>
Date: Fri, 17 May 2024 12:03:44 +0200
Subject: [PATCH] Rework build process

---
 .forgejo/workflows/build.yml                  | 11 +---
 .gitignore                                    | 24 ++++++++
 Makefile                                      | 59 +++++++++++--------
 build.sh                                      | 20 -------
 config.h => config/config.h                   |  0
 config.mk => config/config.mk                 |  0
 patches.h => config/patches.h                 |  0
 .../patches}/default-class-name.diff          |  0
 .../patches}/font-features.diff               |  0
 {patches => config/patches}/histsize.diff     |  0
 10 files changed, 59 insertions(+), 55 deletions(-)
 delete mode 100755 build.sh
 rename config.h => config/config.h (100%)
 rename config.mk => config/config.mk (100%)
 rename patches.h => config/patches.h (100%)
 rename {patches => config/patches}/default-class-name.diff (100%)
 rename {patches => config/patches}/font-features.diff (100%)
 rename {patches => config/patches}/histsize.diff (100%)

diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml
index d650120..2c63b1a 100644
--- a/.forgejo/workflows/build.yml
+++ b/.forgejo/workflows/build.yml
@@ -36,20 +36,13 @@ jobs:
       - name: Build
         run: |
           set -xeu
-          cp -f config.mk Makefile patches.h st-flexipatch/
-          flexipatch-finalizer/flexipatch-finalizer.sh -r -d st-flexipatch -o st-final
-          for patch in patches/*.diff; do 
-            patch -d st-final <"$patch"
-          done
-          cp -f config.h st-final/
-          cd st-final && make clean install DESTDIR="${GITHUB_WORKSPACE}/pkg" PREFIX="/usr"
+          make clean install DESTDIR="${GITHUB_WORKSPACE}/pkg" PREFIX="/usr"
 
       - name: Create package
         run: |
           set -xeu
           export XBPS_TARGET_ARCH=${{ env.ARCH }}
-          mkdir /target
-          cd /target || exit 1
+          mkdir /target && cd /target || exit 1
           xbps-create -A ${{ env.ARCH }} \
             -H "${{ github.server_url }}/${{ github.repository }}" \
             -l "${{ env.LICENSE }}" \
diff --git a/.gitignore b/.gitignore
index caf9763..293ca20 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,27 @@
 st-final
 pkg
 *.xbps
+patch
+FAQ
+LEGACY
+LICENSE
+README
+TODO
+arg.h
+config.def.h
+config.h
+config.mk
+hb.c
+hb.h
+sixel.c
+sixel.h
+sixel_hls.c
+sixel_hls.h
+st.1
+st.c
+st.desktop
+st.h
+st.info
+win.h
+x.c
+terminfo/s
diff --git a/Makefile b/Makefile
index 9eb2abc..406db1d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,7 @@
 # st - simple terminal
 # See LICENSE file for copyright and license details.
+.PHONY: all clean install uninstall
+
 .POSIX:
 
 include config.mk
@@ -7,41 +9,46 @@ include config.mk
 SRC = st.c x.c $(LIGATURES_C) $(SIXEL_C)
 OBJ = $(SRC:.c=.o)
 
-all: st
+all: st terminfo
+
+# TODO: Update command to update submodules
+
+clean:
+	find . -maxdepth 1 -type f  | grep -Pv "^\./\.|Makefile$$" | xargs -r rm
+	rm -r tmp patch terminfo 2>/dev/null || true
+	git -C st-flexipatch reset --hard HEAD
+	git -C st-flexipatch clean -fdx
+
+buildroot:
+	cp config/patches.h st-flexipatch/patches.h
+	flexipatch-finalizer/flexipatch-finalizer.sh -r -d st-flexipatch -o tmp
+	rm -r tmp/Makefile
+	mv tmp/* ./
+	for patch in config/patches/*.diff; do patch <"$$patch"; done
+
+config.mk:
+	cp config/config.mk ./config.mk
 
 config.h:
-	cp config.def.h config.h
-
-patches.h:
-	cp patches.def.h patches.h
+	cp config/config.h ./config.h
 
+*.c: buildroot
+*.h: buildroot
 .c.o:
 	$(CC) $(STCFLAGS) -c $<
 
-st.o: config.h st.h win.h
-x.o: arg.h config.h st.h win.h $(LIGATURES_H)
+st.o: buildroot config.h st.h win.h
+x.o: buildroot arg.h config.h st.h win.h $(LIGATURES_H)
 
-$(OBJ): config.h config.mk patches.h
+$(OBJ): config.h config.mk buildroot
 
-st: $(OBJ)
+st: buildroot $(OBJ)
 	$(CC) -o $@ $(OBJ) $(STLDFLAGS)
 
-clean:
-	rm -f st $(OBJ) st-$(VERSION).tar.gz
-
-dist: clean
-	mkdir -p st-$(VERSION)
-	cp -R FAQ LEGACY TODO LICENSE Makefile README config.mk\
-		config.def.h st.info st.1 arg.h st.h win.h $(LIGATURES_H) $(SRC)\
-		st-$(VERSION)
-	tar -cf - st-$(VERSION) | gzip > st-$(VERSION).tar.gz
-	rm -rf st-$(VERSION)
-
-terminfo:
-	mkdir terminfo
+terminfo: buildroot
 	tic -sx -o terminfo st.info
 
-install: st
+install: st terminfo
 	mkdir -p $(DESTDIR)$(PREFIX)/bin
 	cp -f st $(DESTDIR)$(PREFIX)/bin
 	chmod 755 $(DESTDIR)$(PREFIX)/bin/st
@@ -50,9 +57,11 @@ install: st
 	chmod 644 $(DESTDIR)$(MANPREFIX)/man1/st.1
 	mkdir -p $(DESTDIR)$(PREFIX)/share
 	cp -a terminfo $(DESTDIR)$(PREFIX)/share/terminfo
+	chmod 755 $(DESTDIR)$(PREFIX)/share/terminfo/s
+	chmod 644 $(DESTDIR)$(PREFIX)/share/terminfo/s/st-*
 	# mkdir -p $(DESTDIR)$(PREFIX)/share/applications # desktop-entry patch
 	# test -f ${DESTDIR}${PREFIX}/share/applications/st.desktop || cp -n st.desktop $(DESTDIR)$(PREFIX)/share/applications # desktop-entry patch
-
+	
 uninstall:
 	rm -f $(DESTDIR)$(PREFIX)/bin/st
 	rm -f $(DESTDIR)$(MANPREFIX)/man1/st.1
@@ -64,5 +73,3 @@ uninstall:
 	rm -f $(DESTDIR)$(PREFIX)/share/terminfo/s/st-meta
 	rm -f $(DESTDIR)$(PREFIX)/share/terminfo/s/st-meta-256color
 	rm -f $(DESTDIR)$(PREFIX)/share/terminfo/s/st-mono
-
-.PHONY: all clean dist install uninstall
diff --git a/build.sh b/build.sh
deleted file mode 100755
index 4052d2b..0000000
--- a/build.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/sh
-
-cp -f config.mk Makefile patches.h st-flexipatch/
-flexipatch-finalizer/flexipatch-finalizer.sh -r -d st-flexipatch -o st-final
-git -C st-flexipatch reset --hard HEAD && git -C st-flexipatch clean -fd
-for patch in patches/*.diff; do
-    patch -d st-final <"$patch"
-done
-cp -f config.h st-final/
-cd st-final || exit 1
-
-if [ "$1" = "-i" ]; then
-    sudo make install
-elif [ "$1" = "-ti" ]; then
-    mkdir -p /usr/local/share/terminfo/s/
-    make terminfo
-    cp -a terminfo/s/* /usr/local/share/terminfo/s/
-else
-    make
-fi
diff --git a/config.h b/config/config.h
similarity index 100%
rename from config.h
rename to config/config.h
diff --git a/config.mk b/config/config.mk
similarity index 100%
rename from config.mk
rename to config/config.mk
diff --git a/patches.h b/config/patches.h
similarity index 100%
rename from patches.h
rename to config/patches.h
diff --git a/patches/default-class-name.diff b/config/patches/default-class-name.diff
similarity index 100%
rename from patches/default-class-name.diff
rename to config/patches/default-class-name.diff
diff --git a/patches/font-features.diff b/config/patches/font-features.diff
similarity index 100%
rename from patches/font-features.diff
rename to config/patches/font-features.diff
diff --git a/patches/histsize.diff b/config/patches/histsize.diff
similarity index 100%
rename from patches/histsize.diff
rename to config/patches/histsize.diff