#!/bin/bash
########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn is free software: you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# MuGLIn is distributed 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 General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

if [[ "$1" = "-h" ]]; then
	echo "Renerates MuGLIn client image"
fi

echo -n "Checking sessions..."
# wenn session in /var/run/muglin/sessions und in db und status gleich 0 dann behalten, sonst löschen
# wenn session in db aber kein nicht in /var/run/muglin/sessions dann sessionstatus auf fehler
echo "done"

echo -n "Updating database informations..."
echo "done"

INITRD_DIR=/usr/local/lib/muglin/client/
PASSWORD="$(pwgen -s 16 1)"
M_CLIENTUSR="muglinclient"

echo -n "Updating client password..."
if [ ! -d $INITRD_DIR/etc/muglin ]; then
	mkdir -p $INITRD_DIR/etc/muglin
fi
echo $PASSWORD > $INITRD_DIR/etc/muglin/auth
echo -e "$PASSWORD\n$PASSWORD" | passwd $M_CLIENTUSR >/dev/null 2>&1
if [ "$?" != "0" ]; then
	echo "failed"
else
	echo "done"
fi

echo -n "Generating initrd.gz..."
cp /usr/local/lib/muglin/show_iif $INITRD_DIR/muglin/
# don't know why, but it have taken away all exec perm on our stuff oO
chmod +x $INITRD_DIR/muglin/*
cd $INITRD_DIR
find . | cpio --quiet -H newc -o | gzip > /tftpboot/muglin/initrd.gz 
cd $OLDPWD
echo done

