#!/bin/bash
#
#  gbd
#
#  Copyright (C) 2008 Steven J Stover <stump842@gmail.com>
#
#  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.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
showhelp() {
   cat << EOF
Usage: $(basename "$0") [OPTION]...
Uses genbasedir to update a local repository

  -n,  --noupdate   Don't update package list
  -h,  --help       Display this help and exit
EOF
   exit 0
}

#############################################
##          Execution starts here          ##
#############################################

myname=$(basename "$0")

while [ $# -gt 0 ]
do case "$1" in
      -h|--help)     dohelp=1; shift;;
      -n|--noupdate) noup=1; shift;;
      *)             inval=$1; dohelp=1; shift;;
   esac
done


if [ -z "$noup" ] && ! ps|grep $PPID|grep -q $(basename $SHELL); then
	e="Run $myname from a console please."
	which kdialog && exec kdialog --title $myname --caption "" --error "$e"
	which zenity && exec zenity --title $myname --error --text="$e"
	which konsole && exec konsole -T $myname --caption "" --noclose -e echo "$e"
	which xterm && exec xterm -T $myname -fn *-fixed-*-*-*-20-* -hold -e echo "$e"
	exit 0
fi

[ -n "$inval" ] && echo -e "Invalid option: $inval\n"
[ -n "$dohelp" ] && showhelp

sl=/etc/apt/sources.list
ba="genbasedir --flat --bz2only --progress --topdir "
pa=( $(sed -n -e 's|^rpm  *file:\(.*\)$|\1|p' $sl) )
td=${pa[0]}${pa[1]}
co=${pa[@]:2}
nn=${#pa[@]}
if [ "$nn" -lt 3 ]; then
   case $nn in
      0) echo "Nothing found";;
      1) echo "URI=file:${pa[0]}, missing Distribution & Section(s)";;
      2) echo "URI=file:${pa[0]}, Distribution=${pa[1]}, missing Section(s)";;
   esac
   echo "Check file \"$sl\" for a line like this:"
   echo "rpm file:/home/username/src/apt pclinuxos/2007 i586 noarch"
   exit 1
fi
er=0
for sc in $co; do
   ff=${td}/RPMS.${sc}
   if ! [ -d "$ff" -o -L "$ff" ]; then
      echo "Can't find folder $ff for Section $sc in \"$sl\""
      er=1
   fi
done
if [ "$er" -eq 1 ]; then
   echo "Check path \"$td\" for folders (or links) like \"RPMS.section\""
   exit 2
fi      
if [ -d "${td}/base" ]; then
   echo "rm -f ${td}/base/*"
   rm -f ${td}/base/*
else
   echo "mkdir -p ${td}/base"
   mkdir -p ${td}/base
fi
echo "$ba $td $co"
$ba $td $co

[ -n "$noup" ] && exit 0

echo -ne "\nUpdate apt package list? [y/N] "
read u
case $u in
        y|Y)    echo -ne "\nPlease enter root "
                su -c "apt-get -y update";;
esac
