///////////////////////////////////////////////
// Create user directories, for code files and resources
func (c *SignupController) createUserDirectory(user models.User, group string) {

	// Create project directories
	models.CreateDirectories(beego.AppConfig.String("userdata::location")+user.Name, true)

	// Create .spielplatz files
	dir := beego.AppConfig.String("userdata::location") + user.Name + "/" + beego.AppConfig.String("userdata::spielplatzdir") + "/"
	identityFile := dir + "identity"
	file, err := os.Create(identityFile)
	if err != nil {
		beego.Error(err)
	}
	file.Close()
	cnf, err := config.NewConfig("ini", identityFile)
	if err != nil {
		beego.Error("Cannot create identity file in " + dir + " (" + err.Error() + ")")
	}
	cnf.Set("auth::Pwhash", user.Pwhash)
	cnf.Set("rights::NoSpecialRights", "true")
	cnf.Set("groups::member", group)
	cnf.SaveConfigFile(identityFile)

	// Clone Admin Spielplatz project
	err = models.CloneProjectDir(user.Name, beego.AppConfig.String("userdata::commonproject"), true)
	if err != nil {
		beego.Error(err)
	}
}
func initProject(s session.Store, projectName string, fileType string, fileName string, codeFile string, resourceFiles []string, image string) Data {

	// if user is not logged in return
	if s.Get("UserName") == nil {
		beego.Error("No user name available.")
		return Data{}
	}
	if projectName == "" {
		beego.Error("No project name available.")
		return Data{}
	}

	userName := s.Get("UserName").(string)
	userDir := beego.AppConfig.String("userdata::location") + userName
	projectDir := userDir + "/" + beego.AppConfig.String("userdata::projects") + "/" + projectName
	bareDir := beego.AppConfig.String("userdata::location") + beego.AppConfig.String("userdata::bareprojects") + "/" + projectName

	////////////////////////////////////////////////////////////////////7
	// Everything, that has to be done for init a project
	_, err := os.Stat(bareDir)
	if !os.IsNotExist(err) {
		return Data{
			"Error": "project exists",
		}
	}

	// Create new directory
	if err := os.MkdirAll(bareDir, os.ModePerm); err != nil {
		beego.Error("Cannot create directory", bareDir)
	}

	// Initialize as bare git directory
	_, err = git.InitRepository(bareDir, true)
	if err != nil {
		beego.Error("Cannot init git directory", bareDir, "(", err.Error(), ")")
	}

	// Clone it to own project directory
	options := git.CloneOptions{
		Bare: false,
	}
	_, err = git.Clone(bareDir, projectDir, &options)
	if err != nil {
		beego.Error("Cannot clone git directory", bareDir, "into", projectDir, "(", err.Error(), ")")
	}

	// err = models.GitSetName(userName, userName+"@"+beego.AppConfig.String("userdata::emailserver"))

	// Create project directories
	models.CreateDirectories(projectDir, false)

	// Create .gitignore file with .spielplatz/project in it
	models.CreateTextFile(projectDir+"/"+".gitignore", beego.AppConfig.String("userdata::spielplatzdir")+"/rights")

	// Copy resource files
	for i := 0; i < len(resourceFiles); i++ {
		resType := resourceFiles[i][strings.LastIndex(resourceFiles[i], ".")+1:]
		filename := resourceFiles[i][strings.LastIndex(resourceFiles[i], "/")+1:]
		dir := "."

		switch resType {
		case "png":
			dir = beego.AppConfig.String("userdata::imagefiles")
		case "mp3":
			dir = beego.AppConfig.String("userdata::soundfiles")
		}
		err = copyFileContents(userDir+"/"+dir+"/"+resourceFiles[i], projectDir+"/"+dir+"/"+filename)
		if err != nil {
			beego.Error("Cannot copy resource file", resourceFiles[i], "from", userDir, "to", projectDir, "(", err.Error(), ")")
		}
	}

	// Mount resource directories
	models.MountResourceFiles(userName, projectName)

	// Create project config file
	projectFile := projectDir + "/" + beego.AppConfig.String("userdata::spielplatzdir") + "/project"
	file, err := os.Create(projectFile)
	if err != nil {
		beego.Error(err)
	}
	file.Close()
	cnf, err := config.NewConfig("ini", projectFile)
	if err != nil {
		beego.Error("Cannot create project file " + projectFile + " (" + err.Error() + ")")
	}
	cnf.Set("Playground", beego.AppConfig.String("userdata::name"))
	cnf.Set("Origin", "none")
	cnf.Set("Gallery", "false")
	cnf.SaveConfigFile(projectFile)

	// Create rights file
	rightsFile := projectDir + "/" + beego.AppConfig.String("userdata::spielplatzdir") + "/rights"
	file, err = os.Create(rightsFile)
	if err != nil {
		beego.Error(err)
	}
	file.Close()
	cnf, err = config.NewConfig("ini", rightsFile)
	if err != nil {
		beego.Error("Cannot create rights file " + rightsFile + " (" + err.Error() + ")")
	}
	for _, right := range models.PRR_NAMES {
		cnf.Set("rights::"+right, "true")
	}
	cnf.SaveConfigFile(rightsFile)

	// Create database entry
	user, _ := models.GetUser(userName)
	project := new(models.Project)
	project.Name = projectName
	project.Playground = beego.AppConfig.String("userdata::name")
	project.Origin = "none"
	project.Gallery = false
	project.Forks = 0
	project.Stars = 0
	models.CreateProjectDatabaseEntry(project, user, int64(1<<uint(len(models.PRR_NAMES)))-1)

	// Write source files to new project directory
	name := projectName + "." + fileType
	data := writeSourceFile(s, name, projectName, fileType, codeFile, int64(0), image, "", "", false)

	// Add all rights as return values
	data["Rights"] = models.PRR_NAMES

	// Only one user by now
	data["Users"] = []string{userName}

	// Add, commit and push
	err = models.GitAddCommitPush(userName, projectDir, beego.AppConfig.String("userdata::firstcommit"), true)
	if err != nil {
		beego.Error(err)
	}

	// Remove old files, if any
	if fileName != "" {
		deleteSourceFiles(s, []string{fileName}, nil)
	}

	return data
}