#!/usr/bin/sh

#// repacman.sh
#// (c) 2008 - gyo <gyo_at_archlinux_dot_fr>
#// license : GPL
#// description : utility to rebuild installed package from information contained in pacman's database

_prg_name="${0##*/}"
_prg_version='2.0'

# Getting some parameters from pacman.conf
eval $(sed -re '/^[[:space:]]#/d' -e '/(DBPath|CacheDir)/s:([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+):\1="\2":p;d' /etc/pacman.conf)

PACMAN_DB_DIR=${DBPath:-'/var/lib/pacman/'}
PACMAN_CACHE_DIR=${CacheDir:-'/var/cache/pacman/pkg/'}
FILE_PREP_DIR="/tmp/${_prg_name}_$$"
CURRENT_DIR="`pwd`"

DEBUG=0

#------ function ------#

function f_generate_pkginfo {
[ -e $package_dir/desc ] || _f_missed_file desc 3

function file_stream {
cat $package_dir/desc
[ -e $package_dir/depends ] && cat $package_dir/depends
sed -rne '/%BACKUP%/,$s/^([^[:space:]]+).*/\1/p' $package_dir/files
}
file_stream | while read; do
  case $REPLY in
    %NAME%)	  pkginfo_var='pkgname';;
    %VERSION%)	  pkginfo_var='pkgver';;
    %DESC%)	  pkginfo_var='pkgdesc';;
    %GROUPS%)	  pkginfo_var='group';;
    %URL%)	  pkginfo_var='url';;
    %LICENSE%)	  pkginfo_var='license';;
    %ARCH%)	  pkginfo_var='arch';;
    %BUILDDATE%)  pkginfo_var='builddate';;
    %PACKAGER%)	  pkginfo_var='packager';;
    %SIZE%)	  pkginfo_var='size';;
    %REPLACES%)	  pkginfo_var='replaces';;
    %CONFLICTS%)  pkginfo_var='conflict';;
    %PROVIDES%)	  pkginfo_var='provides';;
    %BACKUP%)	  pkginfo_var='backup';;
    %DEPENDS%)	  pkginfo_var='depend';;
    %OPTDEPENDS%) pkginfo_var='optdepend';;
    %MAKEPKGOPT%) pkginfo_var='makepkgopt';;
    %*%) unset pkginfo_var;;
    *)
      if [ -n "$REPLY" -a -n "$pkginfo_var" ]; then
        echo "$pkginfo_var = $REPLY" >> .PKGINFO
      fi
    ;;
  esac
done
}

function f_create_tar {
  mkdir $FILE_PREP_DIR
  cd $FILE_PREP_DIR
  [ -e $package_dir/files ] || _f_missed_file files 2

  sed -rne '/%FILES%/,/%BACKUP%/{/^(%FILES%|%BACKUP%| *)$/d;s:^:/:p}' $package_dir/files > .FILELIST

  if [ -e $package_dir/install ]; then
    cd $FILE_PREP_DIR
    cp $package_dir/install .INSTALL
  fi

  if [ -e $package_dir/changelog ]; then
    cd $FILE_PREP_DIR
    cp $package_dir/changelog .CHANGELOG
  fi

  f_generate_pkginfo

  echo
  bsdtar cJvf "$CURRENT_DIR"/$pkgname_ver-$arch.pkg.tar.xz --exclude=. --exclude=.. --exclude=.FILELIST -n -T .FILELIST .*
}

function _f_missed_file {
  echo "! File \"$1\" is missing" >&2
  exit $2
}

function f_help {
  cat << EOF
[ $_prg_name $_prg_version ]
  $_prg_name package_name (ex: $_prg_name hal)
  -h : show this help
EOF
}

#------ end function ------#

#------ MAIN ------#

if [ "`id -u`" != 0 -a `type -p fakeroot` ]; then
  fakeroot -- $0 $@
  exit $?
fi

if ((!$#)); then
  echo "Error: not enough arguments !"
  echo
  f_help
  exit 1
fi

if [ "$1" = '-h' ]; then f_help; exit 0; fi

pkgname="$1"

package_dir=`echo "$PACMAN_DB_DIR"/local/$pkgname-[0-9]*`

if [ ! -d $package_dir ]; then
  echo "Package \"$pkgname\" does not exist in the database !"
  exit 1
fi

pkgname_ver=${package_dir##*/}
arch=`pacman -Qi $pkgname | grep Architecture | gawk '{ print $3 }'`

if [ -f "$PACMAN_CACHE_DIR"/$pkgname_ver-$arch.pkg.tar.xz ]; then
  echo "Package \"$pkgname_ver-$arch.pkg.tar.xz\" already exists in cache repository : "$PACMAN_CACHE_DIR""

read -n 1 -p 'Continue (y/N) ? '
case $REPLY in
  y|Y) ;;
  *) echo -e '\nBye.'; exit 0;;
esac

  echo
fi

echo "- Full package name : $pkgname_ver"
if (($DEBUG)); then
echo $package_dir
fi

read -n 1 -p 'Continue (Y/n) ? '
case $REPLY in
   n|N) echo -e '\nBye.'; exit 0;;
   *) ;;
 esac


pkgname_ver="${package_dir##*/}"

f_create_tar

if ((!$DEBUG)); then
rm -fr $FILE_PREP_DIR
fi

cd "$CURRENT_DIR"

echo "--> "$CURRENT_DIR"/$pkgname_ver-$arch.pkg.tar.xz created !"

exit 0

#------ END MAIN ------#
