#!/bin/sh
#
# init-script for translucency
#
# this script works though a config file and tries to set up translucency
# for each line in this file. comments and whitespaces are ignored.
# The first thing expected in the config is the source separated by some
# blanks/tabs from the target. the line
#
# /   /tmp # some comment here
#
# will enable translucency from / to /tmp.
#
# The config-files should be placed in the directory /etc/translucency. The file
# holding the forwards as described above should be named mappings. Two additional
# files, run-up and run-down, may exist holding commands to be executed right
# before translucency is set up (repectively right after translucency has been
# turned off). this may be used to mount a ramdisk used by translucency.
#
# NOTE: this script writes all map-rules from the config-file to /proc,
# even though the maximal number of forwardings is limited in base.h. By default
# only the first eight rules are applied.
#
# fabian thorns (fabian@thorns.it), 30. 8. 2003
#
# this script is free software in the sense of the terms of the GNU GPL version 2,
# or (at your option) any later version.


# Full path to the config-files
CONF_DIR=/etc/translucency


# This makes the appeareance a bit smater but should be commented
# out for debugging

printk=`cat /proc/sys/kernel/printk`
echo "0 0 0 0" > /proc/sys/kernel/printk


# Here we go ...

case "$1" in

start)
   echo -n "Setting up Translucency: "

   # Let's see if we are ready

   if [ ! -d /proc/sys/translucency ]
   then
      if ! insmod translucency
      then
         echo "failed."
         exit 1
      fi
   fi

   # So far everything looks good, let's run pre-config scripts.

   if [ -f $CONF_DIR/run-up ]
   then
      . $CONF_DIR/run-up
   fi

   # OK, Let's work though the config.

   if [ -f $CONF_DIR/mappings ]
   then
      i=0

      while read from to odd
      do

         case "$from" in
         ""|\#)
            continue
         ;;
         esac

         echo -n "$from -> $to, "

         echo -n "$from -> $to" > /proc/sys/translucency/$i

         i=$[$i+1]
      done < $CONF_DIR/mappings

      echo "done."
   else
      echo "$CONF/mappings: File not found."
      exit 1
   fi
;;


stop)
   echo -n "Disabling Translucency: "

   for i in /proc/sys/translucency/[0-9]*
   do
      echo > $i
   done

   if ! rmmod translucency
   then
      echo "failed."
      exit 1
   fi

   if [ -f $CONF_DIR/run-down ]
   then
      . $CONF_DIR/run-down
   fi

   echo "done."
;;

status)
   if /sbin/lsmod|grep -q translucency ; then
     echo running
   else
     echo unused
   fi
;;

restart)
   $0 stop
   $0 start
;;

*)
   echo "Usage $0 {start|stop|restart|status}"
;;

esac


# Restore old status of printk
echo $printk > /proc/sys/kernel/printk


# That's it for today :-)

exit 0
