#!/bin/bash set -euo pipefail usage() { echo "Usage: $0 [-b|--build] [-m|--make] " echo echo "Options:" echo " -m, --make After cmake, run 'make' inside the build directory (requires make)" echo " -h, --help Show this help" echo echo "Example: $0 -b my_project" exit 1 } DO_MAKE=0 while [[ ${1:-} == -* ]]; do case "$1" in -m|--make) DO_MAKE=1; shift ;; -h|--help) usage ;; --) shift; break ;; *) echo "Unknown option: $1" >&2; usage ;; esac done if [ $# -ne 1 ]; then usage fi raw_name="$1" name="${raw_name#@}" src="as_template" dest="${name}" if [ -e "$dest" ]; then echo "Error: destination '$dest' already exists" >&2 exit 1 fi echo "$0: COPY: $src -> $dest..." cp -a -- "$src" "$dest" echo "$0: MOVE: $src/as_template.cpp -> $dest/${name}.cpp..." mv "$dest/as_template.cpp" "$dest/${name}.cpp" echo "$0: REPLACE: 'as_template' -> '$name' in all files in $dest..." sed -i "s/as_template/${name}/g" "$dest"/* echo "Preparing build directory and running cmake..." cd "$dest" && mkdir -p build && cd build && cmake .. echo "CMake finished (see $dest/build)." if [ "$DO_MAKE" -eq 1 ]; then echo "Make option enabled. Building with make..." if command -v nproc >/dev/null 2>&1; then NPROCS=$(nproc) elif getconf _NPROCESSORS_ONLN >/dev/null 2>&1; then NPROCS=$(getconf _NPROCESSORS_ONLN) else NPROCS=1 fi make -j"$NPROCS" echo "Make finished." fi echo "Created new assignment '$dest'." exit 0