#!/bin/bash

. config

DB_ROOT=$FTP_BASE
DB_BASE=$DB_ROOT/dbscripts/db-repo-cleanup.d
DB_REPO=(hyperbola)
DB_TYPE=(${PKGREPOS[@]})
DB_ARCH=(${ARCHES[@]})
PKG_EXT=$PKGEXT
SRC_EXT=$SRCEXT
SIG_EXT=.sig

mkdir -pv $DB_BASE/list
for _arch in ${DB_ARCH[@]}; do
  mkdir -pv $DB_BASE/arch/$_arch
  fakeroot pacman -Syyb $DB_BASE/arch/$_arch \
    --config $DB_BASE/arch/$_arch/pacman.conf
  pacman -Ssb $DB_BASE/arch/$_arch \
    | sed '\|^[ ][ ][ ][ ]|d;s|^.\+/||;s|[ ](.\+)$||;s|[ ]|-|' \
      > $DB_BASE/list/pkgdb_${_arch}_dbl.txt
  sort $DB_BASE/list/pkgdb_${_arch}_dbl.txt \
    >> $DB_BASE/list/pkgdb_${_arch}_dbl.txt.sort
  mv $DB_BASE/list/pkgdb_${_arch}_dbl.txt{.sort,}
done
unset _arch
for _dirs in pkg src; do
  rm $DB_BASE/list/${_dirs}rp_base_dbl.txt || true
  for _repo in ${DB_REPO[@]}; do
    ls -1 $DB_ROOT/pool/$_repo \
      > $DB_BASE/list/pkgrp_${_repo}_dbl.txt
     sed -i "\|^.\+${SIG_EXT}$|d;s|${PKG_EXT/[?]/.}$||" \
       $DB_BASE/list/pkgrp_${_repo}_dbl.txt
    ls -1 $DB_ROOT/sources/$_repo \
      > $DB_BASE/list/srcrp_${_repo}_dbl.txt
    sed -i "\|^.\+${SIG_EXT}$|d;s|$SRC_EXT$||" \
      $DB_BASE/list/srcrp_${_repo}_dbl.txt
    cat $DB_BASE/list/${_dirs}rp_${_repo}_dbl.txt \
      | sed "s|^|${_repo}/|" \
        >> $DB_BASE/list/${_dirs}rp_base_dbl.txt
    sort $DB_BASE/list/${_dirs}rp_base_dbl.txt \
      >> $DB_BASE/list/${_dirs}rp_base_dbl.txt.sort
    mv $DB_BASE/list/${_dirs}rp_base_dbl.txt{.sort,}
  done
  unset _repo
done
unset _dirs
cp $DB_BASE/list/pkgdb_{x86_64,any}_dbl.txt
cp $DB_BASE/list/pkgrp_{base,cleanup}_dbl.txt
for _arch in ${DB_ARCH[@]} any; do
  for _pkgr in $(cat $DB_BASE/list/pkgdb_${_arch}_dbl.txt); do
    sed -i "\|^.\+/$_pkgr-$_arch$|d" \
      $DB_BASE/list/pkgrp_cleanup_dbl.txt
  done
  unset _pkgr
done
rm $DB_BASE/list/pkgdb_any_dbl.txt
unset _arch

mkdir -pv $DB_ROOT/pool/quarantine
echo "Do you want to use the enable interactive mode before every deprecated package removal?"
read _itrt
case $_itrt in
     [Yy][Ee][Ss]|[Yy]) for _pkgqr in $(cat $DB_BASE/list/pkgrp_cleanup_dbl.txt); do
                          echo "Move the $_pkgqr$PKG_EXT to quarantine directory?"
                          read _move
                          case $_move in
                            [Yy][Ee][Ss]|[Yy]|'') mv -iv \
                                                    $DB_ROOT/pool/$_pkgqr$PKG_EXT{,$SIG_EXT} \
                                                    $DB_ROOT/pool/quarantine || true
                                                  ;;
                                   [Nn][Oo]|[Nn]) true
                                                  ;;
                                               *) echo "Input Error"
                                                  exit 1
                                                  ;;
                          esac
                          unset _move
                        done
                        unset _pkgqr
                        ;;
      [Nn][Oo]|[Nn]|'') for _pkgqr in $(cat $DB_BASE/list/pkgrp_cleanup_dbl.txt); do
                          mv -iv \
                            $DB_ROOT/pool/$_pkgqr$PKG_EXT{,$SIG_EXT} \
                            $DB_ROOT/pool/quarantine || true
                        done
                        unset _pkgqr
                        ;;
                     *) echo "Input Error"
                        exit 1
                        ;;
esac
unset _itrt

echo "Do you want to use the interactive mode before every deprecated symlink removal?"
read _itrt
case $_itrt in
     [Yy][Ee][Ss]|[Yy]) for _type in ${DB_TYPE[@]}; do
                          for _arch in ${DB_ARCH[@]}; do
                            find -P $DB_ROOT/$_type/os/$_arch -xtype l -exec rm -iv {} \;
                          done
                          unset _arch
                        done
                        unset _type
                        ;;
      [Nn][Oo]|[Nn]|'') for _type in ${DB_TYPE[@]}; do
                          for _arch in ${DB_ARCH[@]}; do
                            find -P $DB_ROOT/$_type/os/$_arch -xtype l -exec rm  -v {} \;
                          done
                          unset _arch
                        done
                        unset _type
                        ;;
                     *) echo "Input Error"
                        exit 1
                        ;;
esac
unset _itrt

case $1 in
  -s) mkdir -pv $DB_ROOT/sources/quarantine
      for _srcqr in $(cat $DB_BASE/list/srcrp_base_dbl.txt); do
        echo "Move the $_srcqr$SRC_EXT to quarantine directory?"
        read _move
        case $_move in
             [Yy][Ee][Ss]|[Yy]) mv -iv \
                                  $DB_ROOT/sources/$_srcqr$SRC_EXT{,$SIG_EXT} \
                                  $DB_ROOT/sources/quarantine || true
                                ;;
              [Nn][Oo]|[Nn]|'') true
                                ;;
                             *) echo "Input Error"
                                exit 1
                                ;;
        esac
        unset _move
      done
      unset _srcqr
      ;;
esac
