#!/bin/bash
#
#   deb2dsl  -  Converts one or more Debian packages (*.deb files) into
# a DSL extension (*.dsl file) with prompts to create a
# myDSL Menu item for the newly created package.
#
# Revision: 1
# Date: 02/19/05
# Original Author: cbagger01 from the DSL forums
#
# Rev 1 - Removed leading . from dsl file listings and
#         removed .dsl from mydsl menu filename
#
# This script will grab all *.deb files located in your
# "Home" directory, IE: the /home/username  directory
# It will also grab all *.deb files located in your apt
# cache, IE: /var/cache/apt/archives
#
# Before running this script, you need to actually install
# all of your Debian packages using 'dpkg -i' or
# 'apt-get install' or a package manager like Synaptic.
# Do NOT delete your leftover *.deb files or purge your
# apt-cache until after you have finished running this
# script.
#
# Disclaimer:
# This script is just a file repackaging program that
# can be used for simple Debian packages.  It will not
# perform post-extraction configuration that is done
# by more sophisticated Debian packages.  It also cannot
# be used to upgrade packages from older versions
# that are part of the DSL livecd base installation.

. /etc/init.d/dsl-functions

# This script will not work if it is run under the 'root' effective
# user ID which is 0
if [ "$EUID" -eq "0" ]; then
 echo "Do not use 'root' access to run this script. Aborting..."
 exit 1
fi

# Start things out from the home directory so we won't get confused
cd $HOME

# Clean up any leftover temp files if needed
rm -rf /tmp/deb2dsl_files.tmp
rm -rf /tmp/deb2dsl_prompt.tmp

# Get user input before we start processing
echo "${CYAN}Enter the full name for your DSL extension."
echo -n "${WHITE}Example: ${YELLOW}rox.dsl${NORMAL} "
read package_name
if [ -z "$package_name" ]; then
  echo "No package name entered."
  exit 1
fi

echo
echo "${CYAN}Enter the MyDSL menu name of your extension."
echo -n "${WHITE}Example: ${YELLOW}Rox Filer${NORMAL} "
read menu_name
if [ -n "$menu_name" ]; then
   echo -n "${CYAN}Is this an X applicantion? (y/..): ${NORMAL}"
   read xapp
fi

echo
echo "${CYAN}Enter the full path executable name."
echo -n "${WHITE}Example: ${YELLOW}/usr/bin/rox${NORMAL} "
read program_path
if [ -z "$program_path" ]; then
  echo "No program path entered."
  exit 1
fi

# Find all Debian packages in your home directory and grab the list of
# files that are contained inside each package.  Make sure that directory
# names are not grabbed.  Only file names will be added to the list.
# Remove leading . character from the filename list.
for i in $( ls $HOME/*.deb 2>/dev/null ); do
  dpkg -c  $i | awk '{ print substr($6,2) }' \
  | grep -v "\/$" >> /tmp/deb2dsl_files.tmp
done

# Find all Debian packages in your apt cache directory and grab the list
# of files that are contained inside each package.  Make sure that
# directory names are not grabbed.  Only file names will be added to the
# list.
# Remove leading . character from the filename list.
for i in $( ls /var/cache/apt/archives/*.deb 2>/dev/null ); do
  dpkg -c  $i | awk '{ print substr($6,2) }' \
  | grep -v "\/$" >> /tmp/deb2dsl_files.tmp
done

# Remove /usr/share/doc
sed -i '/\/usr\/share\/doc/d' /tmp/deb2dsl_files.tmp

# Remove /usr/share/man
sed -i '/\/usr\/share\/man/d' /tmp/deb2dsl_files.tmp


if [ -n "$menu_name" ]; then
  # Create the mydsl menu directory
  mkdir /tmp/mydsl.menu

  # Strip the .dsl characters from the end of the mydsl menu filename
  menu_filename=`echo $package_name | awk '{ print substr($0,1,length($0)-4) }'`

  if [ "$xapp" != 'y' ]; then
     program_path="aterm -e $program_path"
  fi

  # Create the new mydsl menu item file
  echo "[exec] ("$menu_name") {"$program_path"}" > /tmp/mydsl.menu/$menu_filename

  # Add the mydsl menu file to the list of files that will be
  # included into the new new dsl package
  echo "/tmp/mydsl.menu/"$menu_filename >> /tmp/deb2dsl_files.tmp
fi

# Create the new DSL package
tar -zcvf $package_name -T /tmp/deb2dsl_files.tmp -C /
rm /tmp/deb2dsl_files.tmp
rm /tmp/mydsl.menu/$menu_filename
rm -r /tmp/mydsl.menu
echo "${YELLOW}$package_name ${CYAN}Successfully created.${NORMAL}"
exit 0
