#!/bin/bash

# A simple script that adds the source file list to a dsc file.
#
# (C) 2012 by <joerg.steffens@dass-it.de>
#
# This program is free software; you can redistribute it and/or  
# modify it under the terms of the GNU General Public License  
# as published by the Free Software Foundation; either version 2  
# of the License, or (at your option) any later version.  
# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.  


# defaults
MYVERSION=""
FILES=""

while test $# -gt 0; do
  case $1 in
    *-file)
      FILES="$FILES $2"
      shift
    ;;
    *-outdir)
      MYOUTDIR="$2"
      shift
    ;;
    *)
      echo Unknown parameter $1.
      echo 'Usage: dsc_filelist --file $FILE --outdir $OUT'
      exit 1
    ;;
  esac
  shift
done

if [ -z "$FILES" ]; then
  echo "ERROR: no inputs files are given via --file parameter!"
  exit 1
fi

filesize()
{
    stat  --format='%s' $*
}

md5()
{
    md5sum $* | cut -f 1 -d ' '
}

export LANG=C

FILES_DSC="*.dsc"

# update the file list in the description file
for FILE_DSC in $FILES_DSC; do
    [ -e $FILE_DSC ] || continue
    printf "$0: modifing file $FILE_DSC, adding "
    grep -q "^Files:" $FILE_DSC || echo "Files: " >> $FILE_DSC
    for filename in $FILES; do
        path=`ls -1 "$filename 2>/dev/null" || eval ls -1 "_service:*:$filename 2>/dev/null"`
        [ -e $path ] || continue
        printf "${path##*:} (${path})"
        printf ' %s %s %s\n' `md5 $path` `filesize $path` ${path##*:} >> $FILE_DSC
    done
    printf "\n"
done

exit 0
