# Lukasz Pawelczyk <havner@pld-linux.org> 2004
# $Id: functions-live,v 1.21 2006/10/17 10:17:56 havner Exp $

SYMLINKS="
/etc/X11/WindowMaker
/etc/X11/gdm
/etc/X11/icewm
/etc/X11/xkb
/etc/X11/xscreensaver
/etc/bluetooth/firmware
/etc/cups/ppds.dat
/etc/elinks
/etc/gconf
/etc/gimp
/etc/hotplug
/etc/joe
/etc/log.d
/etc/mplayer
/etc/p0f
/etc/pcmcia
/etc/php
/etc/sensors.conf
/etc/security
/etc/sound
/etc/xfce4
/etc/Muttrc
/etc/bash_completion
/etc/fb.modes
/etc/gnome-vfs-mime-magic
/etc/lineakkb.def
/etc/lynx.cfg
/etc/mime.types
/etc/pci.ids
/etc/pine.conf
/etc/services
/etc/slrn.rc
/home/services/httpd/error
/home/services/httpd/icons
/home/services/httpd/manual
/var/lib/arpwatch
/var/lib/mks
/var/lib/rpm
/var/lib/scrollkeeper"

GLFILES="
/usr/lib/libGL.so
/usr/lib/libGL.so.1
/usr/X11R6/lib/libGL-nvidia.so.1.0
/usr/X11R6/lib/libGL-xfree.so.1.2
/usr/X11R6/lib/libGL-fglrx.so.1.2
/usr/X11R6/lib/libGL.so
/usr/X11R6/lib/libGLcore.so.1.0
/usr/X11R6/lib/libXvMCNVIDIA.so.1.0
/usr/X11R6/lib/libfglrx_gamma.so.1.0
/usr/X11R6/lib/modules/extensions/libglx-xfree.a
/usr/X11R6/lib/modules/extensions/libglx-nvidia.so.1.0
/usr/X11R6/lib/modules/extensions/libglx.a
/usr/X11R6/lib/modules/extensions/libglx.so
/usr/X11R6/lib/modules/extensions/libGLcore.a
/usr/X11R6/lib/modules/drivers/nvidia_drv.o
/usr/X11R6/lib/modules/drivers/fglrx_drv.o
/usr/X11R6/lib/modules/dri/fglrx_dri.so
/usr/X11R6/lib/modules/linux/libfglrxdrm.a
/usr/X11R6/bin/fgl_glxgears
/usr/X11R6/bin/fglrxconfig
/usr/X11R6/bin/fglrxinfo
/usr/X11R6/bin/fireglcontrol
/etc/live-alt"

