#!/bin/bash
# 32bits Mode for CentOS
# created by virnmer
#
# chkconfig: - 90 90
# description: 32bits Mode for CentOS
#
# processname: cent32
#

# source function library
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

RETVAL2=0
PATH_CENT32="/cent32"


start() {
    echo -n $"Mounting 32bits chroot dirs: "
    mount -o bind /dev ${PATH_CENT32}/dev >/dev/null &
    mount -o bind /dev/pts ${PATH_CENT32}/dev/pts >/dev/null &
    mount -o bind /dev/shm ${PATH_CENT32}/dev/shm >/dev/null &
    mount -o bind /proc ${PATH_CENT32}/proc >/dev/null
    mount -o bind /proc/bus/usb ${PATH_CENT32}/proc/bus/usb >/dev/null &
    mount -o bind /sys ${PATH_CENT32}/sys >/dev/null &
    mount -o bind /tmp ${PATH_CENT32}/tmp >/dev/null &
    mount -o bind /home ${PATH_CENT32}/home > /dev/null &
    mount -o bind /root ${PATH_CENT32}/root > /dev/null &
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        echo_success
    else
        echo_failure
    fi
    echo 
    echo -n $"Copying 32bits chroot files: "
    cp -pf /etc/resolv.conf ${PATH_CENT32}/etc >/dev/null
    cp -pf /etc/passwd ${PATH_CENT32}/etc >/dev/null
    cp -pf /etc/shadow ${PATH_CENT32}/etc >/dev/null
    cp -pf /etc/group ${PATH_CENT32}/etc >/dev/null
    cp -pf /etc/gshadow ${PATH_CENT32}/etc >/dev/null
    cp -pf /etc/hosts ${PATH_CENT32}/etc > /dev/null
    cp -Ppf /etc/localtime ${PATH_CENT32}/etc >/dev/null
    RETVAL2=$?
    if [ $RETVAL -eq 0 ] ; then
        echo_success
    else
        echo_failure
    fi
    echo
    [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/cent32 || \
        RETVAL=1
    return $RETVAL
}

stop() {
    echo -n $"Unmounting 32bits chroot dirs: "
    umount -f ${PATH_CENT32}/dev/pts >/dev/null
    umount -f ${PATH_CENT32}/dev/shm >/dev/null
    umount -f ${PATH_CENT32}/dev >/dev/null
    umount -f ${PATH_CENT32}/proc/bus/usb >/dev/null
    umount -f ${PATH_CENT32}/proc >/dev/null
    umount -f ${PATH_CENT32}/sys >/dev/null
    umount -f ${PATH_CENT32}/tmp >/dev/null
    umount -f ${PATH_CENT32}/home >/dev/null
    umount -f ${PATH_CENT32}/root >/dev/null
    RETVAL=$?
    if [ "$RETVAL" = 0 ] ; then
        echo_success
    else
        echo_failure
    fi
    echo
    [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/cent32
    return $RETVAL
}

restart() {
        stop
        start
}

reload() {
    [ -f /var/lock/subsys/cent32 ] && restart
    return $RETVAL
}

rhstatus() {
    status cent32
    RETVAL=$?
    if [ $RETVAL -ne 0 ] ; then
        return $RETVAL
    fi
}

if [ "$1" = status ]; then
   rhstatus
   exit $?
fi

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  status)
    rhstatus
    ;;
  condrestart)
    [ -f /var/lock/subsys/cent32 ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|reload|status|condrestart}"
    exit 2
esac

exit $?
