#!/bin/bash
#
# splash_geninitramfs -- an utility to create initramfs images for use with fbsplash
#
# Copyright (C) 2004-2007, Michal Januszewski <spock@gentoo.org>
#
# This file is a part of the splashutils package.
#
# This file is subject to the terms and conditions of the GNU General Public
# License v2.  See the file COPYING in the main directory of this archive for
# more details.
#

cleanup()
{
	rm -rf ${workdir}
}

usage()
{
	cat <<EOTB
splash_geninitramfs/splashutils-1.5.4.4
Usage: splash_geninitramfs [options] [--all|theme ...]

Main operation modes:
  -g, --generate=IMG  generate an initramfs image with all necessary files
  -a, --append=IMG    append a theme and helper files (if necessary)
                      to an initramfs image
  -c, --copy=DIR      copy all necessary files into the specified directory;
                      DIR should point to the root directory of an initramfs
  -o, --overlay=DIR   include the contents of DIR in the initramfs image
  -h, --help          show this help message

Options:
  -r, --res=RES   copy data for specific resolutions only; RES is a
                  comma-separated list of the resolutions for which the images
		  are to be copied
  -v, --verbose   verbose output
      --no8bpp    ignore 8bpp images (can save a lot of space)
EOTB

#   -d              use dynamically linked splash helper
}

printv()
{
	if [ $verbose -gt 0 ]; then
		echo "$*"
	fi
}

put_item()
{
	item="$1"

	if [ "${item:0:1}" == "/" ]; then
		cp -pRH --parents "${item}" "${imgdir}"
	else
		cp -pRH --parents "${themedir}/${theme}/${item}" "${imgdir}"
	fi
}

themedir="//etc/splash"
declare -a themes
mode="h"
splash_hlp="//usr/bin/fbcondecor_helper"
res=""
overlay=""
verbose=0
index=0
no8bpp=0

args="$@"
temp=`getopt -l no8bpp,all,generate:,append:,copy:,overlay:,help,verbose,res: a:g:c:r:o:hv "$@"`

if [ $? != 0 ]; then
	usage; exit 2
fi

eval set -- "$temp"

