#!/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 ( ) {
  /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
}

# 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-theme-ganymede"   "Ganymede" 
  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/enlightenment/usr/bin ; ls | sed '/^enlightenment$/d;s/$/.1/' | xargs)
  eh_echo "$needdocs"
  dh_undocumented -p $e $needdocs
}

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' \) -print`
}

# install meta function
eh_install ( ) {
  eh_menufilter
  eh_installthemes
  eh_undocumented
  eh_clean
}

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

#file:enlightenment-theme-bluesteel.README.debian
#See /usr/share/doc/enlightenment/ for additional information.

#file:enlightenment-theme-ganymede.README.debian
#See /usr/share/doc/enlightenment/ for additional information.

#file:enlightenment-theme-shinymetal.README.debian
#See /usr/share/doc/enlightenment/ for additional information.

#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"
#
#genmenu=replacewith(parent($section), "/", "_") ".menu"
#rootprefix="/etc/enlightenment/menus"
#treewalk="cm"
#rootsection="Debian"
#
## Limit usage to root for now until we figure out how to convince the
## E menu to ignore the system menus if user menus exist
#onlyrunasroot=true
#
#function quote($target)= "\"" $target "\""
#function tick($target)= "\'" $target "\'"
#
## choose your term here, uncomment one
#function term()="Eterm -T"
##function term()="xterm -T"
##function term()="rxvt -title"
#
#function menu()= quote(prefix() "/" replacewith($section , "/", "_") ".menu")
#
#function icon_select()=ifelse($icon32x32, $icon32x32, \
#     ifelse($icon16x16, $icon16x16, $icon))
#function icon()= quote(ifelse(icon_select(),icon_select(),"NULL"))
#
#function title()= quote(esc($title,"\""))
#
#function x11()= quote($command)
#function text()= quote(term() " " tick(esc($title,"\"")) " -e " $command)
#function wm()= quote("eesh -e " tick("restart_wm " $command))
#
#function menu_entry($name,$icon,$action,$parameters)=\
#firstentry(quote(basename($section)) "\n")\
#print($name "  " $icon "  " $action " " $parameters "\n")
#
#supported
#   x11=  menu_entry( title(), icon(), quote("exec"), x11())
#   text= menu_entry( title(), icon(), quote("exec"), text())
#   wm= menu_entry( title(), icon(), quote("exec"), wm())
#endsupported
#
#submenutitle= menu_entry( title(), icon(), quote("menu"), menu())
#
## this stuff is unused, but has proven to be a pain if not defined
#preoutput=""
#postoutput=""
#startmenu=""
#endmenu=""
#

#file:enlightenment.preinst
##!/bin/sh
#
#themedir='/usr/share/enlightenment/themes'
#
#if [ -L "$themedir/BrushedMetal-Tigert" ]; then
#  echo "WARNING: removing $themedir/BrushedMetal-Tigert symlink"
#  rm -v "$themedir/BrushedMetal-Tigert"
#fi
#
#if [ -d "$theme_dir/BrushedMetal" ]; then 
#  echo "WARNING: It is recommended to remove them \"$themedir/BrushedMetal\" dir."
#fi
#
#if [ -d "$themedir/bmt" ]; then
#  echo "WARNING: It is recommended to remove the \"$themedir/bmt\" dir."
#fi
#
##DEBHELPER#
#
#exit 0

#file:enlightenment.prerm
##!/bin/sh
#set -e
#
#menu_dir='/etc/enlightenment/menus'
#find "${menu_dir}" -type f -name 'Debian_*.menu' -print0 | xargs -0r rm -f
#
##DEBHELPER#
#
#exit 0

