#!/bin/sh

# create chroot for skype skype

if [ -z "$1" -o -z "$2" -o "$1" = "--help" ]; then
	echo "Usage: $0 directory-for-chroot users
	(730Mb free space required)"
	exit 1
fi

if  [ `id -u` -ne 0 ]; then
	echo "This script should be run as root"
	exit 1
fi

# Check required packages

for pkg in debootstrap schroot pulseaudio; do
  if [ "$(dpkg-query --show --showformat '${db:Status-Abbrev}' $pkg)" != 'ii ' ]; then
	  dpkg-query --show --showformat '${db:Status-Abbrev}' $pkg
		
  	  echo " Package $pkg is not installed"
	   fail=yes
  fi
done

[ -n "$fail" ] && exit 1


# Check if pulse-audio tcp protocol is enabled

if xprop -root PULSE_SERVER |grep -q 'tcp:' ; then
   echo "It seems that pulse audio is configured to listen TCP OK"
else
	echo "Please enable module-native-protocol-tcp and module-x11-publish in your pulseaudio config"
	exit 1
fi

# Check if skype package is available
SKYPE_PKG=`echo skype*.deb`
if [ ! -f skype-debian*.deb ]; then
   echo "Please download skype Debian package into current directory"
   exit 1
fi

# 

set -e
CHROOT=$1
shift
mkdir -p $CHROOT/skype
debootstrap --arch=i386 stable $CHROOT/skype
# Copying video device
cp -a /dev/video* $CHROOT/skype/dev

cp skype*.deb $CHROOT/skype/root

cp -f /etc/timezone /etc/localtime $CHROOT/skype/etc

cat > $CHROOT/skype/root/final.sh << EOF
#!/bin/sh
cd /root
EOF

for i in "$@"; do	
	echo "adduser --uid `id -u $i` --disabled-password --gecos $i $i " >> $CHROOT/skype/root/final.sh
	echo "mkdir /tmp/user/`id -u $i`" >> $CHROOT/skype/root/final.sh
	echo "chown $i /tmp/user/`id -u $i`" >> $CHROOT/skype/root/final.sh
done

cat >> $CHROOT/skype/root/final.sh << EOF2
dpkg -i skype*.deb
apt-get install -f -y --force-yes
apt-get install -y  --force-yes x11-utils
apt-get clean
EOF2


#Writing schroot config file

cat > /etc/schroot/chroot.d/skype <<EOC
[skype]
description= Chroot for skype
directory=$CHROOT/skype
users=$*
groups=$*
root-groups=root
EOC

#Install packages and add users into chroot

mkdir $CHROOT/skype/tmp/user
mkdir $CHROOT/skype/tmp/user/0

schroot -c skype -d root -u root sh /root/final.sh

rm $CHROOT/skype/root/final.sh
rm $CHROOT/skype/root/skype*.deb

#writing run-skype script which would run inside chroot
cat > $CHROOT/skype/usr/local/bin/run-skype <<EOS
#!/bin/bash
USER=\`id -un\`
HOME=\`eval echo ~\$USER\`
DISPLAY=${1:-:0.0}
PULSE_SERVER=localhost
export DISPLAY
export PULSE_SERVER
skype &
EOS

chmod +x $CHROOT/skype/usr/local/bin/run-skype

#write skype which would run outside chroot
cat > /usr/local/bin/chroot-skype <<EOS2
#!/bin/bash
if [ \`id -u\` -ne 0 -a -z "\$SUDO_USER" ]; then
	echo "This command expects to run under sudo"
	exit 1
fi
CHROOT=\`schroot --info skype |awk '\$1 == "Path" {print \$2}'\`
SUDO_HOME=\`eval echo ~\$SUDO_USER\`

cp -p \$SUDO_HOME/.Xauthority \$CHROOT/\$SUDO_HOME/.Xauthority

for MNT in /proc /tmp/.X11-unix /tmp/.ICE-unix; do
   mount |grep -q \${CHROOT}\${MNT} || mount -o bind \$MNT \${CHROOT}\${MNT}
done

schroot -c skype -u \$SUDO_USER run-skype $DISPLAY&
EOS2
chmod +x /usr/local/bin/chroot-skype
echo '#!/bin/sh
sudo chroot-skype' > /usr/local/bin/skype
chmod +x /usr/local/bin/chroot-skype
cp ${CHROOT}/skype/usr/share/applcations/skype.desktop /usr/share/applications
# write sudoers.d file for skype
echo > /etc/sudoers.d/skype
for i in "$@"; do
	echo "$i ALL=(ALL) NOPASSWD: /usr/local/bin/chroot-skype" >> /etc/sudoers.d/skype
done
chmod 0440 /etc/sudoers.d/skype
echo "Now run sudo chroot-skype to start skype inside chroot"
echo "There is script /usr/local/bin/skype which does it for you"
