#!/bin/bash

# rm's cue2flac - a script to convert cue+(ape/flac/wv) to flac
# Copyright (C) 2009-2020 Roman Mamedov <https://romanrm.net/>
#
# This program is free software: you can redistribute it and/or modify it under the terms of
# the GNU General Public License version 3 as published by the Free Software Foundation.
# See http://www.gnu.org/licenses/gpl-3.0.html for more details.

# This is a version released on 2022-11-26.

FIX_CBP_FORMAT=1

if [ ! `ls *.cue 2> /dev/null | wc -l` -eq 1 ]
then
  echo "This script supports only one CUE file in the current dir."
  exit 1
fi

if [ ! `ls *.ape *.flac *.wv *.wav 2> /dev/null | wc -l` -eq 1 ]
then
  echo "This script supports only one APE/FLAC/WV/WAV file in the current dir."
  exit 1
fi

echo "Checking dependencies..."
if [ ! `dpkg -l | colrm 40 | grep " cuetools \| flac \| libjmac-java \| shntool \| wavpack " | wc -l` -eq 5 ]
then
  echo "Please install all the required dependencies:"
  echo "  sudo apt-get install cuetools flac libjmac-java shntool wavpack"
  exit 1
fi

while getopts ":e:cf:" opt; do
  case $opt in
    c) CLEANUP=1;;
    e) CUE_ENC=$OPTARG;;
    f) FORMAT=$OPTARG;;
  esac
done

if [ "$FORMAT" == "" ]; then
  FORMAT=flac
fi

if [ "$FORMAT" == "flac" ]; then
  FINALEXT=flac
  ENCODER="$FINALEXT flac --best - -o %f"
elif [ "$FORMAT" == "mp3" ]; then
  FINALEXT=mp3
  ENCODER="cust ext=mp3 lame --quiet --preset insane -q0 - %f"
fi

if [ "$FIX_CBP_FORMAT" == "1" ]; then
  CBPFORMATFIX="sed -e s/$/0/"
else
  CBPFORMATFIX=cat
fi

echo "Creating a temporary folder..."
PREFIXTMP=cue2flac-`whoami`-tmp
mkdir $PREFIXTMP.$$ || exit 1
cd $PREFIXTMP.$$ || exit 1

cp ../*.cue tmp.cue

if [ -n "$CUE_ENC" ]
then
  echo Decoding the .cue file from $CUE_ENC to current locale...
  iconv -f $CUE_ENC ../*.cue > tmp.cue
else
  cp ../*.cue tmp.cue
fi

if [ -n "`ls ../*.ape 2> /dev/null`" ]
then
  echo "Decoding APE to WAV..."
  java -jar /usr/share/java/jmac.jar d ../*.ape temp.wav || exit 1
  echo "Splitting WAV into individual tracks and encoding to $FINALEXT..."
  cuebreakpoints tmp.cue | $CBPFORMATFIX | shnsplit -a split-track temp.wav -o "$ENCODER" || exit 1
  rm temp.wav
fi

if [ -n "`ls ../*.wv 2> /dev/null`" ]
then
  echo "Splitting WV into individual tracks and encoding to $FINALEXT..."
  cuebreakpoints tmp.cue | $CBPFORMATFIX | shnsplit -a split-track ../*.wv -o "$ENCODER" || exit 1
fi

if [ -n "`ls ../*.wav 2> /dev/null`" ]
then
  echo "Splitting WAV into individual tracks and encoding to $FINALEXT..."
  cuebreakpoints tmp.cue | $CBPFORMATFIX | shnsplit -a split-track ../*.wav -o "$ENCODER" || exit 1
fi

if [ -n "`ls ../*.flac 2> /dev/null`" ]
then
  echo "Splitting FLAC into individual tracks and encoding to $FINALEXT..."
  cuebreakpoints tmp.cue | $CBPFORMATFIX | shnsplit -a split-track ../*.flac -o "$ENCODER" || exit 1
fi

echo "Tagging $FINALEXT files..."
cuetag tmp.cue *.$FINALEXT || exit 1

echo "Renaming $FINALEXT files..."
# for (( i=1 ; i <= $(cueprint -d '%N' ../*.cue) ; ++i )) ; do
# As reported, the above line does not work on some systems, so instead of it, let's use the following:
for i in `seq  1 $(cueprint -d '%N' tmp.cue)` ; do
  NN=$(printf '%02d' $i)
  mv split-track$NN.$FINALEXT "$NN) `cueprint -n $i -t "%p - %t" tmp.cue`.$FINALEXT"
done

cd ..

echo "Moving old files to \"orig/\"..."
mkdir orig
mv *.{flac,ape,wv,wav,cue} orig/ 2>/dev/null

if [ "$CLEANUP" == "1" ]
then
  echo Cleaning up...
  rm Thumbs.db 2>/dev/null
  mv *.url orig/
  if [ -n "`ls *.{jpg,gif,log} 2> /dev/null`" ]
  then
    mkdir misc
    mv *.{jpg,gif,log} misc/ 2>/dev/null
  fi
fi

echo "Removing temporary files..."
mv $PREFIXTMP.$$/*.$FINALEXT . || exit 1
rm -rf $PREFIXTMP.*

echo "Done."
