#!/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|-n|-s] [[-d] -g |-G] [-t ] 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##*/} # option processing, using silent mode while getopts :dfGg:lnst:v opt do case $opt in d) default=1 ;; f) mainfile=filter.cpp ;; G) gitfav=1 ;; g) gitroot=$OPTARG usegitroot=1 ;; l) library=1 ;; n) mainfile=main.cpp library=0 simple=0 ;; s) simple=1 ;; t) tplroot=$OPTARG ;; v) echo "$progname version 2.0.0" exit 0 ;; \?) msg "invalid option -$OPTARG" usage ;; :) msg "option -$OPTARG needs argument" usage ;; esac done # leave only file arguments shift $(( OPTIND - 1 )) [ $# -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 -a $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