mkproj/mkproj.sh

113 lines
2.9 KiB
Bash

#!/usr/bin/env bash
config=$HOME/.config/mkprojrc
source $config
default=0
mainfile=main.cpp
gitfav=0
library=0
simple=0
usegitroot=0
usage() {
echo "usage: $progname [-f|-l|-s] [[-d] -g <gitroot>|-G] [-t <tplroot>] PROJECTNAME" >&2
exit 1
}
msg() {
echo "$progname:" "$@" >&2
}
die() {
msg "$@"
exit 1
}
check_gitroot() {
[ -n "$gitroot" ] || die "please use --gitroot to specify a git url, used as prefix for all your repos"
}
progname=${0##*/}
# argument normalizing; use colon for required arg,
# double colon for optional arg.
TEMP=$(getopt -o dfGg:lst:v --long default,filter,gitfav,gitroot,library,simple,tplroot,version -n "$progname" -- "$@")
[ $? -eq 0 ] || die "invalid arguments"
eval set -- "$TEMP"
# option processing
while true
do
case "$1" in
-d | --default ) default=1 ; shift ;;
-f | --filter ) mainfile=filter.cpp ; shift ;;
-G | --gitfav ) gitfav=1 ; shift ;;
-g | --gitroot ) gitroot="$2" ; usegitroot=1 ; shift 2 ;;
-l | --library ) library=1 ; shift ;;
-s | --simple ) simple=1 ; shift ;;
-t | --tplroot ) tplroot="$2" ; shift 2 ;;
-v | --version ) echo "$progname version 2.0.0" ; exit 0 ;;
-- ) shift ; break ;;
* ) break ;;
esac
done
[ $# -gt 0 ] || usage
[ -n "$tplroot" ] || die "no template root directory defined"
proj=${1//[- ]/_} # replace all spaces and dashes with underscores
if [ $default = 1 ]
then
check_gitroot
if grep -q gitroot $config
then
sed -i -e "s/^gitroot=.*$/gitroot=\"$gitroot\"" $config
else
printf 'gitroot="%s"\n' $gitroot >> $config
fi
fi
if [ $gitfav = 1 ] || [ $usegitroot = 1 ]
then
check_gitroot
git clone "$gitroot/$proj.git" || die "not an existing repo: $proj"
else
mkdir -p "$proj"
fi
if [ $simple = 1 ]
then
cp $tplroot/Makefile.simple $proj/Makefile
cp $tplroot/main.simple.cpp $proj/$proj.cpp
else
mkdir -p $proj/src
cp $tplroot/Makefile.unified $proj/Makefile
cp $tplroot/precomp.hpp $proj/src
cp -r $tplroot/tests $proj/
uproj=$(echo $proj | tr '[:lower:]' '[:upper:]')
sed -e "s/{PROJECT}/$proj/" -e "s/{PROJ}/$uproj/" $tplroot/version.hpp > $proj/src/version.hpp
sed -e "s/{PROJECT}/$proj/" $tplroot/version.cpp > $proj/src/version.cpp
if [ $library = 1 ]
then
mkdir -p $proj/man/man3
sed -e "s/{PROJECT}/$proj/" $tplroot/lib.3 > $proj/man/man3/$proj.3
cp $tplroot/premake.lib.make $proj/premake.make
else
mkdir -p $proj/man/man1
sed -e "s/{PROJECT}/$proj/" $tplroot/tool.1 > $proj/man/man1/$proj.1
cp $tplroot/premake.tool.make $proj/premake.make
sed -e "s/{PROJECT}/$proj/" $tplroot/$mainfile > $proj/src/main.cpp
fi
fi
sed -e "s/{PROJECT}/$proj/" $tplroot/gitignore > $proj/.gitignore
if [ ! -d .git ]
then
cd $proj
git init
git add ./* .gitignore
git commit -m 'First commit'
fi