#!/bin/bash
#
# -*- coding: utf-8 -*-
#
# Univention UCS@school
#
# Copyright 2007-2017 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

USERNAMECACHE_P="$(mktemp /tmp/tmp.ucsschool.p.XXXXXXXXX)"
USERNAMECACHE_T="$(mktemp /tmp/tmp.ucsschool.t.XXXXXXXXX)"
FIRSTNAMES="Anton Bertram Christina Daniela Ernst Friderike Gisela Hans Ines Jens Klaus Lisa Marlene Norbert Otto Pauline Richard Silke Tim Ulrike Victor Wilhelmine"
LASTNAMES="Meyer Schmidt Schulz Müller Schneider Fischer Weber Wagner Becker Koch Richter Lange"
CLASSES="1A 1B 2A 2B 3B 3C 4B 4r Froesche Igel"

trap "rm -Rf ${USERNAMECACHE_P} ${USERNAMECACHE_P}.* ${USERNAMECACHE_T} ${USERNAMECACHE_T}.*" EXIT

# options
usage() {

	echo "$(basename $0): [options] <ou>"
	echo "  create a dummy user import file"
	echo
	echo "Arguments:"
	echo "  <ou>  name of the school ou"
	echo
	echo "Options:"
	echo "  -h, --help  show this help message and exit"
	
	exit 0
}

while [ $# -ge 1 ] ; do
	case $1 in
		-h|--help) shift 1; usage;;
		*)
			if [ -z "$OU" ]; then
				OU="$(echo "$1" | tr -cd 'a-zA-Z0-9-')"
			fi 
			shift 1
			;;
	esac
done

if [ -z "$OU" ] ; then
	usage
fi

rotate_classes() {
	CLASSES="$(echo "$CLASSES" | cut -d' ' -f2-) $(echo "$CLASSES" | cut -d' ' -f1)"
}

get_class() {
	CLASS="$OU-$(echo "$CLASSES" | cut -d' ' -f1)"
}

get_username_pupils() {
	local usernamepart="$1"
	local fn="${USERNAMECACHE_P}.${usernamepart}"
	if [ ! -f "$fn" ] ; then
		let i=1
	else
		let i="$(cat $fn)"
		let i=i+1
	fi
	echo "$i" > "$fn"
	ACCOUNTNAME="${usernamepart}$i"
}

get_username_teacher() {
	local fname="$1"
	local lname="$(echo $2 | tr '[:upper:]' '[:lower:]')"
	local usernamepart="${fname:0:1}.${lname:0:8}"

	local fn="${USERNAMECACHE_T}.${usernamepart}"
	if [ ! -f "$fn" ] ; then
		let i=1
	else
		let i="$(cat $fn)"
		let i=i+1
	fi
	echo "$i" > "$fn"

	ACCOUNTNAME="${usernamepart}$i"
}

eval "$(/usr/sbin/ucr shell)"

# create pupils
for fn in $FIRSTNAMES ; do
	for ln in $LASTNAMES ; do
		get_username_pupils "$fn"
		get_class
		echo -e "A\t${ACCOUNTNAME}\t$ln\t$fn\t$OU\t$CLASS\t\t${ACCOUNTNAME}@${domainname}\t0\t1\t0"
		rotate_classes
	done
done

# create teachers
for fn in Daniel Gabriele ; do
	for ln in Krause Lehmann ; do
		get_username_teacher "$fn" "$ln"
		classlist="${OU}-$(echo $CLASSES | sed -e "s| |,$OU-|g")"
		echo -e "A\t${ACCOUNTNAME}\t$ln\t$fn\t$OU\t$classlist\t\t${ACCOUNTNAME}@${domainname}\t1\t1\t0"
	done
done
