#!/bin/bash

# This software is free under GNU standards and provided 'AS IS' without any warranty.

# Requirements: wget, w3m (or any other browser, set the BROWSER variable accordingly).

VERSION="0.4"
BROWSER="w3m"

# Functions

Help()
{
	echo "palm-get v. $VERSION"
	echo "Usage: palm-get -[s|u|l|p|h] <package-name>"
	echo "options:"
	echo "s     Search the palmdb repository."
	echo "u     Update the palmdb repository database."
	echo "l     Show the latest uploaded applications."
	echo "p     Show the most popular applications."
	echo "h     Print this Help."
	echo
	echo "At first run update the database with palm-get -u"
	echo 
	echo "Search example:"
	echo "$> palm-get -s slovoed"
    echo
	echo "Output:"
	echo "slovoed <- package name"
	echo "files/media-books/slovoed: <- files location"
	echo "SlovoEd_Studio_v1.0.zip"
	echo "SlovoEd_V6.05.zip"
	echo "SlovoEd.zip"
	echo
	echo "Download example:"
	echo "$> palm-get slovoed"
	echo
	echo "Latest/Popular apps will be shown in $BROWSER:"
	echo "$> palm-get -l|p"
	exit 1
}

Update()
  {
	  
	echo "Updating local copy of repository..."
	echo
	mkdir ./palmdb.local
	rm -f ./palmdb.local/LastUpdate*
	wget -O ./palmdb.local/files.txt "https://palmdb.net/content/files.txt"
	touch ./palmdb.local/"LastUpdate_$(date +%F)"	
	echo "Done."
	exit 1

  }

Search()
  {

	echo "Searching..."
	grep -i -e "$OPTARG" ./palmdb.local/files.txt | less
	exit 1

  }

Latest()
  {

	mkdir ./latest
	rm -f ./latest/LastUpdate*
	wget -O ./latest/latest.html "https://palmdb.net/latest"
	touch ./latest/"LastUpdate_$(date +%F)"	
	echo "Done."
	$BROWSER ./latest/latest.html
	exit 1

  }

Popular()
  {
	
	mkdir ./popular
	rm -f ./popular/LastUpdate*
	wget -O ./popular/popular.html "https://palmdb.net/popular"
	touch ./popular/"LastUpdate_$(date +%F)"	
	echo "Done."
	$BROWSER ./popular/popular.html
	exit 1

  }

# Main

# If no arguments are given show Help
if [[ $# -eq 0 ]] ; then
	    Help
		    exit 0
fi


# Run function according to command line argument
# s: means that a search term is needed, the : after a letter makes 
# the use of OPTARG possible
# example: getopts 'a:b:cfh:" 

while getopts 'ulphs:' OPTION; do
  case "$OPTION" in
    s)
		Search
      ;;
    u)
		Update
      ;;
    l)
		Latest
      ;;
    p)
		Popular
      ;;
    h)
		Help
      ;;
    ?)
      echo "Script usage: $(basename \$0) [-h] [-u] [-s searchterm]" >&2
      exit 1
      ;;
  esac
done

shift "$(($OPTIND -1))"

# Start Downloading files

clear

echo "Downloading:"
echo "https://palmdb.net/app/$1"
echo
mkdir ./$1
# Download html page locally
$WGET_PATH/wget -P ./$1 "https://palmdb.net/app/$1" -O ./$1/$1.html  

clear

echo "Downloading:"
# Download files
CONTENT=$(grep -i "files" ./palmdb.local/files.txt | grep -i "$1" | rev | cut -c2- | rev)
CONTENT2=$(echo "$CONTENT" | sed 's/files/file/g')
echo "https://lite.palmdb.net/$CONTENT2"
$WGET_PATH/wget -r -l1 -nd -e robots=off -A zip,prc,pdb --no-check-certificate -P ./$1 "https://lite.palmdb.net/$CONTENT2"

# Check if the html page is empty and if so delete the failed package

echo
echo

if [ -s ./$1/$1.html ]
then
	echo "Done."
	$BROWSER ./$1/$1.html
else	
	rm -f -r ./$1
	echo "Package not found."
fi


