#!/bin/bash
#
# Copyright 2006 Marios Andreopoulos - mrsaccess
# Distributed under the terms of the GNU General Public License v2
# 
#
#
# musearch is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY. YOU USE AT YOUR OWN RISK. THE AUTHOR
# WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY
# OTHER  KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.
# See the GNU General Public License for more details.
#
# Gentoo is copyright of Gentoo Foundation
# gentoo-portage.com is a unofficial portage website of
# Mike Valstar [ http://mikevalstar.com ]
#
# Version notice:
#  There aren't any known bugs in this script.
#  The beta (b) in the version is kept just in case. ;)
#
#
# musearch 1.03b is tested against these versions of eix:
#     0.6.4  |  0.7.9  |  0.8.5
#
# Changelog 1.03b from 1.02b:
#   - Fixed a small bug concerning current testing versions of eix (0.8.x).
#     Due to the bug some annoying messages were printed  between the
#     search results.
# Changelog 1.02b from 1.01b:
#   - Added support for eix 0.7.x. If you have an earlier version of
#     eix you can still use musearch 1.01b.
#   - Temp files based on username.
##
prog_name="musearch"
prog_version="1.03b"
release_name="Gin and Tonic - Available throughout the Galaxy"
fullprog="${prog_name}-${prog_version}"

PAGE=/tmp/`whoami`_saved_page
TMP_FILE1=/tmp/`whoami`_musearch1
TMP_FILE2=/tmp/`whoami`_musearch2
USE_BACKEND=0     #0 for emerge, 1 for eix, 2 for esearch, 3 for none
SEARCH_DESCRIPTION=''
SEARCH_TERM=''
SEARCH_OK=0
MAX_PACKAGES_BEFORE_ASK=5
MAX_PACKAGES=20

#Setting color codes
GR=$'\x1b[1;32m'
RED=$'\x1b[31m'
REDB=$'\x1b[31;1m'
TQ=$'\x1b[36;1m'
TQB=$'\x1b[36;1;5m'
YL=$'\x1b[33;1m'
NC=$'\x1b[0m'

if [ "$COLUMNS" = "" ]; then
  let COLUMNS=80
fi
let cut_columns=$COLUMNS-4

rm -f $PAGE $TMP_FILE1 $TMP_FILE2

version_info()
{
cat <<END
 ${prog_name}, version ${prog_version}
 codename ${TQB}$release_name${NC}
 Copyright (C) 2006 Marios Andreopoulos - mrsaccess
 
 This is free software.  You may redistribute copies of it under the terms of
 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
 There is NO WARRANTY, to the extent permitted by law.
 
 
 Gentoo is copyright of Gentoo Foundation.
 gentoo-portage.com is a unofficial portage website of Mike Valstar.
END
}

help_info()
{
cat <<END
 Usage: ${prog_name}  [OPTIONS]  SEARCH_TERM
  Search for packages including the SEARCH_TERM in their name and explain their
  USE flags.
  
 Arguments
   -x, --eix         Use the eix backend for quicker search.
   -s, --esearch     Use the esearch backend for quicker search.
   -S, --searchdesc  Search also through descriptions. Please use with sense.
                     Too many results will produce too much traffic to the
                     gentoo-portage.com website.
   -d, --direct      This will work if you give the exact app-category/package
                     name as the SEARCH_TERM. It will not search but it will
                     give you the USE flags of the exact package. It is usefull
                     in case there are many matches, ie gnome-base/gnome.
   -h, --help        This info.
   -v, --version     Version Information
   
  Once more: please respect the bandwidth of gentoo-portage.com
END
}


acquire_flags()
{
rm -f $PAGE
wget -q -O $PAGE http://www.gentoo-portage.com/$1/USE#ptabs
if (( $? == 1 )); then
  echo -e " Sorry, couldn't find ${REDB}$1${NC} on gentoo-portage.com database.\n"
else
  cat $PAGE | grep -A 3 '<div class="useflag">' > $TMP_FILE1
  echo -e ${YL}$1$NC
  if (( `wc -l $TMP_FILE1|sed -e 's/ .*//'` == 4 )); then 
    echo -e " ${REDB}No use flags?${NC}"
  else
    while read LOG_LINE; do
      echo $LOG_LINE | grep '<div><b>' >/dev/null 2>&1
      if (( $? == 0 )); then
        mr=`echo $LOG_LINE | sed -e 's/<div><b>//'|sed -e 's/<\/b><\/div>//'`
        echo -e " $TQ* $GR$mr$NC"
      fi
      echo $LOG_LINE | grep -e '<div>[A-Za-z0-9]' >/dev/null 2>&1
      if (( $? == 0 )); then
        mr2=`echo $LOG_LINE | sed -e 's/.*<i>//'|sed -e 's/<\/i>.*//'`
        if [ -z "$mr2" ]; then
          echo -e "    ${RED}Undocumented${NC}"
        else
          echo $mr2|
          while read -n $cut_columns line; do
            echo "    $line"
          done
        fi
      fi
    done<$TMP_FILE1
  fi
  echo
fi
}

