~strahinja/ste

e3573f9d24f52aa9caddcb1028648cc6e2c216c8 — Страхиња Радић 4 months ago 5e16ae2 v0.6
Switch to POSIX make from redo; update copyright years

Signed-off-by: Страхиња Радић <contact@strahinja.org>
36 files changed, 223 insertions(+), 564 deletions(-)

M INSTALL
A Makefile
M README
D all.do
D clean.do
A config.mk
D config.redo
D default.do
D default.gz.do
D do
D install.do
R date.do => lib/makedate -rw-r--r-- => -rwxr-xr-x
A lib/makeversion
A lib/mkpack
A lib/replacepat
A lib/runtests
D pack.do
D rebuild.do
M ste.1.in
M ste.in
M transpose
M transpose.1.in
M tsvdel
M tsvdel.1.in
M tsvedit
M tsvedit.1.in
M tsvfind
M tsvfind.1.in
M tsvins
M tsvins.1.in
M tsvmove
M tsvmove.1.in
M tsvselect
M tsvselect.1.in
D uninstall.do
D version.do
M INSTALL => INSTALL +8 -13
@@ 24,28 24,23 @@ Install

	$ git clone https://git.sr.ht/~strahinja/ste
	$ cd ste
	$ su

If you have djb redo:

	# redo install

if you don't:

	# ./do install
	# make install


Persistent build configuration file
-----------------------------------

The file config.redo will be sourced when building targets which also accept
The file config.mk will be sourced when building targets which also accept
configuration through environment variables.  For example, PREFIX is specified
in config.redo.
in config.mk.


OpenBSD
-------

Change MANPREFIX to
Before running make(1), comment the MANPREFIX definition in config.mk above the
line with the "OpenBSD" comment and uncomment the one below it:

	MANPREFIX=$PREFIX/man
	#MANPREFIX	= $(PREFIX)/share/man
	# OpenBSD
	MANPREFIX	= $(PREFIX)/man

A Makefile => Makefile +54 -0
@@ 0,0 1,54 @@
.POSIX:

include config.mk

.SUFFIXES: .1.in .1

.1.in.1:
	sh lib/replacepat $< $@

all: makedate makeversion $(PROG) $(MANPAGES)
	if [ -f .rebuild ]; then rm .rebuild; make -e all; else true; fi

date:
	sh lib/makedate

version:
	sh lib/makeversion

makedate:
	sh lib/makedate -l

makeversion:
	sh lib/makeversion -l

$(MANPAGE): $(MANPAGE).in date version

$(PROG): $(PROG).in date version
	sh lib/replacepat $(PROG).in $@

clean:
	rm -f version date version.h *.xz *~ $(PROG) $(MANPAGES)

install: all
	$(INSTALL) -dm 0755 $(MANPREFIX)/man1
	$(INSTALL) -m 0644 $(MANPAGES) $(MANPREFIX)/man1
	$(INSTALL) -Dm 0644 README $(DOCDIR)/README
	$(INSTALL) -Dm 0644 LICENSE $(DOCDIR)/LICENSE
	$(INSTALL) -dm 0755 $(BINDIR)
	$(INSTALL) -m 0755 $(PROGS) $(BINDIR)

uninstall:
	cd $(MANPREFIX)/man1 && rm -f $(MANPAGES) && cd -
	rmdir $(MANPREFIX)/man1 2>/dev/null || true
	rmdir $(MANPREFIX) 2>/dev/null || true
	rm -f $(DOCDIR)/README
	rm -f $(DOCDIR)/LICENSE
	rmdir $(DOCDIR) 2>/dev/null || true
	cd $(BINDIR) && rm -f $(PROGS) && cd -
	rmdir $(BINDIR) 2>/dev/null || true

pack: all
	sh -x lib/mkpack

.PHONY: clean install makedate makeversion pack uninstall

M README => README +1 -1
@@ 23,7 23,7 @@ License
-------

ste - Simple table editor
Copyright (C) 2023  Страхиња Радић
Copyright (C) 2023-2024  Страхиња Радић

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software

D all.do => all.do +0 -2
@@ 1,2 0,0 @@
. ./config.redo
redo-ifchange ste $MANPAGES

D clean.do => clean.do +0 -3
@@ 1,3 0,0 @@
. ./config.redo
redo-always
rm -f ste *.1 $MANPAGES *~ version date

