#!/bin/bash
# Apt-packages-list-update-1s.sh
 
VER="1.0"
GREEN="\033[1;32m"
RED="\033[0;31m"
YELLOW="\033[1;33m"
ENDCOLOR="\033[0m"
 
tproc=`basename $0`
echo -e $GREEN"$tproc version $VER"$ENDCOLOR
echo ""

usage()
{
    tproc=`basename $0`
    echo -e $YELLOW"usage:"$ENDCOLOR
    echo -e $GREEN" bash $tproc"$ENDCOLOR
}

testargs()
{
    if [ "+$1" != "+" ]
    then
	usage
	exit 0
    fi
}

testcomponent()
{
    tnocomp=""
    tcomp="/usr/bin/apt-ftparchive"
    tdeb="apt-utils_*.deb"
    if [ ! -f "$tcomp" ]
    then
	tnocomp="$tnocomp $tcomp($tdeb)"
    fi
    tcomp="/bin/grep"
    tdeb="grep_*.deb"
    if [ ! -f "$tcomp" ]
    then
	tnocomp="$tnocomp $tcomp($tdeb)"
    fi
    if [ "+$tnocomp" != "+" ]
    then
	echo -e $RED"Not found $tnocomp !"$ENDCOLOR
	echo ""
	exit 0
    fi
}

testdeb()
{
    echo ""
    echo -e $YELLOW"Test apt packages..."$ENDCOLOR
    echo ""
    tpwd=`pwd`
    tpstr1="deb file:$tpwd ./"
    tpstr2="deb file:$tpwd/ ./"
    tgrep1=`grep "$tpstr1" /etc/apt/sources.list`
    tgrep2=`grep "$tpstr2" /etc/apt/sources.list`
    if [ "+$tgrep1" != "+$tpstr1" -a "+$tgrep2" != "+$tpstr2" ]
    then
	echo -e $RED" ! WARNING ! "$ENDCOLOR
	echo "Add /etc/apt/sources.list:"
	echo -e $GREEN"$tpstr1"$ENDCOLOR
	echo "or:"
	echo -e $GREEN"$tpstr2"$ENDCOLOR
    else
	echo "deb OK."
    fi
    echo ""
}

main()
{
    echo ""
    echo -e $YELLOW"Packages list update..."$ENDCOLOR
    echo ""
    echo "Add source packages info..."
    apt-ftparchive sources . | gzip >Sources.gz
    echo "Add binary packages info..."
    apt-ftparchive packages . | gzip >Packages.gz
    echo "Add contents packages info..."
    apt-ftparchive contents . | gzip >Contents-i386.gz
    echo "Add release packages info..."
    apt-ftparchive release . >Release
}

testargs $@
testcomponent
main
testdeb
#end
