#!/bin/bash
# Author: Weiser Robert
# Description: This bash-script will generate simple mac packages or
#              meta-packages
# Last Changed: 20.08.2005, 11:54

# --------- CONSTANT DECLARATIONS --------- #

# EXIT  CODES
EXIT=0
E_NO_ARGS=2
E_WRONG_OPTION=3
E_NO_DIR=4
E_NO_PACKAGE_FOLDER=5

# GLOBAL VARIABLES
pax_ext=".pax"
bom_ext=".bom"
simple_package_ext=".pkg"
meta_package_ext=".mpkg"
package_folder="Packages"

# TOGGLES
simple=

# OTHER
NO_ARGS=0
ONE_ARG=1

# --------- FUNCTIONS --------- #

function main ()
{
  # initialize
  init
  
  # process command line args
  process_command_line_args $*
}

function init ()
# initialize
{
  cd `dirname $0`
}

function process_command_line_args ()
{
  # check if enough arguments
  if (( $# == "$NO_ARGS" ))
  then
    echo "Error: You must specify more arguments."
    show_usage
    exit $E_NO_ARGS
  fi
  
  # get options
  while getopts ":smh" Option
  do
    case $Option in
      s ) simple=1;;

      m ) create_meta_package;
          exit $EXIT;;
          
      h ) show_usage;
          exit $EXIT;;

      * ) echo "Option not supported";
          show_usage;
	      exit $E_WRONG_OPTION;;
    esac
  done

  shift $(($OPTIND - 1))
  
  if (( simple == 1 ))
  then
    # check if more then the required numer of arguments
    # has been given
    if (( $# < "$ONE_ARG" ))
    then
      echo "No directory specified."
      exit $E_WRONG_ARGS
    fi
    
    # check if a valid directory has been provided
    if [ -d "$1" ]
    then
      create_simple_package $1
    else
      echo "Please provide a valid directory ..."
      show_usage
      exit $E_NO_DIR
    fi
  else
    echo "Wrong option specified ..."
    show_usage
    exit $E_WRONG_OPTION
  fi
}

function show_usage ()
# display usage of the script
{
  echo "This script must be placed in the folder of which"
  echo "you want to create the package ..."
  echo "Usage: `basename $0` [option]"
  echo "Options:"
  echo "  -h		Display this help."
  echo "  -s <dir>	Create a simple package."
  echo "  -m		Create a meta-package."
}

function create_simple_package ()
# create a simple package
{
  local dir=
  local u_dir=
  local package_name=
  
  # clean up hidden stuff
  clean_hidden
  
  # find out package name
  package_name=`ls *.info | sed -n "s/.info//p"`
  
  # pack all files
  pax -wf $package_name$pax_ext -x pax $1
  
  # compress the package
  gzip -f $package_name$pax_ext
  
  # create a bill of material
  mkbom $1 $package_name$bom_ext
  
  # clean up dir and script
  rm -rf $1
  rm -rf $0
  
  # add correct package extension
  dir=`pwd`
  cd ..
  u_dir=`pwd`
  
  mv "$dir" "$u_dir/$package_name$simple_package_ext"
}

function create_meta_package ()
# create a meta-package
{
  local dir=
  local u_dir=
  local package_name=
  local list_file=
  
  # clean up hidden stuff
  clean_hidden
  
  # find all packages and store em in the correct list
  list_file=`ls *.info | sed -n "s/.info/.list/p"`
  
  # find out package name
  package_name=`ls *.info | sed -n "s/.info//p"`
  
  if [ ! -d $package_folder ]
  then
    exit $E_NO_PACKAGE_FOLDER
  fi
  
  cd $package_folder
  command="ls"
  
  for file in $(eval $command)
  do
    echo "$file" >> ../$list_file
  done
  
  cd ..
  
  # clean up script
  rm -rf $0
  
  # add correct package extension
  dir=`pwd`
  cd ..
  u_dir=`pwd`
  
  mv "$dir" "$u_dir/$package_name$meta_package_ext"
}

function clean_hidden ()
# clean hidden files in current working directory
{
  rm -rf .DS_Store
}

# --------- ENTRY POINT --------- #

main $*
