#!/bin/bash # whatthefuck - smart wrapper around wtf # # Copyright (c) 2009, Devendra Gera # This software is released under the terms of the GNU General Public License # version 2, a copy of which may be obtained from: # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt # This software comes with NO WARRANTY, either explicit or implied, to the # extent permissible by law. # # Do not trust this code to do any useful thing. It will eat you and destroy # all your data if not used properly. You have ben warned. HTML2TEXT=html2text WTF=`which wtf` BROWSER=${BROWSER:=gnome-www-browser} Term=$1 # eat the "is" if [ x"$Term" == x"is" ]; then Term=$2 fi if [ x"$Term" == x"" ]; then echo "wtf do you want?" fi function fetch_from_wikipedia() { local Term=$1 local Filename=$2 if [ x"$Term" == x"" ]; then return -1 fi if [ x"$Filename" == x"" ]; then return -1 fi wget -q -O - "http://en.wikipedia.org/wiki/$Term" | $HTML2TEXT -nobs | perl -ne 'print unless /^$/../^$/' > $Filename local file_lines=$(wc -l $Tempfile | awk '{print $1}') return $file_lines } Out=$($WTF $Term 2>/dev/null) if [ $? -ne 0 ]; then Out=$($WTF -t comp $Term 2>/dev/null) if [ $? -ne 0 ]; then trap 'rm -f $Tempfile' 0 2 3 6 9 11 13 15 17 Tempfile=`tempfile` || exit 1 fetch_from_wikipedia $Term $Tempfile file_lines=$? if [ $file_lines -le 0 ]; then # try the uppercase version of the term Term2=`tr 'a-z' 'A-Z' <<<$Term` fetch_from_wikipedia $Term2 $Tempfile file_lines=$? if [ $file_lines -le 0 ]; then rm -f $Tempfile exec ${BROWSER} "http://www.google.com/search?q=$Term" fi fi fi fi if [ x"$Tempfile" == x"" ]; then # it's a wtf output echo $Out exit 0 else screen_lines=$(stty size | awk '{print $1}') if [ $file_lines -gt $screen_lines ]; then LESSCHARSET="utf-8" pager $Tempfile else cat $Tempfile fi fi exit 0