#!/bin/sh

while [ $# -gt 0 ] ; do
  case $1 in
    --projectroot) PROJECTROOT=$2; shift 2;;
               * )  shift 1;;
  esac
done


mkdir -p $PROJECTROOT/data/repository/.trash 2>&1 > /dev/null

git config --system core.autocrlf false
git config --system user.name "ERP Nelson - technische Informatik"
git config --system user.email "admindb@local"
 
num=`ls $PROJECTROOT/data/repository | wc -l`

if [ "$num" = "0" ]; then
    echo initalize repository
    lang=`echo $LANG | sed -e "s/_.*$//"`
    if [ "$lang" = "de" ]; then 
      name=Vorlage
      document=Dokumente
      archiv=Archiv
      commitmessage="Initialversion aus der Installation"
    else
      name=template
      document=documents
      archiv=archiv
      commitmessage="initial version of the installation"
    fi
    
    mkdir -p $PROJECTROOT/data/repository/$name
    ( cd $PROJECTROOT/data/repository;
      git init --quiet --shared=group $name;
      mkdir $name/$document; touch $name/$document/.gitignore;
      mkdir $name/$archiv; touch $name/$archiv/.gitignore;
      cd $name;
      git add .;
      git commit -m "$commitmessage";
    )
fi

chown -R root:www-data $PROJECTROOT/data
chmod -R g+w $PROJECTROOT/data
find $PROJECTROOT/data -type d | xargs -d"\n" chmod g+ws

if [ -d $PROJECTROOT/data/cert ]; then
	chown -R root:root $PROJECTROOT/data/cert/key 2>/dev/null 1>&2
	chmod  600 $PROJECTROOT/data/cert/key/* 2>/dev/null 1>&2
fi
