#!/bin/bash

 printHelp () {
   echo ""
   echo "  Usage: resetrepos.sh [--FORCE]"
   echo ""
   echo "  Reset (clear) all the contents of all the fossEDA repositories."
   echo "  All history will be lost."
   echo ""
   echo "  USE EXTREME CAUTION IF RUNNING THIS SCRIPT."
   echo ""
   echo "  Options:"
   echo "    [--FORCE] : Actually perform the reset."
 }

 if [ $# -eq 0 ]; then printHelp; exit 0; fi


 doReset="false"
 while [ $# -gt 0 ]; do
   case $1 in
     --FORCE) doReset="true";;
     *)       badArgument="$1";;
   esac
   shift
 done
 if [ ! -z "${badArgument}" ]; then
   echo "[ERROR] resetrepos.sh: Unknown argument \"${badArgument}\"."
   exit 1
 fi


 currentDir=`pwd`
 rpmRepos=""
 rpmRepos="${rpmRepos} AlmaLinux_8"
 rpmRepos="${rpmRepos} AlmaLinux_9"
 rpmRepos="${rpmRepos} Fedora_40"
 rpmRepos="${rpmRepos} Fedora_41"
 rpmRepos="${rpmRepos} openSUSE_Leap_15.6_images"
 rpmRepos="${rpmRepos} openSUSE_Tumbleweed"
 debRepos=""
 debRepos="${debRepos} Debian_12"
 debRepos="${debRepos} Ubuntu_22_04"
 debRepos="${debRepos} Ubuntu_24_04"
 debRepos="${debRepos} Ubuntu_24_10"

 echo "* Reseting all repositories contents."
 echo "* RPM based distributions:"
 for rpmRepo in ${rpmRepos}; do
   absolutePath="${currentDir}/${rpmRepo}"
   echo -n "  - ${absolutePath} ..."
   if [ -d "${absolutePath}" ]; then
     rm -r "${absolutePath}"
     echo "done."
   else
     echo "not found."
   fi
 done
 echo "* DEB based distributions:"
 for debRepo in ${debRepos}; do
   absolutePath="${currentDir}/${debRepo}"
   echo -n "  - ${absolutePath} ..."
   if [ -d "${absolutePath}" ]; then
     rm -r "${absolutePath}/db"
     rm -r "${absolutePath}/dists"
     rm -r "${absolutePath}/lists"
     rm -r "${absolutePath}/pool"
     echo "done."
   else
     echo "not found."
   fi
 done