A config.mk => config.mk +14 -0
@@ 0,0 1,14 @@
INSTALL		= install
PREFIX		= /usr/local
PROG		= ste
PROGS		= ste transpose tsvdel tsvedit tsvfind tsvins tsvmove tsvselect

# Derived macros
BINDIR		= $(PREFIX)/bin
DOCDIR		= $(PREFIX)/share/doc/$(PROG)
MANPREFIX	= $(PREFIX)/share/man
# OpenBSD
#MANPREFIX	= $(PREFIX)/man
MANPAGE		= $(PROG).1
MANPAGES	= $(MANPAGE) transpose.1 tsvdel.1 tsvedit.1 tsvfind.1 \
		  tsvins.1 tsvmove.1 tsvselect.1

D config.redo => config.redo +0 -12
@@ 1,12 0,0 @@
PREFIX=${PREFIX:-/usr/local}

BINDIR=$PREFIX/bin
DOCDIR=$PREFIX/share/doc/ste
MANPREFIX=$PREFIX/share/man

# OpenBSD
#MANPREFIX=$PREFIX/man

PROGS="ste transpose tsvdel tsvedit tsvfind tsvins tsvmove tsvselect"
MANSUFFIX=${MANSUFFIX-$(command -v gzip >/dev/null && printf ".gz")}
MANPAGES=$(for prog in $PROGS; do printf "%s.1%s\n" "$prog" "$MANSUFFIX"; done)

D default.do => default.do +0 -10
@@ 1,10 0,0 @@
if [ -r $1.in ]; then
    redo-ifchange $1.in version date
    read VERSION <version
    read DATE <date
    sed -e "s/%VERSION%/$VERSION/g" \
        -e "s/%DATE%/$DATE/g" <$1.in
else
    echo "$0: don't know how to build '$1'" >&2
    exit 99
fi

D default.gz.do => default.gz.do +0 -2
@@ 1,2 0,0 @@
redo-ifchange $2
gzip -cf $2 >$3

D do => do +0 -446
@@ 1,446 0,0 @@
#!/usr/bin/env sh
#
# A minimal alternative to djb redo that doesn't support incremental builds.
# For the full version, visit http://github.com/apenwarr/redo
#
# The author disclaims copyright to this source file and hereby places it in
# the public domain. (2010 12 14; updated 2019 02 24)
#
USAGE="
usage: do [-d] [-x] [-v] [-c] <targets...>
  -d  print extra debug messages (mostly about dependency checks)
  -v  run .do files with 'set -v'
  -x  run .do files with 'set -x'
  -c  clean up all old targets before starting

  Note: do is an implementation of redo that does *not* check dependencies.
  It will never rebuild a target it has already built, unless you use -c.
"

# CDPATH apparently causes unexpected 'cd' output on some platforms.
unset CDPATH

# By default, no output coloring.
green=""
bold=""
plain=""

if [ -n "$TERM" -a "$TERM" != "dumb" ] && tty <&2 >/dev/null 2>&1; then
	green="$(printf '\033[32m')"
	bold="$(printf '\033[1m')"
	plain="$(printf '\033[m')"
fi

# The 'seq' command is not available on all platforms.
_seq() {
	local x=0 max="$1"
	while [ "$x" -lt "$max" ]; do
		x=$((x + 1))
		echo "$x"
	done
}

# Split $1 into a dir part ($_dirsplit_dir) and base filename ($_dirsplit_base)
_dirsplit() {
	_dirsplit_base=${1##*/}
	_dirsplit_dir=${1%$_dirsplit_base}
}

# Like /usr/bin/dirname, but avoids a fork and uses _dirsplit semantics.
qdirname() (
	_dirsplit "$1"
	dir=${_dirsplit_dir%/}
	echo "${dir:-.}"
)

_dirsplit "$0"
REDO=$(cd "$(pwd -P)" &&
	cd "${_dirsplit_dir:-.}" &&
	echo "$PWD/$_dirsplit_base")
export REDO
_cmd=$_dirsplit_base

DO_TOP=
if [ -z "$DO_BUILT" ]; then
	export _do_opt_debug=
	export _do_opt_exec=
	export _do_opt_verbose=
	export _do_opt_clean=
fi
while getopts 'dxvcj:h?' _opt; do
	case $_opt in
		d) _do_opt_debug=1 ;;
		x) _do_opt_exec=x ;;
		v) _do_opt_verbose=v ;;
		c) _do_opt_clean=1 ;;
		j) ;;  # silently ignore, for compat with real redo
		\?|h|*) printf "%s" "$USAGE" >&2
		   exit 99
		   ;;
	esac
