#!/bin/bash
# Your script doesn't succeed creating more that 9 projects, you might want to check it

PROJECT=$1
# I put the $HOME var to test it anywhere, you may want to keep it
WORKINGDIR="$HOME/Business/Projects"

# Check if the working dir exists and create it if not
if [ ! -d $WORKINGDIR ]
then mkdir -p $WORKINGDIR;
fi

cd $WORKINGDIR

# if the project exists, we'll just send a warning
EXISTS=`ls | grep [0-9][0-9][0-9]-$PROJECT$`

if [ "${#EXISTS}" -ne "0" ]
then echo "$PROJECT found in $EXISTS"
else

# we run the script
# Test for existence of zeroth project
if [ ! -e 000-* ] ; then
  Num=0
else
  MaxIndex=0
  for dir in [0-9][0-9][0-9]-* ;
  do
     Num=${dir%%-*}
     Num=${Num##0}
     Prj=${Num##0}
     if [ $MaxIndex -lt $Num ]; then
        MaxIndex=$Num
     fi
  done
  Num=$((Num+1));
fi

Num=$(printf %03d $Num)
PROJECT=$Num-$PROJECT

mkdir $PROJECT
mkdir $PROJECT/1-Process
mkdir $PROJECT/2-Proposal
mkdir $PROJECT/3-Reg-n-Host
mkdir $PROJECT/4-Resources

mkdir -p $PROJECT/1-Process/01-Discovery/1-Gathering-Information

fi

exit
