#!/bin/bash

usage() {
  >&2 echo 'Verwendung:'
  >&2 echo "${ich}"' [-d|--download URL] [-a|--alle] [-s|--suche wonach] [-i|--intensiv] [-x|--exakt] [-l|--leise]'
  exit 1
}

verpackung() {
  typ="$(file -bizL "$1")"
  case "${typ}" in
    "application/x-tar; charset=binary")
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-xz; charset=binary")
      echo "-J"
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-bzip2; charset=binary")
      echo "-j"
    ;;
    "application/x-tar; charset=binary compressed-encoding=application/x-gzip; charset=binary" | \
    "application/octet-stream; charset=binary compressed-encoding=application/x-gzip; charset=binary")
      echo "-z"
    ;;
    "ERROR: (null)" | \
    "inode/x-empty; charset=binary")
      return 1
    ;;
    *)
      ${leise} || >&2 echo "FEHLER: Unbekannter Verpackungstyp '${typ}'"
      return 1
    ;;
  esac
  return 0
}

ich="$(readlink -f "$0")"
pfad="$(dirname "${ich}")/.checkAllRepos"
[ ! -d "${pfad}" ] && mkdir -p "${pfad}"

eval set -- "$(
  getopt -o ad:ils:x \
    --long alle \
    --long download: \
    --long exakt \
    --long intensiv \
    --long leise \
    --long suche: \
    -n "$(basename "$0")" \
    -- "$@" \
    || echo "usage"
)"
args=("$@")

unset downloads suchen
alle=false
exakt=false
intensiv=false
leise=false
flag_l=''

while true
do
  case "$1" in
    -a|--alle)
      alle=true
    ;;
    -d|--download)
      shift
      if [ -n "$1" ]
      then
        downloads[${#downloads[@]}]="$1"
      fi
    ;;
    -i|--intensiv)
      intensiv=true
    ;;
    -l|--leise)
      leise=true
      flag_l="-l"
    ;;
    -s|--suche)
      shift
      if [ -n "$1" ]
      then
        suchen[${#suchen[@]}]="$1"
      fi
    ;;
    -x|--exakt)
      exakt=true
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo "FEHLER: Kenne Option '$1' doch nicht"
      exit 1
    ;;
  esac
  shift
done

if [ $# -ne 0 ]
then
  echo 'FEHLER: Zu viele Argumente'
  usage
fi

if ${alle}
then
  curl "https://wiki.archlinux.org/index.php/Unofficial_user_repositories" 2> /dev/null | \
    sed 's|#.*$||' | \
    sed '
      :begin;
        $!N;
        s@^\(<pre>.*\)\n\([^<]\+\)$@\1 \2@;
      tbegin;
      P;
      D
    ' | \
    grep "^<pre>" | \
    sed "s|^<pre> \[\([^]]*\)].*\sServer = \(\S\+\)\(\s.*\)\?\$|\1 \2/\1.db|" | \
    sed "s|^\(\S\+\)\s\+\(.*\)\$repo|\1 \2\1|; s|\$arch|x86_64|" | \
    sed "s|^\S\+\s\+||" | \
    parallel -j0 "${ich} ${flag_l} -d {}"
fi

for ((i=0;i<${#downloads[@]};i++))
do
  err=false
  ziel="${pfad}/${downloads[${i}]##*/}"
  wget -O "${ziel}" "${downloads[${i}]}" 2> /dev/null || err=true
  if ! ${err}
  then
    if ! verp=$(verpackung "${ziel}") || ! tar ${verp} -tf "${ziel}" &> /dev/null
    then
      err=true
    fi
  fi
  if ${err}
  then
    rm -f "${ziel}"
  else
    ${leise} || echo "${ziel##*/} erfolgreich heruntergeladen"
  fi
done

for ((i=0;i<${#suchen[@]};i++))
do
  suchName="${suchen[${i}]}"
  ${exakt} && suchName="^\(${suchName}\)-[^-]\+-[^-]\+\$"
  for db in ${pfad}/*.db
  do
    if tar $(verpackung "${db}") -tf "${db}" 2> /dev/null | \
      grep "/\$" |
      sed "s|/\$||" |
      grep -q "${suchName}"
    then
      echo "'$(basename "${db}" .db)' enthält ein Paket, welches auf '${suchen[${i}]}' passt:"
      tar $(verpackung "${db}") -tf "${db}" | \
        grep "/\$" |
        sed "s|/\$||" |
        grep "${suchName}" | \
        sed "s|^\(.*\)\$|  '\1'|"
    fi
    if ${intensiv}
    then
      for pkg in $(tar $(verpackung "${db}") -tf "${db}" --wildcards "*/desc" 2> /dev/null)
      do
        inhalt="$(
          tar $(verpackung "${db}") -Oxf "${db}" "${pkg}" 2> /dev/null | \
            sed '
              :begin;
                $!N;
                s@^\(%[^ %]*%.*\)\n\(.*\S.*\)$@\1 \2@;
              tbegin;
              P;
              D
            ' | \
          grep "^%DESC%" | \
            sed "s|^%DESC%\s*||"
        )"
        if echo "${inhalt}" | \
          grep -q "${suchen[${i}]}"
        then
          echo "Die Beschreibung von Paket '${pkg%/desc}' aus '$(basename "${db}" .db)' passt auf '${suchen[${i}]}':"
          echo "  ${inhalt}" | \
            grep --color=auto "${suchen[${i}]}"
        fi
      done
    fi
  done
done