#!/bin/sh

# Don't waste your time making this bash/ash/sh rc
# file executable or trying to create debian menus
# with it. The hashbang at the top is for syntax 
# highlighting.


# convenience variables
e='enlightenment'

eh_installdir ( ) {
  eh_echo "creating dir(s) $@"
  /usr/bin/install -g root -o root -d -m 755 "$@"
}

eh_echo ( ) {
  echo "ehelper: $@..."
}

# consolidate debhelper files into debian/ehrc
nospoon ( ) {
sed '/^#ehrc#/,$ d' ehrc > data 
echo -e "#ehrc# This is a regex target that begins the data section.\n" >> data
(for i in enl*; do echo "#file:$i"; sed 's/^/\#/' $i; echo -e "\n"; done | cat -s) >> data
mv ehrc ehrc.old; mv data ehrc
}

spoon ( ) {
  eh_echo "unpacking debhelper files"
  perl -we 'undef $/; while(<>) { while( /^#file:(.*?)^(.*?)^$/smg ) { $file=$1; $data=$2; $data =~ s/^#//smg; open FILE,">$file" || die "$file !?"; print FILE "$data" || die "$file !?"; close FILE || die "$file !?"; } }' ehrc
}

# place Debian menu stubs in menus.cfg
eh_menufilter ( ) {
eh_echo "adding Debian menu stub to menus.cfg"
perl -lwne '/^__E_CFG_VERSION 0$/ and print("$_\n\n", qw@BEGIN_NEW_FILE_MENU("DEBIAN_MENU", "ROOT", "/etc/enlightenment/menus/debian.menu")@, "\nEND_MENU") or /^ADD_MENU_SUBMENU_TEXT_ITEM.*APPS_SUBMENU/ and print("$_\n", qw@ADD_MENU_SUBMENU_TEXT_ITEM("Debian",        "DEBIAN_MENU")@) or print;' src/themes/configs/menus.cfg > debian/$e/usr/share/$e/config/menus.cfg
eh_installdir debian/$e/etc/$e/menus
}

# IF this is a CVS source tree, expand debhelper files
# and run ./autogen.sh. ELSE, just run ./configure.
eh_configure ( ) {
  if [ -d CVS ]; then
     eh_echo "CVS/ detected, assuming a CVS build"; (cd debian; spoon)
     test -f config.cache && eh_echo "removing config.cache" && rm config.cache
     eh_echo "executing autogen.sh"; ./autogen.sh "$@"
  else
     eh_echo "no CVS/ detected, assuming a normal build"
     test -f config.cache && eh_echo "removing config.cache" && rm config.cache
     eh_echo "executing the GNU configure script"; ./configure "$@"
  fi
}

eh_unpacktheme ( ) {
  local themedir="debian/$1/usr/share/enlightenment/themes/$2"
  local srcdir="src/themes"
  eh_installdir $themedir
  eh_echo "unpacking $2.etheme to $themedir"
  
  tar zxpf "$srcdir/$2.etheme" -C $themedir
}

eh_installthemes ( ) {
  eh_unpacktheme "$e-theme-bluesteel"  "BlueSteel"
  eh_unpacktheme "$e-theme-shinymetal" "ShinyMetal"
  eh_unpacktheme "$e"                  "BrushedMetal-Tigert"
}

eh_makfilefilter ( ) {
  eh_echo "disabling src/themes/Makefile's theme unpacking routine"
  perl -ni'.old' -lwe '/^install-data-local:$/ .. /^$/ or print' src/themes/Makefile
}

eh_undocumented ( ) {
  eh_echo "adding undocumented man page links"
  local needdocs=$(cd debian/$e/usr/bin ; ls | sed  '/^enlightenment$/d; s#$#.1#' | xargs)
  eh_echo "$needdocs"
  dh_undocumented -p $e $needdocs
  eh_installdir debian/$e/usr/share/doc/$e
  cp src/ChangeLog debian/$e/usr/share/doc/$e/changelog
}

eh_clean ( ) {
  rm -f core `find . \( -name '*.orig' -o -name '*.rej' -o -name '*~' \
    -o -name '*.bak' -o -name '#*#' -o -name '.*.orig' \
    -o -name '.*.rej' -o -name '.SUMS' -o -name '*.old' \) -print`
}

# install meta function
eh_install ( ) {
  eh_menufilter
  eh_installthemes
  eh_undocumented
  perl -pi -lwe 's/^EBIN=.*/EBIN=\/usr\/bin/' debian/$e/usr/bin/$e.install
  eh_clean
}

#ehrc# This is a regex target that begins the data section.

#file:enlightenment-theme-bluesteel.README.Debian
#See the enlightenment package's /usr/share/doc/enlightenment/ 
#for additional information.

