#!/bin/sh
#
# kbootchart - kde frontend for bootchart
# 
# Copyright (c) 2006 by Stefano Cotta Ramusino <stefano.cotta@qilinux.it>

lang="java"

if [ -z "$JAVA_HOME" ]; then
   JRE_HOME="/usr/java/jre"
   JDK_HOME="/usr/java/jdk"
   if [ -x "$JRE_HOME/bin/java" ]; then
      JAVA_HOME=$JRE_HOME
   elif [ -x "$JDK_HOME/bin/java" ]; then
      JAVA_HOME=$JDK_HOME
   elif [[ $(host www.qilinux.org &> /dev/null; echo $?) -eq 0 ]]; then
      lang="bash"
   elif [ -x "$(which perl 2> /dev/null)" ]; then
      lang="perl"
   else
      kdialog --title "KBootchart" \
         --icon $(basename $0) \
         --error "Perl is missing in your system.\nTo create the bootchart image you need to install perl."
      exit 1
   fi
fi

if [[ "$lang" == "java" || "$lang" == "bash" ]]; then
   format="$(kdialog --title "KBootchart" \
      --icon $(basename $0) \
      --radiolist "Select the output image format:" \
      png "PNG" on svg "SVG" off eps "EPS" off)"

   if [ -z "$format" ]; then
      exit 1
   fi
else
   format="svg"
fi

output_dir="$(kdialog --title "Select the directory where the image will be created" \
   --icon $(basename $0) \
   --getexistingdirectory ~)"

if [ -z "$output_dir" ]; then
   exit 1
fi

if [ $format == "svg" && $lang != "perl" ]]; then
   output_ext_file="svgz"
else
   if [ $format == "eps" ]; then
      output_ext_file="eps.gz"
   else
      output_ext_file=$format
   fi
fi

if [ "$lang" == "java" ]; then
   PATH=$JAVA_HOME/bin:$PATH bootchart -f $format -o $output_dir &> /dev/null
elif [ "$lang" == "bash" ]; then
   bootchart.sh $format $output_dir &> /dev/null
else
   bootchart.pl > $output_dir/bootchart.$output_ext_file 2> /dev/null
fi

if [ $? -ne 0 ]; then
   kdialog --title "KBootchart" \
      --icon $(basename $0) \
      --error "Error during creation of image file."
   exit 1
fi

kdialog --title "KBootchart" \
   --icon $(basename $0) \
   --msgbox "File bootchart.$output_ext_file successfully created"

exit $?
