#!/bin/sh
if ! test -d meta; then
echo "${0##*/}: fatal: directory 'meta' not found" >&2
exit 2
fi
if ! test -d .git; then
echo "${0##*/}: fatal: repo (.git) not found -- use 'initrepo' first" >&2
exit 2
fi
name=$(cat meta/name)
. "$(dirname $0)"/libcomain
if test ! -d comain; then
dief 2 "directory 'comain' not found"
fi
desc="$(head -1 meta/desc)"
site="$(head -1 meta/site)"
code="$(head -1 meta/code)"
git="$(head -1 meta/git)"
sp=$((52 - ${#name} - ${#site}))
sp="$(printf "%${sp}s")"
prefix=$(sanitize $name)
year=$(date +%Y)
run cat > h <<EOF
/* This file is part of $name$sp$site
* Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
EOF
git checkout -b dev
git checkout -b dev-wip
git add h
git commit -m '[wip] Add h'
run cp -a comain/meta/deps.tpl/limb meta/deps
run mkdir src/$name
run touch src/$name/+limb
sedesc()
{
printf %s "$*" | sed -e 's/\//\\\//g'
}
sed_site="$(sedesc $site)"
sed_code="$(sedesc $code)"
sed_git="$(sedesc $git)"
sed_desc="$(sedesc $desc)"
sedtpl()
{
if test $# -ne 2; then dief 2 "wrong sedtpl invocation"; fi
run sed -e s/\$name/$name/g -e s/\$prefix/$prefix/g -e s/\$year/$year/g \
-e s/\$site/$sed_site/g -e s/\$code/$sed_code/g -e s/\$git/$sed_git/g \
-e s/\$desc/"$sed_desc"/g \
"comain/tpl/$1" >> "$2"
}
cp h src/$name/$name.c
sedtpl main.c src/$name/$name.c
run rm project.mk
sedtpl project.mk project.mk
run mkdir src/doc
sedtpl main.md src/doc/$name.1.md
sedtpl gitignore .gitignore
git add . .gitignore
git commit --message="First commit"
git checkout dev
git cherry-pick dev-wip
git rebase --onto dev dev-wip~2 dev-wip
make
./configure
echo "done."