#! /bin/bash

# program : apt-list
# author: ankyo <http://ankyo.blog.so-net.ne.jp/>
# date 2012/12/06

ver=0.1.0

# Set default arg names.
search_name='*'
category_name=
subname=
list_dir="/var/lib/apt/lists"
repository=
package_name=

# Script control variables.
match_pattern=''
showlist=false
detail=false

# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: $0 [OPTION]
Utility tool for debian package list.

  -l, --list         print repository list.
  -c, --category     category of repository or PPA user name.
                     (e.g. main,security,universe...etc.)
  -s, --sub          subname of repository or PPA.
                     (e.g. binary-i386, source, i18n_Translation...etc.)
  -n, --name         package name. 
  -d, --detail       print detail of package information.
  -w, --whole        only matching whole words for package name.
  -h, --help         print this message and exit
  -v, --version      print the version information and exit

EOF
}

# Check the arguments.
while [ $1 ]; do
    case "$1" in
    -h | --help)
        usage
        exit 0 ;;
    -v | --version)
        echo "$0  ${ver}"
        exit 0 ;;
    -l | --list)
        showlist=true
        ;;
    -d | --detail)
        detail=true
        ;;
    -n)
        shift
        search_name=$1
        ;;
    --name=*)
        search_name=`echo "$1" | sed 's/--name=//'`
        ;;
    -c)
        shift
        category_name=$1
	;;
    --category=*)
        category_name=`echo "$1" | sed 's/--category=//'`
	;;
    -s)
        shift
        subname=$1
	;;
    --sub=*)
        subname=`echo "$1" | sed 's/--sub=//'`
	;;
    -w | --whole)
        match_pattern='$'
	;;
    -*)
        echo "Unrecognized option \`$1'" 1>&2
        usage
        exit 1
        ;;
    esac
    shift
done

# pre-processing
search_file="*Packages"
if [ "x$subname" != "x" ]; then
    search_file="*$subname$search_file"
fi
if [ "x$category_name" != "x" ]; then
    search_file="*$category_name$search_file"
fi

search_word="Packages"
if [ "x$subname" != "x" ]; then
    search_word="${subname}.*${search_word}"
fi
if [ "x$category_name" != "x" ]; then
    search_word=$category_name'.*'$search_word
fi



# main routine
case $showlist in
    true )
	ls $list_dir | grep --color=never "$search_word"
	;;
    false )
	case $detail in
	    true )
		temp_result=`grep "Package: $search_name$match_pattern" \\
		$list_dir/$search_file | sed -e "s/Package: //g" -e "s,^/var.*lists/,,"`
		for i in $temp_result; do
		    repository=`echo $i | sed -e "s/:.*$//"`
		    package_name=`echo $i | sed -e "s/^[^:]*://"`

		    echo "Repository: $repository"

		    cat $list_dir/$repository | awk "
			BEGIN {disp = 0}
			/Package: $package_name$/{disp=1}
			/^$/{disp=0}
			{if (disp == 1){print}}
			"
		    echo ""
		done
		;;
	    false )
		if [ x"$search_name" == x"*" ]; then
		    search_name='.*'
		else
		    if [ x"$match_pattern" == x"$" ]; then
			search_name='^'"$search_name"'$'
		    fi
		fi
		awk '$1 == "Package:" && $2 ~ /'"$search_name"'/ { if (a[$2]++ == 0) print $2; }' $list_dir/$search_file
		;;
	esac
	;;
esac