Mixer: Gain_Module now accepts gain in dB.
[nondaw.git] / remove-unused-sources
blob798a1c0563e21a2a4d0ec7bdf74033202feb0cbe
1 #!/bin/sh
3 # Copyright (C) 2008 Jonathan Moore Liles #
4 # #
5 # This program is free software; you can redistribute it and/or modify it #
6 # under the terms of the GNU General Public License as published by the #
7 # Free Software Foundation; either version 2 of the License, or (at your #
8 # option) any later version. #
9 # #
10 # This program is distributed in the hope that it will be useful, but WITHOUT #
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
13 # more details. #
14 # #
15 # You should have received a copy of the GNU General Public License along #
16 # with This program; see the file COPYING. If not,write to the Free Software #
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
19 ## remove-unused-sources
21 # April 2008, Jonathan Moore Liles
23 # Simple script to scan a compacted Non-DAW project and remove all
24 # unused sources from disk.
26 # USAGE:
28 # $ remove-unused-sources ~/audio/'The Best Song Ever'
30 # NOTES:
32 # This script will not ask for comfirmation! It will ruthlessly
33 # delete all unused sources! You have been warned.
36 DRY_RUN=
37 ONLY_COMPACTED=
38 MOVE=1
40 fatal ()
42 echo Error: "$1"
43 echo 'Aborting!'
44 cleanup
45 exit 1
48 cleanup ()
50 rm -f "${TEMP}/all-sources" "${TEMP}/used-sources"
53 set_diff ()
55 diff --new-line-format '' --old-line-format '%L' --unchanged-line-format '' "$1" "$2"
58 remove_sources ()
60 local TOTAL=0
61 local FILE
62 local SIZE
63 local PSIZE
64 while read FILE
67 PSIZE=`stat -c '%s' "${FILE}.peak" 2>/dev/null`
68 SIZE=`stat -c '%s' "${FILE}" 2>/dev/null`
70 PSIZE=${PSIZE:-0}
72 if ! [ -f "${FILE}" ]
73 then
74 echo "Would remove \"${FILE}\", if it existed."
75 else
76 if [ "$DRY_RUN" = 1 ]
77 then
78 echo "Would remove: ${FILE}"
79 else
80 if [ "$MOVE" = 1 ]
81 then
82 echo "Moving unused source \"${FILE}\"..."
83 mv -f ./"${FILE}" ./"${FILE}".peak ../unused-sources
84 else
85 echo "Removing unused source \"${FILE}\"..."
86 rm -f ./"${FILE}" ./"${FILE}".peak
90 TOTAL=$(( $TOTAL + $SIZE + $PSIZE ))
93 done
95 echo "...Freeing a total of $(($TOTAL / ( 1024 * 1024 ) ))MB"
98 usage ()
100 fatal "Usage: $0 [-n] [-c] [-m|-d] path/to/project"
104 while getopts "dmnc" o
106 case "$o" in
107 d) MOVE= ;;
108 m) MOVE=1 ;;
109 n) DRY_RUN=1 ;;
110 c) ONLY_COMPACTED=1 ;;
111 \?) usage ;;
112 *) echo "$o" && usage ;;
113 esac
114 done
116 shift $(( $OPTIND - 1 ))
117 PROJECT="$1"
119 [ $# -eq 1 ] || usage
121 cd "$PROJECT" || fatal "No such project"
123 [ -f history ] && [ -f info ] || fatal "Not a Non-DAW project?"
125 [ -f .lock ] && fatal "Project appears to be in use"
127 if [ "$ONLY_COMPACTED" = 1 ]
128 then
129 grep -v '\(^\{\|\}$\)\|create' history && fatal "Not a compacted project"
132 echo "Scanning \"${PROJECT}\"..."
134 sed -n 's/^\s*Audio_Region.* :source "\([^"]\+\)".*$/\1/p' history | sort | uniq > "${TEMP}/used-sources"
136 cd sources || fatal "Can't change to source directory"
138 [ "$MOVE" = 1 ] && mkdir ../unused-sources 2>/dev/null
140 ls -1 | grep -v '\.peak$' | sort > "${TEMP}/all-sources"
142 set_diff "${TEMP}/all-sources" "${TEMP}/used-sources" | remove_sources
144 cleanup
146 echo "Done."