#! /bin/sh

# RemObjects Elements Echoes (dotnet) project template instantiator

# Copyright (C)2026, Philip Munts dba Munts Technologies.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

if [ $# -ne 3 ]; then
  echo ""
  echo "Usage: remobjects-elements-dotnet-newproject <new project directory> <language> <library>"
  echo ""
  echo "Languages: csharp go java pascal swift"
  echo "Libraries: libremoteio libsimpleio"
  exit 1
fi

if [ ! -x /bin/uuidgen -a ! -x /usr/bin/uuidgen ]; then
  echo ""
  echo "ERROR: uuidgen not found!"
  echo ""
  exit 1
fi

if [ -e "$1" ]; then
  echo ""
  echo "ERROR: Project directory $1 already exists!"
  echo ""
  exit 1
fi

mkdir -p "$1"

if [ ! -d "$1" ]; then
  echo ""
  echo "ERROR: Cannot create project directory $1"
  echo ""
  exit 1
fi

PROJECTNAME=`basename $1`
PROJECTGUID=`uuidgen`
TEMPLATEDIR=${LIBSIMPLEIO:-/usr/local/share/libsimpleio}/elements/dotnet/templates/elements_dotnet_$2_console_$3.d

install -cm 0644 ${TEMPLATEDIR}/*.elements	"$1"
install -cm 0644 ${TEMPLATEDIR}/Makefile	"$1"
install -cm 0644 ${TEMPLATEDIR}/Program.*	"$1"

sed -i "s/\\\$safeprojectname\\\$/${PROJECTNAME}/g"	"$1"/*
sed -i "s/\\\$guid1\\\$/${PROJECTGUID}/g"		"$1"/*.elements