done
shift "$((OPTIND - 1))"
_debug() {
	[ -z "$_do_opt_debug" ] || echo "$@" >&2
}

if [ -z "$DO_BUILT" -a "$_cmd" != "redo-whichdo" ]; then
	DO_TOP=1
	if [ "$#" -eq 0 ] && [ "$_cmd" = "do" -o "$_cmd" = "redo" ]; then
		set all  # only toplevel redo has a default target
	fi
	export DO_STARTDIR="$(pwd -P)"
	# If starting /bin/pwd != $PWD, this will fix it.
	# That can happen when $PWD contains symlinks that the shell is
	# trying helpfully (but unsuccessfully) to hide from the user.
	cd "$DO_STARTDIR" || exit 99
	export DO_BUILT="$PWD/.do_built"
	if [ -z "$_do_opt_clean" -a -e "$DO_BUILT" ]; then
		echo "do: Incremental mode. Use -c for clean rebuild." >&2
	fi
	: >>"$DO_BUILT"
	sort -u "$DO_BUILT" >"$DO_BUILT.new"
	while read f; do
		[ -n "$_do_opt_clean" ] && printf "%s\0%s.did\0" "$f" "$f"
		printf "%s.did.tmp\0" "$f"
	done <"$DO_BUILT.new" |
	xargs -0 rm -f 2>/dev/null
	mv "$DO_BUILT.new" "$DO_BUILT"
	export DO_PATH="$DO_BUILT.dir"
	export PATH="$DO_PATH:$PATH"
	rm -rf "$DO_PATH"
	mkdir "$DO_PATH"
	for d in redo redo-ifchange redo-whichdo; do
		ln -s "$REDO" "$DO_PATH/$d"
	done
	for d in redo-ifcreate redo-stamp redo-always redo-ood \
	    redo-targets redo-sources; do
	        echo "#!/bin/sh" >"$DO_PATH/$d"
		chmod a+rx "$DO_PATH/$d"
	done
fi


# Chop the "file" part off a /path/to/file pathname.
# Note that if the filename already ends in a /, we just remove the slash.
_updir()
{
	local v="${1%/*}"
	[ "$v" != "$1" ] && echo "$v"
	# else "empty" which means we went past the root
}


# Returns true if $1 starts with $2.
_startswith()
{
	[ "${1#"$2"}" != "$1" ]
}


# Returns true if $1 ends with $2.
_endswith()
{
	[ "${1%"$2"}" != "$1" ]
}


# Prints $1 if it's absolute, or $2/$1 if $1 is not absolute.
_abspath()
{
	local here="$2" there="$1"
	if _startswith "$1" "/"; then
		echo "$1"
	else
		echo "$2/$1"
	fi
}


