#!/bin/bash

# Script to be used as pbscript to create a potentialy 
# missing pb account on the QEMU VM, and adds it to sudo
# 
# Needs to use root account to connect to the VM

# You need to specify here your local public SSH key used to access
# the pb account in the VM later on

cat > /tmp/pbkey << EOF
ssh-dss AAAAB3NzaC1kc3MAAACBAMC06bNLMu8ZBJv3Hi77+fi+GUhtQScIbGKru86C/IP27EjrmYhPVccbu8kpX6SDHIouhpZ00gaefP3zHjLUmPKlXkMNUNjxx5JNLPoHhSIuUvoSTNoh9gsi31abAvzbW+RNer7rvBJO4ytNJRdsuNxBwqjxK04JEmatds+aKTQjAAAAFQDHaNmIuvYZ0RtNBVe+tzTzxxCQmQAAAIA4o89Xob2VBRtQKBjmWVAuTS14qiqnQUPNloaRkM+r2odI+NJDFc+WXOIG9QAPKKt1jvFOBOX9Mu3/xwiDLnAGhxud98FoyHevSpIWUld7rG4j1aww1WxolFWY/iPyAvDDMW3iysCoqQeSOlpRd9Mz/dq6/P/VcizhdGOKyc3JBgAAAIAA1eY+BoneiQ/tQ8j3GYbMTu6U7VzZ2OaoVeQn4NtZmPl+ZeQIj83nFpo99mYCbaBPqnZToERndl+ljp/JX5pamwj6RcLIDCyiB6MXLbQSWj0goegU1htp5aMd3NcyR2Jy8gCnf0QjDr7j23Q+CFGExtRb/nsDmaG5W9D6QSWQ/g== bruno@victoria
EOF
chmod 644 /tmp/pbkey

grep -Eq '^pb:' /etc/passwd
if [ $? -ne 0 ]; then
	if [ ! -d /home ]; then
		mkdir /home
	fi
	useradd pb
fi

# No passwd for pb only keys
perl -pi -e 's/^pb:\!\!:/pb:*:/' /etc/shadow
# Adapt sudoers
grep -Eq '^pb   ' /etc/sudoers
if [ $? -ne 0 ]; then
	echo "pb   ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
fi
# poweroff only

su - pb -c "mkdir -p .ssh ; chmod 700 .ssh ; cp /tmp/pbkey .ssh/authorized_keys ; chmod 600 .ssh/authorized_keys"
rm -f /tmp/pbkey

# Get and install pb
if [ -x /usr/bin/yum ]; then
	yum clean all
	# FC6
	#yum -y install neon newt-devel slang-devel autoconf automake libtool gcc rpm-build wget vim gcc-c++ docbook-utils-pdf ImageMagick man patch cdrecord mkisofs perl-AppConfig perl-DateManip
	# FC4
	yum -y install neon newt-devel slang-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch cdrecord mkisofs perl-DateManip
	rm -rf AppConfig-*
	wget http://search.cpan.org/CPAN/authors/id/A/AB/ABW/AppConfig-1.66.tar.gz
	tar xvfz AppConfig-1.66.tar.gz
	cd AppConfig*
	perl Makefile.PL
	make
	make install
	cd ..
elif [ -x /usr/sbin/urpmi ]; then
	urpmi --auto-select neon newt-devel slang-devel glibc-static-devel autoconf automake libtool gcc rpm-build wget gcc-c++ docbook-utils-pdf ImageMagick man patch mindi mondo mkisofs cdrecord perl-AppConfig perl-DateManip
elif [ -x /usr/bin/apt-get ]; then
	apt-get install autoconf automake libtool g++ wget patch mondo groff imagemagick docbook-utils docbook2x docbook-to-man openssh-server dpkg-dev debian-builder dh-make fakeroot libnewt-dev libncurses5-dev
elif [ -x /usr/sbin/emerge ]; then
	emerge neon newt slang autoconf automake subversion libtool gcc wget vim man groff lynx grub afio buffer mindi mondo-rescue cdrecord mkisofs	
else
	echo "No pkg to install"
fi

cat > /tmp/pbpatch << 'EOF'
--- File.pm     2007-09-29 14:55:28.000000000 +0100
+++ File.pm.new 2007-09-29 14:55:44.000000000 +0100
@@ -15,7 +15,7 @@
 package AppConfig::File;
 use strict;
 use warnings;
-use AppConfig;
+use AppConfig qw(:argcount);
 use AppConfig::State;
 our $VERSION = '1.65';

@@ -208,6 +208,11 @@

                 my $nargs = $state->_argcount($variable);

+                # Fix a bug if the variable is a hash - 1 has been created as a key
+                if ($nargs eq AppConfig::ARGCOUNT_HASH) {
+                    delete $state->{ VARIABLE }->{ $variable }{1};
+                }
+
                 # variables prefixed '-' are reset to their default values
                 if ($flag eq '-') {
                     $state->_default($variable);

EOF

# Patch AppConfig 
file=`find /usr/lib/perl5 -type f -name File.pm | grep AppConfig/File.pm`
grep -q 'Fix a bug if the variable is a hash' $file
if [ $? -ne 0 ]; then
	(cd `dirname $file` ; patch -s -p0 < /tmp/pbpatch)
	if [ $? -eq 0 ]; then
		rm -f /tmp/pbpatch
	fi
fi

rm -rf project-builder-*
wget ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz
tar xvfz project-builder-latest.tar.gz
cd project-builder-*
perl Makefile.PL
make
make install
cd ..
