#!/bin/sh

T2S_DIRS="logos symbols fonts images"

if [ "$1" = "init" ] ; then
   rm -rf /tmpfs/vdr/* > /dev/null 2>&1
   mkdir /tmpfs/vtx > /dev/null 2>&1
   mkdir /tmpfs/log > /dev/null 2>&1
   cd /var/logold
   for i in * ; do
      if [ -d ${i} ] ; then
         mkdir /tmpfs/log/${i} > /dev/null 2>&1
      fi
   done
   rm -rf /var/log > /dev/null 2>&1
   ln -s /tmpfs/log /var/
   ln -s everything/current /tmpfs/log/messages

   mkdir /tmpfs/vdr > /dev/null 2>&1
   mkdir /tmpfs/vdr/plugins > /dev/null 2>&1
   cp /video/epg.data /tmpfs/  > /dev/null 2>&1
   cp /etc/vdr/* /tmpfs/vdr/ > /dev/null 2>&1
   cp /etc/vdr/plugins/* /tmpfs/vdr/plugins/ > /dev/null 2>&1
   cp -rf /etc/vdr/logos /tmpfs/vdr/ > /dev/null 2>&1
   ACT_SKIN=`grep "^OSDSkin" /etc/vdr/setup.conf | cut -f 2 -d "=" | sed -e "s/ //g"`
   if [ ! -z $ACT_SKIN ] && [ -d /etc/vdr/plugins/text2skin/$ACT_SKIN ] ; then
      mkdir /tmpfs/vdr/text2skin > /dev/null 2>&1
      cp -rf /etc/vdr/plugins/text2skin/$ACT_SKIN /tmpfs/vdr/plugins/text2skin/
      cp -rf /etc/vdr/plugins/text2skin/$ACT_SKIN /tmpfs/vdr/plugins/text2skin/
      for i in $T2S_DIRS ; do
         cp -rf /etc/vdr/plugins/text2skin/${i} /tmpfs/vdr/plugins/text2skin/
      done
      cd /etc/vdr/plugins/text2skin
      for i in * ; do
         if [ -d ${i} ] ; then
   	    ln -s /etc/vdr/plugins/text2skin/${i} /tmpfs/vdr/plugins/text2skin/ > /dev/null 2>&1
         fi
      done
   fi
   cd /etc/vdr
   for i in * ; do
      if [ -d ${i} ] ; then
	 ln -s /etc/vdr/${i} /tmpfs/vdr/ > /dev/null 2>&1
      fi
   done
   cd /etc/vdr/plugins
   for i in * ; do
      if [ -d ${i} ] ; then
	 ln -s /etc/vdr/plugins/${i} /tmpfs/vdr/plugins/ > /dev/null 2>&1
      fi
   done
   cd /etc/vdr/plugins/text2skin
else
   if [ "$1" = "save" ] ; then
      cp /tmpfs/epg.data /video/  > /dev/null 2>&1
      cp -u /tmpfs/vdr/* /etc/vdr/  > /dev/null 2>&1
      cp -u /tmpfs/vdr/plugins/* /etc/vdr/plugins/  > /dev/null 2>&1
      cp -ur /tmpfs/log/* /var/logold/  > /dev/null 2>&1
   else
      echo "Wrong parameter given - must be init or save"
   fi
fi