#file:enlightenment-theme-shinymetal.README.Debian
#See the enlightenment package's /usr/share/doc/enlightenment/ 
#for additional information.

#file:enlightenment.README.Debian
#Enlightenment for Debian/GNU Linux
#----------------------------------
#
#NOTES for users of KDE/GNOME and those not using KDE/GNOME:
#  There are a number of config files with pretty obvious names
#  in /usr/share/enlightenment/config. You can go there and
#  copy or the appropriate files to the normal names. As an
#  example:
#  
#    cp keybindings.gmc.cfg keybindings.cfg
#    
#  This can also be accomplished by copying the appropriate
#  files to $HOME/.enlightenment/
#
#Visit http://www.debian.org for the latest official Debian binary and
#source code enlightenment packages.
#
#Visit http://www.debian.org/~ljlane/ for the latest bug tracking,
#news, and pre/experimental package releases of Enlightenment for
#Debian.
#
#Visit http://www.enlightenment.org/ for the latest enlightenment news
#and source code.
#
#
#Laurence J. Lane <ljlane@debian.org>, Fri,  5 Nov 1999 20:33:57 -0500

#file:enlightenment.conffiles
#/etc/menu-methods/enlightenment

#file:enlightenment.docs
#NEWS README INSTALL AUTHORS

#file:enlightenment.menu
#?package(enlightenment):needs="wm" section="WindowManagers"\
#   title="Enlightenment" command="/usr/bin/enlightenment"

#file:enlightenment.menu-method
##!/usr/sbin/install-menu
#
#compat="menu-1";
#!include menu.h
#
#compat="menu-2";
#
## choose a terminal program here:
#  function term_prog()="Eterm";
#  #function term_prog()="rxvt";
#  #function term_prog()="xterm";
#
#treewalk=c(m);
#rootprefix="/etc/enlightenment/menus/";
#userprefix="$HOME/.enlightenment/menus_debian/";
#mainmenutitle="Debian Menu";
#
#function menu_sh() = prefix() "debian_menu_sh";
#prerun="set -e; rm -f " prefix() "debian*menu/" menu_sh();
#postrun="set -e; cat " menu_sh() "| sed 's/_debian/debian/g' | sh; rm -f " menu_sh();
#
#function quote($text)= "\"" $text "\"";
#function tick($text)= "\'" $text "\'";
#
#function space()= "   ";
#function newline()="\n";
#
#function entry($com)= quote(esc(title(),"\"")) space() 
#                      quote(ifelse(icon(),icon(),"NULL")) space() 
#		      quote("exec") space() 
#		      $com;
#
#function term($com)= term_prog() " -T " tick(title()) " -e " $com;
#
#supported;
#  wm=         entry(quote("eesh -e restart_wm " $command) newline());
#  x11=        entry(quote($command) newline());
#  text=       entry(quote(term($command)) newline());
#endsupported;
#
#function etitle()= tolower(replacewith($section,"/","_") ".menu");
#
#startmenu= "cat > " prefix() etitle() " << END" newline() quote(title()) newline();
#
#endmenu= "END" newline() newline();
#
#submenutitle="\""title()"\"  NULL   menu \"" prefix() etitle() "\"\n";
#
#genmenu="debian_menu_sh";

#file:enlightenment.postinst
##!/bin/sh -e
#
#if test -x /usr/sbin/register-window-manager >/dev/null 2>&1; then
#	/usr/sbin/register-window-manager --add /usr/bin/enlightenment
#fi
#
##DEBHELPER#
#
#exit 0

#file:enlightenment.postrm
##!/bin/sh -e
#
#if test "$1" = "purge" -a -x /usr/sbin/register-window-manager; then
#   /usr/sbin/register-window-manager --remove /usr/bin/enlightenment
#fi
#
##DEBHELPER#
#
#exit 0
#

#file:enlightenment.preinst
##!/bin/sh -e
#
#dpkg --assert-long-filenames 2>&1 > /dev/null || { echo "requires dpkg (>= 1.4.17)"; exit 1; }
#
#theme_dir='/usr/share/enlightenment/themes'
#
#if test -f "$themedir/BrushedMetal-Tigert"; then
#  local date=$(date -R | tr ' ' '_')
#  echo "warning: attempting to move $themedir/BrushedMetal-Tigert to $themedir/BrushedMetal-Tigert.$date"
#  mv -v "$themedir/BrushedMetal-Tigert" "$themedir/BrushedMetal-Tigert.$date"
#  echo "You should probably remove $themedir/BrushedMetal-Tigert.$date"
#fi
#
##DEBHELPER#
#
#exit 0

#file:enlightenment.prerm
##!/bin/sh -e
#
#find /etc/enlightenment/menus/ -type f -name 'debian*.menu' -print0 | xargs -0r rm -f
#
##DEBHELPER#
#
#exit 0