for i ; do
	case "$i" in
		-a|--append) 	mode='a'; img="$2"; shift; shift;;
		-g|--generate)	mode='g'; img="$2"; shift; shift;;
		-c|--copy)		mode='c'; destdir="$2"; shift; shift;;
		-h|--help)		usage; exit 2;;
		-r|--res)		res=${2/,/ }; shift; shift;;
		-v|--verbose)	verbose=$(($verbose + 1)); shift;;
		-o|--overlay)	overlay="$2"; shift; shift;;
		--no8bpp)		no8bpp=1; shift;;
		--)				shift; break;;
		--all)
				shift;
				for i in ${themedir}/* ; do
					if [ ! -d "$i" ] ; then
						continue
					fi
					themes[$index]="`basename "$i"`"
					let "index++"
				done;;
	esac
done

if [ "$mode" == "h" ]; then
	usage ; exit 2
fi

if [ $index -eq 0 ]; then
	for i ; do
		themes[$index]="$i"
		let "index++"
	done
fi

if [ $index -eq 0 ]; then
	echo "No themes specified." 1>&2 ; exit 5
fi

if [ "$mode" == "c" ]; then
	if [ ! -d $destdir ]; then
		echo "Destination directory does not exist." 1>&2 ; exit 3
	fi
	imgdir=$destdir
else
	if [ "$mode" == "a" ] && [ ! -e $img ]; then
		echo "Specified image file does not exist." 1>&2 ; exit 4
	fi

	workdir=${TMPDIR-/tmp}/splash.$$.$RANDOM

	if (umask 077 && mkdir $workdir); then
		trap "cleanup" EXIT
	else
		echo "Could not create temporary directory! Exiting." 1>&2 ; exit 1
	fi
fi

if [ "$mode" != "c" ]; then
	imgdir="${workdir}/img"
	mkdir "${imgdir}"
fi

if [ "$mode" == "a" ]; then
	printv "o Unpacking $img.."
	cp "$img" "${imgdir}"
	(cd "${imgdir}"; (gunzip -c "$(basename $img)" 2>/dev/null || cat "$(basename $img)") | cpio -idm --no-absolute-filenames --quiet -H newc)
	rm -f "${imgdir}/$(basename $img)"
fi

printv "o Creating directory structure.."
mkdir -p ${imgdir}/{dev,dev/fb,dev/misc,dev/vc,$themedir,//lib/splash/proc,root,usr/bin,/usr/lib/splash/sys}

if [ $EUID == 0 ]; then
	[[ ! -e "${imgdir}/dev/null" ]] 	&& mknod "${imgdir}/dev/null" c 1 3
	[[ ! -e "${imgdir}/dev/console" ]] 	&& mknod "${imgdir}/dev/console" c 5 1
	[[ ! -e "${imgdir}/dev/tty0" ]]		&& mknod "${imgdir}/dev/tty0" c 4 0
else
	echo "Since you are running this script as a non-root user, no device nodes" 1>&2
	echo "will be created in the initramfs.  Make sure your initramfs contains" 1>&2
	echo "at least the following nodes: /dev/null, /dev/console, /dev/tty0." 1>&2
fi

if [ ! -e ${splash_hlp} ]; then
	echo "${splash_hlp} does not exist." 1>&2 ; exit 4
fi

printv "o Copying ${splash_hlp}.."
cp "${splash_hlp}" "${imgdir}//usr/bin"
ln -sf "${splash_hlp}" "${imgdir}//usr/bin/splash_helper"

res=${res//,/ }

printv "o Copying themes.."
for (( i=0 ; i < index ; i++ )) ; do
	theme=${themes[$i]}

	printv "  - ${theme}"

	# check if the user specified which resolutions are accepted
	# (default: all res)
	if [ -z "$res" ]; then
		res=$(cd ${themedir}/${theme} ; ls *cfg | sed -e 's/.cfg//g')
	fi

	for j in $res ; do
		if [ ! -e "${themedir}/${theme}/${j}.cfg" ]; then
			echo "Warning: config file for theme '${theme}', resolution ${j} does not exist!" 1>&2
			continue
		fi

		cp -pRH --parents "${themedir}/${theme}/${j}.cfg" "${imgdir}"

		if [[ ${no8bpp} == 0 ]]; then
			t="[0-9*]"
		else
			t=""
		fi

		# config file parsing
		pics=`cat "${themedir}/${theme}/${j}.cfg" | \
		      sed -r -e "/(^(silent)?jpeg=)|(^(silent)?pic${t}*=)/! d" \
			     -e 's/[a-z0-9]+=(.*)/\1/'`
		for pic in $pics ; do
			put_item "${pic}"
		done

		icons=`cat "${themedir}/${theme}/${j}.cfg" | \
		       egrep -v '^#' | grep 'icon ' | awk '{print $2}'`
		for icon in ${icons} ; do
			put_item "${icon}"
		done

		fonts=`cat "${themedir}/${theme}/${j}.cfg" | \
		       egrep -v '^#' | grep 'text ' | grep -o '[^ ]\+\.ttf'`
		for font in ${fonts} ; do
			put_item "${font}"
		done

		font=`cat "${themedir}/${theme}/${j}.cfg" | egrep -v '^#' | grep 'text_font='`
		font=${font#*=}
		if [[ -n "${font}" ]]; then
			put_item "${font}"
		else
			put_item "${themedir}/luxisri.ttf"
		fi
	done
done

if [ -n "${overlay}" ]; then
	printv "o Adding files from overlay: $overlay"
	cp -pRH ${overlay}/* "${imgdir}"
fi

printv "o Creating initramfs image.."
if [ "$mode" == "g" ] || [ "$mode" == "a" ]; then
	(cd "${imgdir}" ; find . | cpio --quiet -o -H newc | gzip -9 >../img.cpio.gz)
	mv "${workdir}/img.cpio.gz" "${img}"
fi

exit 0

# vim: set ts=4 sts=4 :
