#!/bin/sh

#####################################
# Copying in RAM all movix files...
#################
cp -a /cdrom/movix/profile            /etc/
chmod a+x /etc/profile
cp -a /cdrom/movix/mixer.pl           /usr/local/bin/
chmod a+x /usr/local/bin/mixer.pl
cp -a /cdrom/movix/rc.lirc            /etc/rc.d
chmod a+x /etc/rc.d/rc.lirc
cp -a /cdrom/movix/setHardware.pl     /usr/local/bin/
chmod a+x /usr/local/bin/setHardware.pl
cp -a /cdrom/movix/movix.pl           /usr/local/bin/
chmod a+x /usr/local/bin/movix.pl
cp -a /cdrom/movix/bugReport.sh       /usr/local/bin/
chmod a+x /usr/local/bin/bugReport.sh
cp -a /cdrom/movix/isapnpmodules.sh   /sbin
chmod a+x /sbin/isapnpmodules.sh
cp -a /cdrom/movix/showisapnpmodules.sh   /sbin
chmod a+x /sbin/showisapnpmodules.sh
cp -a /cdrom/movix/movixrc            /root/.movixrc
#mkdir /root/.mplayer/
cp -a /cdrom/movix/config             /root/.mplayer/
cp -a /cdrom/movix/*conf              /root/.mplayer/
cp -a /cdrom/movix/manpage.txt        /root/.mplayer/
cp -a /cdrom/movix/lircrc             /root/.lircrc
cp -a /cdrom/movix/lircd.conf         /etc/
mkdir -p /tmp/codecs/
ln -s /tmp/codecs/ /usr/local/win32

# copy and load the international keyboard layout
mkdir -p /usr/local/share
cp -a /cdrom/movix/shellkey.map /usr/local/share
loadkmap < /usr/local/share/shellkey.map

if [ -e /cdrom/isolinux/help.txt ]; then
	cp /cdrom/isolinux/help.txt /etc
fi

if [ -e /cdrom/movix/libdvdcss.so ] ; then
    cp -a /cdrom/movix/libdvdcss.so   /lib/libdvdcss.so.2
fi
if [ -e /cdrom/movix/libdvdcss-1.2.6-1.i386.rpm ] ; then
    rpm2cpio /cdrom/movix/libdvdcss-1.2.6-1.i386.rpm > /tmp/libdvdcss-1.2.6-1.i386.cpio
    cd /tmp
    cpio -i -d -F libdvdcss-1.2.6-1.i386.cpio
    cp -a usr/lib/libdvdcss.so.2*     /lib
fi    

### Loading the QuickTime DLLs
if [ `find /cdrom/mplayer/codecs/ -name "qt*"` ] ; then
    echo "Loading QuickTime DLLs in RAM..."
    for i in `ls /cdrom/mplayer/codecs/qt*` ; do cd /tmp/ && bunzip2 -c $i | tar -x ; done;
    ln -s /tmp/qt*/* /tmp/codecs/
fi

### Loading the RealPlayer DLLs
if [ `find /cdrom/mplayer/codecs/ -name "rp*"` ] ; then
    echo "Loading RealPlayer DLLs in RAM..."
    for i in `ls /cdrom/mplayer/codecs/rp*` ; do cd /tmp/ && bunzip2 -c $i | tar -x ; done;
    ln -s /tmp/rp*/* /tmp/codecs/
fi

### Loading the Win32 DLLs
if [ `find /cdrom/mplayer/codecs/ -name "win32*"` ] ; then
    echo "Loading Win32 DLLs in RAM..."
    for i in `ls /cdrom/mplayer/codecs/win32*` ; do cd /tmp && bunzip2 -c $i | tar -x ; done;
    ln -s /tmp/win32*/* /tmp/codecs/
fi

### Loading the Xanim DLLs
if [ `find /cdrom/mplayer/codecs/ -name "xanim*"` ] ; then
    echo "Loading Xanim DLLs in RAM..."
    for i in `ls /cdrom/mplayer/codecs/xanim*` ; do cd /tmp && bunzip2 -c $i | tar -x ; done;
    ln -s /tmp/xanim*/* /tmp/codecs/
fi

### It's easier to do this than including ldconfig in MoviX
ln -s /usr/local/mplayer/lib/lib* /usr/lib
### Let's use aliases for the movix script to let play dvd, vcd, mp3 CD & audio cd
ln -s /usr/bin/movix /usr/bin/dvd
ln -s /usr/bin/movix /usr/bin/vcd
ln -s /usr/bin/movix /usr/bin/acd
ln -s /usr/bin/movix /usr/bin/mcd

##############################################
###  Load a few modules
########################
/usr/local/bin/setHardware.pl

### Start the lirc daemon if a remote is found
/etc/rc.d/rc.lirc

##############################################
#     Detect & load ISA modules              #
##############################################
    
get_isapnp_modules () {
   while read IGNORE ID IGNORE
       do
         DEVICE="0x"`echo $ID | cut -b6-7``echo $ID | cut -b4-5`
         cat /lib/modules/`uname -r`/modules.isapnpmap | grep "^[^ ][^ ]*  *[^ ][^ ]*  *$DEVICE " | cut -f 1 -d " "
       done
}

for MODULE in `get_isapnp_modules < /proc/bus/isapnp/devices | sort -u`
do 
  echo Loading module $MODULE
  modprobe -s $MODULE
done

/bin/setterm -blank 0
