#!/bin/bash

# first we declare all our data

# the shared home for all our files
sharedhome="/home/postproduction/"

# all the groups and users we need
groups="production planning script technical videoeditors audioengineers sftpjailed"
users="marie hugo victor camille dave sarah ester adam eefje alex"

# each user also has to belong to specific groups
marie="production planning script technical videoeditors audioengineers sftpjailed"
hugo="production planning script sftpjailed"
victor="production planning sftpjailed"
camille="production script sftpjailed"
dave="technical videoeditors sftpjailed"
sarah="technical videoeditors sftpjailed"
ester="technical videoeditors sftpjailed"
adam="technical audioengineers sftpjailed"
eefje="technical audioengineers sftpjailed"
alex="production planning script technical videoeditors audioengineers"

# now we create the functions we need

function addusers () {
	# adding the home
	mkdir -p $sharedhome
	
	# adding the groups
	for group in $groups
	do
		echo "adding group $group"
		groupadd $group
	done
	
	# adding the users
	for user in $users
	do
		echo "adding user $user"
		useradd $user
		echo "setting the password for $user"
		echo "$user:test" | chpasswd
	done
	
	# adding the users to their groups
	for user in $users
	do
		echo "adding groups for $user"
		usergroups=$(eval echo \$$user)
		for group in $usergroups 
		do
			echo "adding $user to $group"
			usermod -a -G $group $user
		done
	done
}

function delusers () {
	# removing the groups
	for group in $groups
	do
		echo "removing group $group"
		groupdel $group
	done
	
	# removing the users and their primary groups
	for user in $users
	do
		echo "removing user $user"
		userdel $user
		echo "removing group $user"
		groupdel $user
	done
	
	# removing the home
	rm -r $sharedhome
}

function createtree () {
	movie="awesome_movie_project"
	
	# we'll make a folder to house all the movie data
	mkdir -p $sharedhome$movie
	
	# we cd into that directory to make our life easier
	cd $sharedhome$movie
	echo "we'll create all files here: $(pwd)"
	
	# planning
	touch planning.ics
	chown marie:planning planning.ics
	chmod 664 planning.ics
	
	# scenario
	touch scenario.md
	chown marie:script scenario.md
	chmod 664 scenario.md
	
	# audiofiles
	mkdir audiofiles
	chown marie:audioengineers audiofiles
	chmod 2770 audiofiles
	mkdir -p audiofiles/day{01..14}
	
	touch audiofiles/day{01..14}/recording_{00..99}.wav
	chown marie:audioengineers -R audiofiles
	chmod 2770 audiofiles
	chmod 2770 audiofiles/day{01..14}
	chmod 0660 audiofiles/day{01..14}/*.wav
	
	# videofiles
	mkdir videofiles
	chown marie:videoeditors videofiles
	chmod 2770 videofiles
	mkdir -p videofiles/day{01..14}
	
	touch videofiles/day{01..14}/clip_{00..99}.mp4
	chown marie:videoeditors -R videofiles
	chmod 2770 videofiles
	chmod 2770 videofiles/day{01..14}
	chmod 0660 videofiles/day{01..14}/*.mp4
	
	# renders
	mkdir -p renders
	touch renders/final_render.{wav,mp4}
	chmod 664 renders/final_render.{wav,mp4}
	chown marie:technical renders
	chown marie:audioengineers renders/final_render.wav
	chown marie:videoeditors renders/final_render.mp4
}

function setupsftp () {
	local configfile="/etc/ssh/sshd_config"
	sed -i '/@movie/,+4d' $configfile
	cat << EOF >> $configfile
# @movie
Match Group sftpjailed
	ForceCommand internal-sftp -u 0002
	ChrootDirectory $sharedhome
EOF
	systemctl restart sshd.service
}

function showhelp () {
	# shows a help message on the command line
	echo "use add to add the users"
	echo "use del to delete the users"
	echo "use tree to create the tree layout"
}

# from here on out the actual program runs

if [ "$UID" -ne 0 ]
then
	echo "you need to be root to execute this script"
	exit 1
fi

case $1 in
	add)
		addusers
		;;
	del)
		delusers
		;;
	tree)
		createtree
		;;
	all)
		echo "first we delete all previous users, groups, files and folders"
		delusers
		echo "now we add all users and groups"
		addusers
		echo "next we create the directory structure"
		createtree
		echo "and we set the sftp access limitations and umask"
		setupsftp
		echo "done"
		;;
	sftp)
		setupsftp
		;;
	*)
		showhelp
		;;
esac
