#!/bin/bash

piece=$1
ooo_build_tag=$2
app=$3

source $OO_TOOLSDIR/piece/sys-setup

# hack for older SUSE distributions (to pass build checks)
list_icon_dirs=
if test "$DISTRO" = "SUSE-10.1" ; then
    list_icon_dirs=1
fi

create_wrapper()
{
    mkdir -p $DESTDIR$OO_PREFIX/bin
    cat <<EOT >$DESTDIR$OO_PREFIX/bin/$1
#!/bin/sh
$OO_INSTDIR/program/$2 $3 "\$@"
EOT
    chmod 755 $DESTDIR$OO_PREFIX/bin/$1
    test -n "$DESTDIR" && echo "$OO_PREFIX/bin/$1" >>files-$piece.txt
}

create_man_link()
{
    mkdir -p $DESTDIR$OO_MANDIR/man1
    echo ".so man1/openoffice.1" >| $DESTDIR$OO_MANDIR/man1/$1.1
    gzip $DESTDIR$OO_MANDIR/man1/$1.1
    test -n "$DESTDIR" && echo "$OO_MANDIR/man1/$1.1.gz" >>files-$piece.txt
}

install_man()
{
    mkdir -p $DESTDIR$OO_MANDIR/man1
    man_page_in=$OO_TOOLSDIR/../man/$1.1.in
    # use the distro specific man page if available
    if test -f $OO_TOOLSDIR/../man/$1.1_${DISTRO%%-*}.in ; then
        man_page_in=$OO_TOOLSDIR/../man/$1.1_${DISTRO%%-*}.in
    fi
    sed -e "s|@BINSUFFIX@|$BINSUFFIX|g" $man_page_in \
	>| "$DESTDIR$OO_MANDIR/man1/$1.1" || exit 1;
    gzip $DESTDIR$OO_MANDIR/man1/$1.1
    test -n "$DESTDIR" && echo "$OO_MANDIR/man1/$1.1.gz" >>files-$piece.txt
}

install_desktop_file()
{
    mkdir -p $DESTDIR/usr/share/applications
    cp $OO_TOOLSDIR/../desktop/$1.desktop $DESTDIR/usr/share/applications || exit 1
    test -n "$DESTDIR" && echo "/usr/share/applications/$1.desktop" >>files-$piece.txt
}

install_icon()
{
    category=$1
    icon_name=$2
    icon_dir=/usr/share/icons/hicolor
    # FIXME: fix the structure of icons in ooo-build/desktop direcotry
    test "$category" = "apps" && category_indir= || category_indir=$category

    test -n "$list_icon_dirs" && echo "%dir $icon_dir" >>files-$piece.txt

    for size in 16x16 22x22 24x24 32x32 48x48 scalable ; do
	# note that the scalable directory includes .svg icons
	file=$OO_TOOLSDIR/../desktop/$category_indir/$size/$icon_name.[ps][nv]g
	filename=`basename $file`
	target_dir="$icon_dir/$size/$category"
	test -f $file || continue;
	mkdir -p "$DESTDIR$target_dir"
	cp $file "$DESTDIR$target_dir/$filename" || exit 1;
	test -n "$DESTDIR" && echo "$target_dir/$filename" >>files-$piece.txt
	test -n "$list_icon_dirs" && echo "%dir $icon_dir/$size" >>files-$piece.txt
	test -n "$list_icon_dirs" && echo "%dir $icon_dir/$size/$category" >>files-$piece.txt
    done

    # create symlink below share/pixmaps to keep the backward compatibility
    if test "$category" = "apps" ; then
	file=$DESTDIR/$icon_dir/48x48/apps/$icon_name.png
	if test -f $file ; then
	    filename=`basename $file`
	    pixmaps_dir=/usr/share/pixmaps
	    mkdir -p $DESTDIR$pixmaps_dir
	    ln -sf $icon_dir/48x48/apps/$filename $DESTDIR$pixmaps_dir
	    test -n "$DESTDIR" && echo "$pixmaps_dir/$filename" >>files-$piece.txt
	fi
    fi
}

case "$app" in
    "fromtemplate")
	create_wrapper "oofromtemplate" "soffice" "" || exit 1;
	install_desktop_file template || exit 1;
	;;
    "unopkg")
	create_wrapper unopkg unopkg "" || exit 1;
	install_desktop_file ooo-extension-manager || exit 1;
	install_man $app || exit 1;
	install_icon mimetypes application-vnd.openofficeorg.extension || exit 1;
	;;
    "ooffice")
	create_wrapper ooffice soffice "" || exit 1;
	create_man_link ooffice openoffice || exit 1;
	install_desktop_file startcenter || exit 1;
	install_icon apps ooo-gulls || exit 1;
	install_man openoffice || exit 1;
	;;
    *)
	create_wrapper oo$app soffice "-$app" || exit 1;
	create_man_link oo${app} openoffice || exit 1;
	install_desktop_file $app || exit 1;
	install_icon apps ooo-$app || exit 1;
	;;
esac