check_final_argument()
{
if [ "$SEARCH_TERM" == "" ]; then
  SEARCH_TERM=$1
else
  echo -e "Sorry, too many and/or invalid arguments!\n"
  exit 1
fi
}

check_results()
{
if (( $1 == 0 )); then echo -e " Sorry, your search yield no results.\n"; exit; fi
if (( $1 > $MAX_PACKAGES_BEFORE_ASK && $1 < $MAX_PACKAGES)); then
cat <<END
 Your search results in $1 packages. These means too much traffic towards
 gentoo-portage.com .
 
 If you are sure about this ${GR}press y to continue${NC} or ${REDB}any other key to cancel${NC}.

END
  read -s -n1 AGREE
  if [ "$AGREE" == "y" ]; then SEARCH_OK=1; else exit; fi
fi
if (( $1 > $MAX_PACKAGES )); then
  cat <<END
 Your search results in $1 packages. Sorry, but I'm not allowed to investigate
 more than $MAX_PACKAGES packages at at a time.
 
END
  exit 1
fi
}

for ((i=$OPTIND; i<=$#; ++i)); do
  case ${!i} in
    -x | --eix        ) if (( $USE_BACKEND == 0 )); then USE_BACKEND=1; else check_final_argument; fi;;
    -s | --esearch    ) if (( $USE_BACKEND == 0 )); then USE_BACKEND=2; else check_final_argument; fi;;
    -S | --searchdesc ) SEARCH_DESCRIPTION=-S;;
    -d | --direct     ) if (( $USE_BACKEND == 0 )); then USE_BACKEND=3; else check_final_argument; fi;;
    -v | --version    ) version_info; exit;;
    -h | --help       ) help_info; exit;;
    * ) OPTIND=$i; check_final_argument ${!i};;
  esac
done

case $USE_BACKEND in
  0 )
      emerge -s $SEARCH_DESCRIPTION $SEARCH_TERM | grep "*" | grep "/"|sed  -e 's/* //'|sed -e 's/ \[.*//' > $TMP_FILE2
      mr=`wc -l $TMP_FILE2|sed -e 's/ \/.*//'`
      check_results $mr
      echo -e "Searching...\n"
      for i in `cat $TMP_FILE2`; do acquire_flags $i; done
    ;;
  1 )
      which eix >/dev/null 2>&1; if (( $? == 1 )); then echo -e " Sorry, eix doesn't seem to be installed.\n"; exit 1; fi
      eix -sn $SEARCH_DESCRIPTION $SEARCH_TERM|grep  -E "^\[[INUD]\]|^\*"|sed -e 's/\*/\[N\]/'|sed -e 's/\[.*\] \(.*\)/\1/'|sed -e 's/ .*//' > $TMP_FILE2
      mr=`wc -l $TMP_FILE2|sed -e 's/ \/.*//'`
      check_results $mr
      echo -e "Searching...\n"
      for i in `cat $TMP_FILE2`; do acquire_flags $i; done
    ;;
  2 )
      which esearch >/dev/null 2>&1; if (( $? == 1 )); then echo -e " Sorry, esearch doesn't seem to be installed.\n"; exit 1; fi
      esearch -o "%p\n" $SEARCH_DESCRIPTION $SEARCH_TERM|grep "/" > $TMP_FILE2
      mr=`wc -l $TMP_FILE2|sed -e 's/ \/.*//'`
      check_results $mr
      echo -e "Searching...\n"
      for i in `cat $TMP_FILE2`; do acquire_flags $i; done
    ;;
  3 ) acquire_flags $SEARCH_TERM;;
esac

#rm -f $PAGE $TMP_FILE1 $TMP_FILE2
