#!/bin/sh

usage() {
    echo "Usage: project-init <type>"
    echo "type git: VCS project"
    echo "type remote-git: VCS project with README, LICENSE and tests"
    echo "type src: Simple coding project"
    echo "type init: Initialize populated project"
    echo "type doc: Assignment"
    echo "type nix-ld: Run unpatched programs with Nix"
    echo "type datasci: Python data science project"
    exit 1
}

copy_nix_files() {
    if [ "$1" = "flake" ]; then
        cp ~/Projects/devenv/flake-template.nix flake.nix
    elif [ "$1" = "nix-ld" ]; then
        cp ~/Projects/devenv/nix-ld.nix shell.nix
    elif [ "$1" = "datasci" ]; then
        cp ~/Projects/devenv/python-data-science.nix flake.nix
    else
        cp ~/Projects/devenv/shell.nix .
    fi
}

git_initialization() {
    git init
    git add ./*
    git commit -m "Initial commit"
}

direnv_integration() {
    if [ "$1" = "flake" ]; then
        echo "use flake" >.envrc
    else
        echo "use nix" >.envrc
    fi
    direnv allow
}

create_dir() {
    if [ ! -d "$1" ]; then
        mkdir "$1"
    fi
}

if [ $# != 1 ]; then
    usage
fi

type=$1

if [ "$type" = "git" ]; then
    copy_nix_files "nix"
    create_dir data
    create_dir src
    git_initialization
    direnv_integration "nix"
elif [ "$type" = "remote-git" ]; then
    touch README.org
    cp ~/Projects/devenv/LICENSE.md LICENSE.md
    copy_nix_files "flake"
    create_dir data
    create_dir src
    git_initialization
    direnv_integration "flake"
elif [ "$type" = "doc" ]; then
    create_dir assets
    touch Report.org
    touch .project
elif [ "$type" = "src" ]; then
    copy_nix_files "nix"
    create_dir data
    create_dir src
    touch .project
    direnv_integration "nix"
elif [ "$type" = "init" ]; then
    touch .project
    if [ ! -f shell.nix ]; then
        copy_nix_files "nix"
    fi
    direnv_integration "nix"
elif [ "$type" = "nix-ld" ]; then
    copy_nix_files "nix-ld"
    direnv_integration "nix"
elif [ "$type" = "datasci" ]; then
    copy_nix_files "datasci"
    create_dir data
    touch .project
    direnv_integration "flake"
else
    usage
fi
