#!/bin/bash
# convert-kl-profile-to-rpm - Part of KIWI-LTSP(server:ltsp on OBS) as created by Alex Savin
#
# Copyright (c) 2012 Alex Savin
# This program 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 2 of the License, or (at your option) any later
# version.
#
# This program 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
# this program; if not, see <http://www.gnu.org/licenses/>.
#
# This is helper script to export kiwi-ltsp profile into rpm

#======================================
# Show usage
#--------------------------------------
if [[ -n ` echo " --usage -h --help " | grep " $1 "` || -z $1 ]] ; then
cat <<EOF 
	This is a helper script to convert KIWI-LTSP profile and image into 
	rpm package, it create source archives and update spec file.
	
	cd /to/the/dir with spec file
	sudo ./convert-kl-profile-to-rpm kiwi_ltsp_profile_name new_name 
EOF
	exit 0
fi
#======================================
# Setup spec 
#--------------------------------------
oldPrfName=$1
prfName=$2
. /etc/sysconfig/kiwi-ltsp
sed -i -e "s:^%define prfName .*:%define prfName "$prfName":" kiwi-ltsp-$prfName.spec
#======================================
# Create root image archive
#--------------------------------------
echo "Updating $prfName-img.tar"
rm -rf srv
mkdir -p srv/kiwi-ltsp/ 
cp $LTSP_IMG_DIR/$oldPrfName.img srv/kiwi-ltsp/$prfName.img
cp $LTSP_IMG_DIR/$oldPrfName.md5 srv/kiwi-ltsp/$prfName.md5 2>/dev/null
rm -f   $prfName-img.tar 2>/dev/null
tar -cf $prfName-img.tar srv/
rm -rf  srv/
#======================================
# Create netboot archive
#--------------------------------------
echo "Updating $prfName-netboot.tar"
mkdir -p srv/tftpboot/boot/
cp $TFTPBOOTPATH/boot/linux-$oldPrfName  srv/tftpboot/boot/linux-$prfName
cp $TFTPBOOTPATH/boot/initrd-$oldPrfName srv/tftpboot/boot/initrd-$prfName
tar -cf $prfName-netboot.tar srv/
rm -rf srv/
#======================================
# Create unneeded archive if needed
#--------------------------------------
if grep -Eq "^Source3:[ \t]*%{prfName}-unneeded.tar.xz" kiwi-ltsp-$prfName.spec ; then
	echo "Updating $prfName-unneeded.tar.xz"
	rm -f $prfName-unneeded.tar.xz 2>/dev/null
	tar --xz -cf $prfName-unneeded.tar.xz $LTSNFSPATH-unneeded/
fi
#======================================
# Export profile config and update spec
#--------------------------------------
[[ ! -d $KIWI_PROFILES_DIR/$oldPrfName/extra-packages/ ]] && \
	echo "Warning: directory $KIWI_PROFILES_DIR/$oldPrfName/extra-packages - is not exist"
	
kiwi-ltsp --export $oldPrfName
# prfArcive=`ls -1t $KIWI_PROFILES_DIR/$oldPrfName* | grep $oldPrfName --max-count=1`
for i in `ls  -1t $KIWI_PROFILES_DIR`
	do
	
	if [[ -f $KIWI_PROFILES_DIR/$i && `echo $i | grep ^$oldPrfName ` ]] ; then
		prfArcive=$i
		break
	fi
done
prfArciveNew=${prfArcive/$oldPrfName/$prfName}
echo "Updating $prfArciveNew"
rm -f $prfArciveNew
cp $KIWI_PROFILES_DIR/$prfArcive $prfArciveNew
sed -i -e "s@^Source2:.*@Source2:       "$prfArciveNew"@" *.spec
#======================================
# Set non-root owner for files
#--------------------------------------
if [[ $USER == "root" ]] ; then
	if [[ -n $SUDO_USER ]] ; then
		chown $SUDO_USER:users *
	else
		dirOwner=`ls -ld . | cut -f3 -d' '`
		dirGroup=`ls -ld . | cut -f4 -d' '`
		chown $dirOwner:$dirGroup *
	fi
fi

exit 0