#!/usr/bin/sh

#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2019********
export TEXTDOMAIN="default-apps"
export TEXTDOMAINDIR="/usr/share/locale"
[ ! -f /etc/blackPanther-default-apps.conf ] && echo "System error :-("  && exit
#---------------------------------------------------------------
# Project         : blackPanther OS Plex Media Server tool
# Module          : bin
# File            : plexsqlfsck
# Version         : 0.1
# Author          : Charles Barcza
# Created On      : Fri Jan 4 2019
# Purpose         : Fix/Clean SQL database
#---------------------------------------------------------------
 . /etc/blackPanther-default-apps.conf
if [ "$UID" != "0" ]; then  
echo "  ==> Sorry! Root access required.." 
exit 0
fi 

PLEX_HOME=/var/lib/plexmediaserver

# integrity_check
check() {
echo "Going to Plex Databases..."
cd $PLEX_HOME/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases/
echo "Backing Up...."
cp com.plexapp.plugins.library.db com.plexapp.plugins.library.db.original
echo "Preparing..."
sqlite3 com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"
echo "Cleaning..."
sqlite3 com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"
echo -n "Integrity check:" 
sqlite3 com.plexapp.plugins.library.db "PRAGMA integrity_check"
echo 
}

#Repair
repair () {
echo "Going to Plex Databases..."
cd $PLEX_HOME/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases/
echo "Backing Up (to .original)"
cp -f com.plexapp.plugins.library.db com.plexapp.plugins.library.db.original
echo "Preparing.."
sqlite3 com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"
echo "Cleaning..."
sqlite3 com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"
echo "Dump Databases..."
sqlite3 com.plexapp.plugins.library.db .dump > dump.sql
echo "Remove broken Databases..."
rm -f com.plexapp.plugins.library.db
echo -n "Restore Database structure:"
sqlite3 com.plexapp.plugins.library.db < dump.sql
echo " ok"
echo
}
# clean temporary backups
clean () {
echo "Going to Plex Databases..."
cd $PLEX_HOME/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases/

echo "Remove Old Plex Databases..."
rm -f com.plexapp.plugins.library.db-shm

rm -f com.plexapp.plugins.library.db-wal

rm -f dump.sql

echo "Cleaning: OK"

echo
}
# permission fix
permfix (){
echo "Going to Plex Home..."
chown plex:plex $PLEX_HOME -R
echo "Plex permissions : ok"
echo
}


case $1 in
    check)
    check
    permfix
    ;;
    repair)
    repair
    clean
    permfix
    ;;
    clean)
    clean
    ;;
    permfix)
    permfix
    ;;
    autofix)
    check
    repair
    clean
    permfix
    ;;
    *)
    echo ""
    echo "Usage: $(basename $0) [ check | repair | clean | permfix | atofix ] (just one)"
    echo ""
    ;;
esac


