#! /bin/bash
#
# splinux - Run a parallel Linux installation
# Copyright (c) 2010 Nebil Reyhani
#
# This file is part of splinux.
# Splinux 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
# 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/>.
#
# Send your feedback or comments to: nebil.reyhani@gmail.com
# 



mounttarget[0]="/tmp"
mounttarget[1]="/proc"
mounttarget[2]="/sys"
mounttarget[3]="/dev"
mounttarget[4]="/dev/pts"

disconnect=false

function MntJob(){
  j=0
  while [ $j -lt 5 ]; do
    if [ $disconnect == true ]; then
      i=`expr 4 - $j`
    else
      i=$j
    fi
    mountcheck=`mount | grep "$mountpoint""${mounttarget[$i]}"`
    if [ -z "$mountcheck" ]; then
      if [ $disconnect == false ]; then
        echo "mountcheck: "$mountpoint""${mounttarget[$i]}" is not mounted yet. Mounting..."
        mount -o bind "${mounttarget[$i]}" "$mountpoint""${mounttarget[$i]}"
      else
        echo "mountcheck: "$mountpoint""${mounttarget[$i]}" is not mounted. Skipping..."
      fi
    else
      if [ $disconnect == true ]; then
        echo "mountcheck: Trying to unmount "$mountpoint""${mounttarget[$i]}""
        umount "$mountpoint""${mounttarget[$i]}"
      else
        echo ""$mountpoint""${mounttarget[$i]}" is already mounted"
      fi
    fi
  let j=j+1
  done
}


until [ -z "$1" ]; do
  if [ "$1" == "-m" ]; then
    shift
    mountpoint="$1"
  elif [ "$1" == "-z" ]; then
    shift
    disconnect=true
  elif [ "$1" == "-l" ]; then
    shift
    user="$1"
  elif [ "$1" == "-c" ]; then
    shift
    command="$1"
  elif [ "$1" == "-d" ]; then
    shift
    display="$1"
  fi
    shift
done

if [ -z "$mountpoint" ]; then
  echo ""
  echo "You must specify the mount point of the target linux installation"
  echo "Usage: splinux -m [moutpoint] -l [loginname] -c [command] -d [display]"
  echo "       splinux -z (make clean)"
  echo ""
  exit
fi

if [ $disconnect == true ]; then
  MntJob
  exit
fi

if [ -z "$user" ]; then
  user=$LOGNAME
fi

if [ -z "$command" ]; then
  command="xterm"
fi

if [ -z "$display" ]; then
  display=":0"
fi

mountcheck=`mount | grep "$mountpoint"`
if [ -z "$mountcheck" ]; then
  echo "$mountpoint not mounted"
  exit
else
# The chrooted system will have access to the same /tmp as the host system
# So we're copying the scripts to be run by the chroot in this directory
  r2u="$0"_r2u
  u="$0"_u
  if [ ! -f $r2u ] || [ ! -f $u ]; then
    echo "Missing necessary files \"splinux_r2u\" and \"splinux_u\" to launch script"
    exit
  else
    cp $r2u  /tmp/
    cp  $u /tmp
    echo "Copying necessary files to /tmp temporarily"
    echo "$r2u --> /tmp"
    echo "$u -->  /tmp"
  fi
fi

MntJob

xhost +

chroot /"$mountpoint"/ /tmp/splinux_r2u $user $command $display

# Now we can remove the files tepmorarily copied to /tmp
rm /tmp/splinux*

exit
