Files
Server-scripts/quiethost/playlist_copy.sh
T
2026-09-14 12:43:38 +02:00

237 lines
6.8 KiB
Bash
Executable File

#!/bin/bash
RSYNCFILE="/tmp/rsync_files.txt"
MUSICDir="/data/DatenLucas/Music/Musik/"
if [[ -d /home/lucas/.quodlibet/playlists/ ]]; then
PLPATH="/home/lucas/.quodlibet/playlists/"
elif [[ -d /home/lucas/.config/quodlibet/playlists/ ]]; then
PLPATH="/home/lucas/.config/quodlibet/playlists/"
else
echo "No Playlist Directory found. Exiting"
exit
fi
if [ -f /home/lucas/.LOG/rsync.log ]; then
rm /home/lucas/.LOG/rsync.log
fi
PHONE_path="Default Path"
if [ -d /run/user/1000/gvfs/mtp* ]; then
PHONE_path="/run/user/1000/gvfs/$(ls /run/user/1000/gvfs/ | grep "mtp")/Internal shared storage/"
fi
ANDR_MUSIC="Music"
PHONE_PL_PATH="/\/storage\/emulated\/0\/${ANDR_MUSIC}\/"
i=0
SYNC=true
DRY_RUN=false
RESYNC=false
COPY=false
RELOAD_PLAYLIST=false
playlist_array=()
usage() {
printf "Usage
-p <pl1> <pl2>\t\tSync Playlists
-resync\t\t\tResync all Playlists
-reloadPlaylist\t\tCopy Playlists only
-all\t\t\tSync all Playlists in ${PLPATH}
-PO\t\t\tDont Sync
-copy\t\tCopy Files
-dry\t\t\tDry Run
-folder\t\tSync To /win10/Users/lucas/Music/
-watch\t\tSync To Garmin Watch
-help\t\t\tDisplay this\n"
}
get_config() {
while [[ ${1:0:1} != "" ]]; do
if [ $1 == -resync ]; then
RESYNC=true
shift 1
continue
fi
if [ $1 == -reloadPlaylist ]; then
RELOAD_PLAYLIST=true
break
fi
if [ $1 == -help ]; then
usage
exit
fi
if [ $1 == -dry ]; then
DRY_RUN=true
shift 1
continue
fi
if [ $1 == -copy ]; then
COPY=true
shift 1
continue
fi
if [ $1 == -diff ]; then
DIFF=true
shift 1
continue
fi
if [ $1 == -folder ]; then
PHONE_path="/win10/Users/lucas/Music/"
PHONE_PL_PATH="\/win10\/Users\/lucas\/Music\/"
rm -dr ${PHONE_path}Music/*
shift 1
continue
fi
if [ $1 == -watch ]; then
if [[ "$(ls /run/user/1000/gvfs/mtp*)" != "" ]]; then
PHONE_path="/run/user/1000/gvfs/mtp:host=091e_4c05_0000c6a66cb9/Primary/"
else
echo "No Watch Path found"
fi
shift 1
continue
fi
if [ $1 == -p ]; then
shift 1
echo "Sync Playlists in Arguments"
while [[ ${1:0:1} != "" ]] && [[ ${1:0:1} != "-" ]]; do
ls "${PLPATH}"$1 >>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "No such Playlist $1"
shift 1
continue
fi
arr=($(ls "${PLPATH}"$1 | rev | cut -f1 -d'/' | rev))
printf "Sync "
for ((i=0;i<${#arr[@]};i++)); do printf "%s " "${arr[$i]}"; done
printf "? (y/*) "
read -t 10 -n 2 choice
if [ "$choice" != "y" ]; then
printf "\nAbort\n"
shift 1
continue
fi
playlist_array+=($(ls "${PLPATH}"$1 | rev | cut -f1 -d'/' | rev))
((i++))
shift 1
continue
done
continue
fi
echo "Parameter $1 discarded"
usage
exit
done
}
if [ $# -lt 1 ]; then
echo "Too few Arguments"
usage
exit 0
fi
get_config "$@"
if [ ! -d "${PHONE_path}" ]; then
echo "Phone Path: ${PHONE_path} not found"
exit
else
echo "Phone Path: ${PHONE_path}"
fi
if [[ "${RESYNC}" == "true" ]] || [[ "${RELOAD_PLAYLIST}" == "true" ]] ; then
playlist_array+=($(ls /run/user/1000/gvfs/mtp:host=*/Internal\ shared\ storage/Playlists/ | sed 's/\.m3u//g'))
if [[ "${RELOAD_PLAYLIST}" == "true" ]]; then
SYNC=false
fi
fi
if [[ "${DIFF}" == "true" ]]; then
for playlist in "${playlist_array[@]}"; do
echo "Check Handy Playlist ${playlist} for Changes"
cat ${PHONE_path}BlackPlayer/Playlists/${playlist}.m3uBackup | sed 's/\/storage\/emulated\/0\/Music\///g' | sort > .handy_${playlist}
cat ${PLPATH}${playlist} | sed 's/\/data\/DatenLucas\/Music\/Musik\///g' | sort > .quodlibet_${playlist}
IFS=$'\n'; set -f
in_quodlibet=("$(diff .handy_${playlist} .quodlibet_${playlist} | grep '> ' | sed 's/> //g')")
in_handy=($(diff .handy_${playlist} .quodlibet_${playlist} | grep '< ' | sed 's/< //g'))
rm .quodlibet_${playlist} .handy_${playlist}
if [ ${#in_handy[@]} -gt 0 ]; then
printf "#### Handy ####\n\n"
else
printf "No Differences found\n"
fi
for song in ${in_handy[@]}; do
echo "Keep ${song}?(y/*)"
read -t 10 -n 2 choice
if [ "$choice" != "y" ]; then
printf "Delete\n"
else
echo "/data/DatenLucas/Music/Musik/${song}" >> ${PLPATH}${playlist}
fi
done
unset IFS; set +f
done
fi
for playlist in "${playlist_array[@]}"; do
IFS=$'\n'
PL_songs=($(cat "${PLPATH}${playlist}"))
unset IFS
NOW=$(pwd)
cd ${MUSICDir}
> ${RSYNCFILE}
for ((i=0;i<${#PL_songs[@]};i++)); do
ACT_PATH="$(echo "${PL_songs[$i]}" | rev | cut -f2- -d'/' | rev | sed 's/\/data\/DatenLucas\/'"${ANDR_MUSIC}"'\/Musik\///g')/"
NAME="$(echo "${PL_songs[$i]}" | rev | cut -f1 -d'/' | rev)"
echo "${ACT_PATH}${NAME}" >> ${RSYNCFILE}
done
if ${SYNC}; then
if ${DRY_RUN}; then
echo "Dry Run"
rsync -n -v --inplace --progress --omit-dir-times --no-perms --size-only --files-from=${RSYNCFILE} . "${PHONE_path}"${ANDR_MUSIC}/
else
if [[ "${COPY}" == "true" ]];then
printf "\nCopy $(ls "${PLPATH}${playlist}")\n"
IFS=$'\n'; set -f
count=0
for file in $(cat ${RSYNCFILE}); do
num=$(cat ${RSYNCFILE} | wc -l)
path=$(echo "$file" | rev | cut -f2- -d'/' | rev)
if [ ! -d "${PHONE_path}${ANDR_MUSIC}/${path}/" ]; then
echo "mkdir ${PHONE_path}${ANDR_MUSIC}/${path}/"
mkdir -p "${PHONE_path}${ANDR_MUSIC}/${path}/"
fi
echo "cp ${file}"
cp --force --no-preserve=ownership --no-preserve=timestamps ${file} ${PHONE_path}${ANDR_MUSIC}/${path}/
((count++))
done
unset IFS; set +f
else
printf "\nSyncing $(ls "${PLPATH}${playlist}")\n"
rsync -v --inplace --progress --delete --omit-dir-times --no-perms --size-only --files-from=${RSYNCFILE} . "${PHONE_path}"${ANDR_MUSIC}/ 2>>~/.LOG/rsync.log
fi
fi
fi
rm ${RSYNCFILE}
cd ${PLPATH}
echo "Copy ${playlist}.m3u"
cp ${playlist} /tmp/${playlist}.m3u
sed -i 's/\/data\/DatenLucas\/Music\/Musik\/'"${PHONE_PL_PATH}"'/g' "/tmp/${playlist}.m3u"
if [ -d "${PHONE_path}Playlists/" ]; then
cp --no-preserve=ownership --force --no-preserve=timestamps "/tmp/${playlist}.m3u" "${PHONE_path}Playlists/"
else
echo "No ${PHONE_path}Playlists/ Directory"
fi
rm "/tmp/${playlist}.m3u"
cd ${NOW}
done
for playlist in ${PLPATH}*; do
echo "Backup $(echo ${playlist} | rev | cut -f1 -d'/' | rev).m3u"
cp ${playlist} /home/lucas/Music/Playlists/$(echo ${playlist} | rev | cut -f1 -d'/' | rev).m3u
cp ${playlist} /home/lucas/Music/Playlists/WIN10/$(echo ${playlist} | rev | cut -f1 -d'/' | rev)_WIN10.m3u
sed -i 's/\/data/D:/g' "/home/lucas/Music/Playlists/WIN10/$(echo ${playlist} | rev | cut -f1 -d'/' | rev)_WIN10.m3u"
sed -i 's/\//\\/g' "/home/lucas/Music/Playlists/WIN10/$(echo ${playlist} | rev | cut -f1 -d'/' | rev)_WIN10.m3u"
done
IFS=$'\n'
for file in $(cat /home/lucas/.LOG/rsync.log | grep mp3 |cut -f2 -d'"'); do
rm "$file"
done
#cat TopOldSchool1 | rev | cut -f1 -d'/' | rev > TopOldSchool
#sort -um TopOldSchool1 TopOldSchool
#awk '!a[$0]++' TopOldSchool TopOldSchool1 > TopOldSchool2
#cat TopOldSchool | sort >> TopOldSchool9
#diff TopOldSchool8 TopOldSchool9