INITRD_MODULES="
drivers/block/loop.ko.gz
drivers/cdrom/cdrom.ko.gz
drivers/ide/pci/*
drivers/ide/ide-cd.ko.gz
drivers/ide/ide-core.ko.gz
drivers/ide/ide-disk.ko.gz
drivers/ide/ide-generic.ko.gz
drivers/scsi/*
fs/isofs/*
fs/nls/nls_cp437.ko.gz
fs/nls/nls_iso8859-1.ko.gz
fs/squashfs/*
lib/zlib_inflate/*"

getparam()
{
	CMDLINE=" `cat /proc/cmdline` "

	case "$CMDLINE" in
		*\ $1=*)
			;;
		*\ $1*)
			return 0;
			;;
		*)
			return 1;
			;;
	esac
	result="${CMDLINE##*$1=}"
	result="${result%%[     ]*}"
	echo "$result"
	return 0
}

get_parts_linux() {
	sfdisk -l 2>/dev/null | grep "Linux$" | awk '{print $1}' | awk -F"/" '{print $3}'
}

get_parts_swap() {
	sfdisk -l 2>/dev/null | grep "Linux\ swap" | awk '{print $1}' | awk -F"/" '{print $3}'
}

get_parts_other() {
	sfdisk -l 2>/dev/null | grep -v "Linux$" | grep -v "swap$" | grep -v "Ext" | grep -v "Empty" | awk '{print $1}' | awk -F"/" '{print $3}'
}

get_parts_fat() {
	sfdisk -l 2>/dev/null | grep "FAT" | awk '{print $1}' | awk -F"/" '{print $3}'
}

get_parts_ntfs() {
	sfdisk -l 2>/dev/null | grep "NTFS$" | awk '{print $1}' | awk -F"/" '{print $3}'
}

get_disks() {
	sfdisk -l 2>/dev/null |grep "Disk /dev" |awk '{print $2}' |sed -e "s#/dev/##" |tr -d ":"
}

get_cdroms() {
# ide
dmesg | grep "^hd.:.*CD" | awk '{print $1}' | sed -e "s/://g" | sort | uniq
# scsi
dmesg | grep "^sr.:" | awk '{print $1}' | sed -e "s/://g;s/sr/scd/g" | sort | uniq
}

check_root() {
if [ "`id -u`" != "0" ]; then
	echo "You must be root to run this script."
	exit 1
fi
}

load_config() {

# default values for all livecd remaster scripts
MAINDIR=~/LIVECD
CD=$MAINDIR/CD
INITRD=$MAINDIR/INITRD
ROOTFS=$MAINDIR/ROOTFS
ISODIR=$MAINDIR
SQUASHFS=$MAINDIR/SQUASHFS
MNT_CDROM=/media/cdrom
MNT_INITRD=$MAINDIR/mnt_initrd
MNT_SQUASHFS=$MAINDIR/mnt_squashfs
LIVECD_VERSION="PLD LiveCD remastered"

if [ -n "$HOME_ETC" -a -f "$HOME_ETC/.livecd_remaster" ]; then
	. $HOME_ETC/.livecd_remaster
elif [ -f ~/.livecd_remaster ]; then
	. ~/.livecd_remaster
else
	echo "There is no config file in your home or \$HOME_ETC dir."
	echo "Default values has been loaded."
fi

if [ "$1" = "nocheck" ]; then
	return
fi

[ -d "$MAINDIR" ]	|| (echo "Main dir: \"$MAINDIR\" does not exist."; exit 1)
[ -d "$CD" ]		|| (echo "Cd dir: \"$CD\" does not exist."; exit 1)
[ -d "$INITRD" ]	|| (echo "Initrd dir: \"$INITRD\" does not exist."; exit 1)
[ -d "$ROOTFS" ]	|| (echo "Rootfs dir: \"$ROOTFS\" does not exist."; exit 1)
[ -d "$ISODIR" ]	|| (echo "ISO dir: \"$ISODIR\" does not exist."; exit 1)

[ -d `dirname "$SQUASHFS"` ]		|| (echo "Parent dir for squashfs: \"`dirname \"$SQUASHFS\"`\" does not exist"; exit 1)
[ -d `dirname "$MNT_INITRD"` ]		|| (echo "Parent dir for mounting initrd: \"`dirname \"$MNT_INITRD\"`\" does not exist"; exit 1)
[ -d `dirname "$MNT_SQUASHFS"` ]	|| (echo "Parent dir for mounting squashfs: \"`dirname \"$MNT_SQUASHFS\"`\" does not exist"; exit 1)

[ -d "$MNT_CDROM" ]	|| (echo "CD-ROM mount point: \"$MNT_CDROM\" does not exist."; exit 1)

}

check_deps() {
while [ "$#" != "0" ]; do
	if [ ! -f "$ROOTFS/$1" -a ! -f "$ROOTFS`chroot $ROOTFS which $1 2>/dev/null`" ]; then
		echo "Install $1 before running this script."
		FAIL=true
	fi
	shift
[ "$FAIL" = "true" ] && exit 1
done
}

check_deps_local() {
while [ "$#" != "0" ]; do
	if [ ! -f "$1" -a ! -f "`which $1 2>/dev/null`" ]; then
		echo "Install $1 before running this script."
		FAIL=true
	fi
	shift
[ "$FAIL" = "true" ] && exit 1
done
}

# load_profiles device name1 name2 name3...
# if device=cd load from LiveCD/profiles/
# it assumes that all modules/devices/mountpoints are ok
load_profiles() {
if [ "$#" -lt "2" ]; then
	echo "${LREDB}Not enough parameters for load_profiles()${LNORMAL}"
	return
fi

DEV=$1
shift

if [ "$DEV" = "cd" ]; then
	DIR=/media/LiveCD/profiles
else
	DIR=/media/floppy
	if ! mount -n -o ro -t auto "$DEV" "$DIR"; then
		echo "${LREDB}Cannot mount ${DEV}${LNORMAL}"
		return
	fi
fi

while [ ! "$#" = "0" ]; do
	if [ -f ${DIR}/$1.tar.gz ]; then
		echo "${LBLUEB}Copying packed profile files ($1)${LNORMAL}"
		cd /
		tar xzpf ${DIR}/$1.tar.gz
	elif [ -f ${DIR}/$1.tgz ]; then
		echo "${LBLUEB}Copying packed profile files ($1)${LNORMAL}"
		cd /
		tar xzpf ${DIR}/$1.tgz
	elif [ -f ${DIR}/$1.tar.bz2 ]; then
		echo "${LBLUEB}Copying packed profile files ($1)${LNORMAL}"
		cd /
		tar xjpf ${DIR}/$1.tar.bz2
	elif [ -f ${DIR}/$1.tbz ]; then
		echo "${LBLUEB}Copying packed profile files ($1)${LNORMAL}"
		cd /
		tar xjpf ${DIR}/$1.tbz
	fi
	if [ -d ${DIR}/$1 ]; then
		echo "${LBLUEB}Copying profile files ($1)${LNORMAL}"
		cp -Rpdf ${DIR}/$1/* /
	fi
	if [ -f ${DIR}/$1.exec ]; then
		echo "${LBLUEB}Executing profile file ($1)${LNORMAL}"
		cp ${DIR}/$1.exec /tmp
		chmod 755 /tmp/$1.exec
		/tmp/$1.exec
		rm -f /tmp/$1.exec
	fi
	shift
done

if [ ! "$DEV" = "cd" ]; then
	umount -n "$DEV" > /dev/null 2>&1
fi
	
}

# vi: syntax=sh:shiftwidth=8:
