#!/usr/bin/env sh
# This file is part of .cargo. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/.cargo/master/COPYRIGHT. No part of rust1, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2016 The developers of .cargo. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/.cargo/master/COPYRIGHT.


set -e
set -u
set -f

_program()
{
	install_sed()
	{
		local year="$1"
		local repositoryName="$2"
		local fromFilePath="$3"
		local toFilePath="$4"
		sed -e 's/2016/'"$year"'/g' -e 's/\.cargo/'"$repositoryName"'/g' "$fromFilePath" >"$toFilePath"
	}

    install_targetSpecifications()
    {
		set +f
    	set -- "$ourFolderName"/target-specifications/*.json
    	set -f

		local linkedFile
		for linkedFile in "$@"
		do
			ln -s "$linkedFile"
		done
	}

    install_install()
    {
		cd "$_program_path" 1>/dev/null
			local fullPath="$(pwd)"
			local ourFolderName="${fullPath##*/}"
		cd .. 1>/dev/null

		local repositoryFullPath="$(pwd)"
		local repositoryName="${repositoryFullPath##*/}"

		local linkedFile
		for linkedFile in .gitignore clippy.toml rustfmt.toml .tm_properties
		do
			ln -s "$ourFolderName"/"$linkedFile"
		done

		install_targetSpecifications

		local folder
		for folder in src
		do
			mkdir -m 0755 -p "$folder"
		done

		# Probably only works on Mac OS X and the BSDs
		local year="$(date -u -j +'%Y')"
		local name="XXXXX"
		install_sed "$year" "$repositoryName" "$ourFolderName"/templates/README.md README.md
		install_sed "$year" "$repositoryName" "$ourFolderName"/COPYRIGHT COPYRIGHT
		install_sed "$year" "$repositoryName" "$ourFolderName"/LICENSE LICENSE
		install_sed "$year" "$repositoryName" "$ourFolderName"/templates/Cargo.toml Cargo.toml
		install_sed "$year" "$repositoryName" "$ourFolderName"/templates/lib.rs src/lib.rs
    }
}

_program_path_find()
{
    if [ "${_program_fattening_program_path+set}" = 'set' ]; then
        printf '%s\n' "$_program_fattening_program_path"

    elif [ "${0%/*}" = "$0" ]; then

        # We've been invoked by the interpreter as, say, bash program
        if [ -r "$0" ]; then
            pwd -P
        # Clutching at straws; probably run via a download, anonymous script, etc, weird execve, etc
        else
            printf '\n'
        fi

    else

        # We've been invoked with a relative or absolute path (also when invoked via PATH in a shell)

        _program_path_find_parentPath()
        {
            parentPath="${scriptPath%/*}"
            if [ -z "$parentPath" ]; then
                parentPath='/'
            fi
            cd "$parentPath" 1>/dev/null
        }

        # pdksh / mksh have problems with unsetting a variable that was never set...
        if [ "${CDPATH+set}" = 'set' ]; then
            unset CDPATH
        fi

        if command -v realpath 1>/dev/null 2>/dev/null; then
            (
                scriptPath="$(realpath "$0")"

                _program_path_find_parentPath
                pwd -P
            )
        elif command -v readlink 1>/dev/null 2>/dev/null; then
            (
                scriptPath="$0"

                while [ -L "$scriptPath" ]
                do
                    _program_path_find_parentPath
                    scriptPath="$(readlink "$scriptPath")"
                done

                _program_path_find_parentPath
                pwd -P
            )
        else
            # This approach will fail in corner cases where the script itself is a symlink in a path not parallel with the concrete script
            (
                scriptPath="$0"

                _program_path_find_parentPath
                pwd -P
            )
        fi

    fi
}

_program_name='install'
_program_version='unversioned'
_program_package_or_build=''
_program_path="$(_program_path_find)"
_program_libPath="${_program_path}/lib"
_program_etcPath="${_program_path}/etc"
_program_varPath="${_program_path}/var"
_program_entrypoint='install_install'

_program
${_program_entrypoint} "$@"