# Prints $1 as a path relative to $PWD (not starting with /).
# If it already doesn't start with a /, doesn't change the string.
_relpath()
{
	local here="$2" there="$1" out= hadslash=
	#echo "RP start '$there' hs='$hadslash'" >&2
	_startswith "$there" "/" || { echo "$there" && return; }
	[ "$there" != "/" ] && _endswith "$there" "/" && hadslash=/
	here=${here%/}/
	while [ -n "$here" ]; do
		#echo "RP out='$out' here='$here' there='$there'" >&2
		[ "${here%/}" = "${there%/}" ] && there= && break;
		[ "${there#$here}" != "$there" ] && break
		out=../$out
		_dirsplit "${here%/}"
		here=$_dirsplit_dir
	done
	there=${there#$here}
	if [ -n "$there" ]; then
		echo "$out${there%/}$hadslash"
	else
		echo "${out%/}$hadslash"
	fi
}


# Prints a "normalized relative" path, with ".." resolved where possible.
# For example, a/b/../c will be reduced to just a/c.
_normpath()
(
	local path="$1" relto="$2" out= isabs=
	#echo "NP start '$path'" >&2
	if _startswith "$path" "/"; then
		isabs=1
	else
		path="${relto%/}/$path"
	fi
	set -f
	IFS=/
	for d in ${path%/}; do
		#echo "NP out='$out' d='$d'" >&2
		if [ "$d" = ".." ]; then
			out=$(_updir "${out%/}")/
		else
			out=$out$d/
		fi
	done
	#echo "NP out='$out' (done)" >&2
	out=${out%/}
	if [ -n "$isabs" ]; then
		echo "${out:-/}"
	else
		_relpath "${out:-/}" "$relto"
	fi
)


# Prints a "real" path, with all symlinks resolved where possible.
_realpath()
{
	local path="$1" relto="$2" isabs= rest=
	if _startswith "$path" "/"; then
		isabs=1
	else
		path="${relto%/}/$path"
	fi
	(
		for d in $(_seq 100); do
			#echo "Trying: $PWD--$path" >&2
			if cd -P "$path" 2>/dev/null; then
				# success
				pwd=$(pwd -P)
				#echo "  chdir ok: $pwd--$rest" >&2
				np=$(_normpath "${pwd%/}/$rest" "$relto")
				if [ -n "$isabs" ]; then
					echo "$np"
				else
					_relpath "$np" "$relto"
				fi
				break
			fi
			_dirsplit "${path%/}"
			path=$_dirsplit_dir
			rest="$_dirsplit_base/$rest"
		done
	)
}


# List the possible names for default*.do files in dir $1 matching the target
# pattern in $2.  We stop searching when we find the first one that exists.
_find_dofiles_pwd()
{
	local dodir="$1" dofile="$2"
	_startswith "$dofile" "default." || dofile=${dofile#*.}
	while :; do
		dofile=default.${dofile#default.*.}
		echo "$dodir$dofile"
		[ -e "$dodir$dofile" ] && return 0
		[ "$dofile" = default.do ] && break
	done
	return 1
}


# List the possible names for default*.do files in $PWD matching the target
# pattern in $1.  We stop searching when we find the first name that works.
# If there are no matches in $PWD, we'll search in .., and so on, to the root.
_find_dofiles()
{
	local target="$1" dodir= dofile= newdir=
	_debug "find_dofile: '$PWD' '$target'"
	dofile="$target.do"
	echo "$dofile"
	[ -e "$dofile" ] && return 0

	# Try default.*.do files, walking up the tree
	_dirsplit "$dofile"
	dodir=$_dirsplit_dir
	dofile=$_dirsplit_base
	[ -n "$dodir" ] && dodir=${dodir%/}/
	[ -e "$dodir$dofile" ] && return 0
	for i in $(_seq 100); do
		[ -n "$dodir" ] && dodir=${dodir%/}/
		#echo "_find_dofiles: '$dodir' '$dofile'" >&2
		_find_dofiles_pwd "$dodir" "$dofile" && return 0
		newdir=$(_realpath "${dodir}.." "$PWD")
		[ "$newdir" = "$dodir" ] && break
		dodir=$newdir
	done
	return 1
}


# Print the last .do file returned by _find_dofiles.
# If that file exists, returns 0, else 1.
_find_dofile()
{
	local files="$(_find_dofiles "$1")"
	rv=$?
	#echo "files='$files'" >&2
	[ "$rv" -ne 0 ] && return $rv
	echo "$files" | {
		while read -r linex; do line=$linex; done
		printf "%s\n" "$line"
	}
}


# Actually run the given $dofile with the arguments in $@.
# Note: you should always run this in a subshell.
_run_dofile()
{
	export DO_DEPTH="$DO_DEPTH  "
	export REDO_TARGET="$PWD/$target"
	local line1
	set -e
	read line1 <"$PWD/$dofile" || true
	cmd=${line1#"#!/"}
	if [ "$cmd" != "$line1" ]; then
		set -$_do_opt_verbose$_do_opt_exec
		exec /$cmd "$PWD/$dofile" "$@"
	else
		set -$_do_opt_verbose$_do_opt_exec
		# If $dofile is empty, "." might not change $? at
		# all, so we clear it first with ":".
		:; . "$PWD/$dofile"
	fi
}


# Find and run the right .do file, starting in dir $1, for target $2,
# providing a temporary output file as $3.  Renames the temp file to $2 when
# done.
_do()
{
	local dir="$1" target="$1$2" tmp="$1$2.redo.tmp" tdir=
	local dopath= dodir= dofile= ext=
	if [ "$_cmd" = "redo" ] ||
	    ( [ ! -e "$target" -o -d "$target" ] &&
	      [ ! -e "$target.did" ] ); then
		printf '%sdo  %s%s%s%s\n' \
			"$green" "$DO_DEPTH" "$bold" "$target" "$plain" >&2
		dopath=$(_find_dofile "$target")
		if [ ! -e "$dopath" ]; then
			echo "do: $target: no .do file ($PWD)" >&2
			return 1
		fi
		_dirsplit "$dopath"
		dodir=$_dirsplit_dir dofile=$_dirsplit_base
		if _startswith "$dofile" "default."; then
			ext=${dofile#default}
			ext=${ext%.do}
		else
			ext=
		fi
		target=$PWD/$target
		tmp=$PWD/$tmp
		cd "$dodir" || return 99
		target=$(_relpath "$target" "$PWD") || return 98
		tmp=$(_relpath "$tmp" "$PWD") || return 97
		base=${target%$ext}
		tdir=$(qdirname "$target")
		[ ! -e "$DO_BUILT" ] || [ ! -w "$tdir/." ] ||
		: >>"$target.did.tmp"
		# $qtmp is a temporary file used to capture stdout.
		# Since it might be accidentally deleted as a .do file
		# does its work, we create it, then open two fds to it,
		# then immediately delete the name.  We use one fd to
		# redirect to stdout, and the other to read from after,
		# because there's no way to fseek(fd, 0) in sh.
		qtmp=$DO_PATH/do.$$.tmp
		(
			rm -f "$qtmp"
			( _run_dofile "$target" "$base" "$tmp" >&3 3>&- 4<&- )
			rv=$?
			if [ $rv != 0 ]; then
				printf "do: %s%s\n" "$DO_DEPTH" \
					"$target: got exit code $rv" >&2
				rm -f "$tmp.tmp" "$tmp.tmp2" "$target.did"
				return $rv
			fi
			echo "$PWD/$target" >>"$DO_BUILT"
			if [ ! -e "$tmp" ]; then
				# if $3 wasn't created, copy from stdout file
				cat <&4 >$tmp
				# if that's zero length too, forget it
				[ -s "$tmp" ] || rm -f "$tmp"
			fi
		) 3>$qtmp 4<$qtmp  # can't use "|| return" here...
		# ...because "|| return" would mess up "set -e" inside the ()
		# on some shells.  Running commands in "||" context, even
		# deep inside, will stop "set -e" from functioning.
		rv=$?
		[ "$rv" = 0 ] || return "$rv"
		mv "$tmp" "$target" 2>/dev/null
		[ -e "$target.did.tmp" ] &&
		mv "$target.did.tmp" "$target.did" ||
		: >>"$target.did"
	else
		_debug "do  $DO_DEPTH$target exists." >&2
	fi
}


# Implementation of the "redo" command.
_redo()
{
	local i startdir="$PWD" dir base
	set +e
	for i in "$@"; do
		i=$(_abspath "$i" "$startdir")
		(
			cd "$DO_STARTDIR" || return 99
			i=$(_realpath "$(_relpath "$i" "$PWD")" "$PWD")
			_dirsplit "$i"
			dir=$_dirsplit_dir base=$_dirsplit_base
			_do "$dir" "$base"
		)
		[ "$?" = 0 ] || return 1
	done
}


# Implementation of the "redo-whichdo" command.
_whichdo()
{
	_find_dofiles "$1"
}


case $_cmd in
	do|redo|redo-ifchange) _redo "$@" ;;
	redo-whichdo) _whichdo "$1" ;;
	do.test) ;;
	*) printf "do: '%s': unexpected redo command" "$_cmd" >&2; exit 99 ;;
esac
[ "$?" = 0 ] || exit 1

if [ -n "$DO_TOP" ]; then
	if [ -n "$_do_opt_clean" ]; then
		echo "do: Removing stamp files..." >&2
		[ ! -e "$DO_BUILT" ] ||
		while read f; do printf "%s.did\0" "$f"; done <"$DO_BUILT" |
		xargs -0 rm -f 2>/dev/null
	fi
fi

D install.do => install.do +0 -12
@@ 1,12 0,0 @@
redo-ifchange all
. ./config.redo
install -d "$DOCDIR" "$BINDIR" "${MANPREFIX}/man1"
for docfile in $DOCS; do
	install -Dm 0644 "${docfile}" "${DOCDIR}/${docfile}"
done
for binfile in $PROGS; do
	install -Dm 0755 "${binfile}" "${BINDIR}/${binfile}"
done
for manfile in $MANPAGES; do
	install -Dm 0644 "${manfile}" "${MANPREFIX}/man1/${manfile}"
done

R date.do => lib/makedate +21 -6
@@ 1,3 1,5 @@
#!/bin/sh -x
# vim: set ft=bash:
FALLBACKDATE=${FALLBACKDATE:-unknown}
LC_ALL=C
export LC_ALL


@@ 7,14 9,27 @@ e_user=$(id -un)
if [ -d .git ] && command -v git >/dev/null 2>&1; then
	if [ "$e_user" = "$user" ]; then
		git log --format=format:%cd \
			--date=format:"%B %d, %Y" -1 @ >$3
			--date=format:"%B %d, %Y" -1 @ >date.new
	else
		su "${user}" -c 'env LC_ALL=C git log --format=format:%cd \
			--date=format:"%B %d, %Y" -1 @' >$3
			--date=format:"%B %d, %Y" -1 @' >date.new
	fi
	echo >>$3
	echo >>date.new
else
	printf "%s\n" "$FALLBACKDATE" >$3
	printf "%s\n" "$FALLBACKDATE" >date.new
fi

if [ -f date ]; then
	if diff date date.new >/dev/null; then
		rm date.new
	else
		mv date.new date
		touch .rebuild
	fi
else
	mv date.new date
	touch .rebuild
fi
if [ "$1" = "-l" ]; then
	printf "DATE=\"%s\"\n" "$(cat date)"
fi
redo-always
redo-stamp <$3

A lib/makeversion => lib/makeversion +33 -0
@@ 0,0 1,33 @@
#!/bin/sh -x
# vim: set ft=bash:
FALLBACKVER=${FALLBACKVER:-unknown}
LC_ALL=C
export LC_ALL
user=$(unalias ls >/dev/null 2>&1 || true; \
	command ls -ld . | awk '{print $3}')
e_user=$(id -un)
if [ -d .git ] && command -v git >/dev/null 2>&1; then
	if [ "$e_user" = "$user" ]; then
		git describe 2>/dev/null | sed 's/^v//' >version.new
	else
		su "${user}" -c 'git describe' 2>/dev/null |
			sed 's/^v//' >version.new
	fi
else
	printf "%s\n" "$FALLBACKVER" >version.new
fi

if [ -f version ]; then
	if diff version version.new >/dev/null; then
		rm version.new
	else
		mv version.new version
		touch .rebuild
	fi
else
	mv version.new version
	touch .rebuild
fi
if [ "$1" = "-l" ]; then
	printf "VERSION=\"%s\"\n" "$(cat version)"
fi

A lib/mkpack => lib/mkpack +12 -0
@@ 0,0 1,12 @@
#!/bin/sh -x
# vim: set ft=bash:
DISTDIR=$(grep ^PROG config.mk |
	sed -E 's,.*=[[:space:]]+([[:alpha:]]+),\1,')-$(cat version)
mkdir "$DISTDIR"
for f in $(git ls-files); do
	d="$DISTDIR"/"$(dirname "$f")"
	mkdir -p "$d"
	cp "$f" "$d"/
done
tar -cf - "$DISTDIR" | xz -9v - > "$DISTDIR".tar.xz
rm -fr "$DISTDIR"

A lib/replacepat => lib/replacepat +5 -0
@@ 0,0 1,5 @@
#!/bin/sh -x
# vim: set ft=bash:
DATE=$(cat date)
VERSION=$(cat version)
sed -e 's|%VERSION%|'"$VERSION"'|g' -e 's|%DATE%|'"$DATE"'|g' "$1" > "$2"

A lib/runtests => lib/runtests +57 -0
@@ 0,0 1,57 @@
#!/usr/bin/perl
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2022-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.


use 5.010;
use File::Find;

my $errors = 0;
my @files;
my $maxlen = 60;
my $testdir = 'tests';

system("sh -c 'command -v expect' >/dev/null") == 0 ||
	die "Test suite requires expect";

find(sub {
	push @files, $File::Find::name if (/\.test$/);
}, $testdir . "/");

for (@files)
{
	system $_;
	$results{$_} = $?>>8;
}

# Clear the screen
print "\033[0m\033[2J\033[2K\033[1;1H";

print <<"!";
TEST RESULTS
============================================================
!

for (@files)
{
	printf "%s:%*s%s\t%s\n", $_, $maxlen-5-length($_), " ",
		$results{$_} == 0 ? "  OK" : "FAIL";
	if ($results{$_})
	{
		$errors = 1;
	}
}

if ($errors == 1)
{
	printf "Some tests were unsuccessful - please report\n";
}
else
{
	printf "All tests passed\n";
}

print <<"!";
============================================================
!

D pack.do => pack.do +0 -13
@@ 1,13 0,0 @@
redo all
redo-ifchange version
read VERSION <version
VERSION=$(echo $VERSION | sed 's/^v//')
DISTDIR=ste-$VERSION
mkdir $DISTDIR
for f in $(git ls-files); do
	d=$DISTDIR/$(dirname $f)
	mkdir -p $d
	cp $f $d
done 
tar cvf $DISTDIR.tar.xz $DISTDIR >/dev/null
rm -fr $DISTDIR

D rebuild.do => rebuild.do +0 -1
@@ 1,1 0,0 @@
redo clean all

M ste.1.in => ste.1.in +1 -1
@@ 162,4 162,4 @@ If argument is omitted, show all current option states.
.Xr vipe 1
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M ste.in => ste.in +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# ste - Simple Table Editor
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

# Global options

M transpose => transpose +3 -1
@@ 1,6 1,8 @@
#!/usr/bin/awk -f
# transpose - Transpose a TSV file
# See the file LICENSE for copyright and license details.
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.
BEGIN { FS="\t" }
{
	if (cols < NF)

M transpose.1.in => transpose.1.in +1 -1
@@ 14,4 14,4 @@ transpose operation from linear algebra.
The result is output to stdout.
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvdel => tsvdel +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvdel - Delete a line from a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvdel.1.in => tsvdel.1.in +1 -1
@@ 24,4 24,4 @@ select the line (row) to be deleted by searching the column with index
.El
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvedit => tsvedit +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvedit - Edit a line from a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvedit.1.in => tsvedit.1.in +1 -1
@@ 25,4 25,4 @@ select the line (row) to be replaced by searching the column with index
.El
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvfind => tsvfind +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvfind - Search for text in a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvfind.1.in => tsvfind.1.in +1 -1
@@ 38,4 38,4 @@ Column number to limit the search to (1-based)
.El
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvins => tsvins +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvins - Insert a line to a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvins.1.in => tsvins.1.in +1 -1
@@ 21,4 21,4 @@ If
is omitted, insert a line at the end of file.
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvmove => tsvmove +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvmove - Move a line in a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvmove.1.in => tsvmove.1.in +1 -1
@@ 25,4 25,4 @@ select the line (row) to be moved by searching the column with index
.El
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

M tsvselect => tsvselect +1 -1
@@ 2,7 2,7 @@
# vim: set ft=bash:
# tsvselect - Select a line from a TSV file
# This program is licensed under the terms of GNU GPL v3 or (at your option)
# any later version. Copyright (C) 2023  Страхиња Радић.
# any later version. Copyright (C) 2023-2024  Страхиња Радић.
# See the file LICENSE for exact copyright and license details.

tab=$(printf "\t")

M tsvselect.1.in => tsvselect.1.in +1 -1
@@ 47,4 47,4 @@ Peter
.
.Sh AUTHORS
.An "Strahinya Radich" Aq contact@strahinja.org ,
2023
2023\-2024

D uninstall.do => uninstall.do +0 -10
@@ 1,10 0,0 @@
. ./config.redo
for docfile in $DOCS; do
	rm -f "${DOCDIR}/${docfile}"
done
for binfile in $PROGS; do
	rm -f "${BINDIR}/${binfile}"
done
for manfile in $MANPAGES; do
	rm -f "${MANPREFIX}/man1/${manfile}"
done

D version.do => version.do +0 -17
@@ 1,17 0,0 @@
FALLBACKVER=${FALLBACKVER:-unknown}
LC_ALL=C
export LC_ALL
user=$(unalias ls >/dev/null 2>&1 || true; \
	command ls -ld . | awk '{print $3}')
e_user=$(id -un)
if [ -d .git ] && command -v git >/dev/null 2>&1; then
	if [ "$e_user" = "$user" ]; then
		git describe 2>/dev/null | sed 's/^v//' >$3
	else
		su "${user}" -c 'git describe' 2>/dev/null | sed 's/^v//' >$3
	fi
else
	printf "%s\n" "$FALLBACKVER" >$3
fi
redo-always
redo-stamp <$3