comparison bin/index @ 138:f963d52c031a

Merge.
author Edho Arief <edho@myconan.net>
date Wed, 02 Nov 2011 22:12:16 +0700
parents 5bafb912837e
children
comparison
equal deleted inserted replaced
137:983beb13c736 138:f963d52c031a
1 #!/bin/sh
2
3 #un(POSIX / bourne shell)-ness: "read -r"
4
5 WORKDIR="${PWD}"
6 TEMPFILE="${WORKDIR}/temp/all"
7 RESFILE="${WORKDIR}/list/all"
8 ARCDIR="${WORKDIR}/archives"
9 FILEDIRS="complete1 complete3 complete4 complete5 incomplete movie"
10 #FILEDIRS="complete-all incomplete movie"
11
12 Err_Fatal() {
13 echo "Error: $*"
14 exit 1
15 }
16
17 Progress_Init() {
18 _last="-"
19 printf "${_last}"
20 }
21
22 Progress_Anim() {
23 case "${_last}" in
24 /) _last="-";;
25 -) _last=\\;;
26 \\) _last=\|;;
27 \|) _last="/";;
28 esac
29 printf "\b${_last}"
30 }
31
32 Progress_Done() { printf "\bdone\n"; }
33
34 Check_Dirs() {
35 [ -w "${WORKDIR}" ] || Err_Fatal "Unable to write ${WORKDIR}"
36 for dir in temp list archives backup; do
37 mkdir -p "${WORKDIR}/${dir}" 2>/dev/null
38 [ -w "${WORKDIR}/${dir}" ] || Err_Fatal "Unable to write ${WORKDIR}/${dir}"
39 done
40 }
41
42 Finish_Copy() {
43 printf "Copying result files... "
44 rm "${WORKDIR}/list/"*
45 cp "${WORKDIR}/temp/"* "${WORKDIR}/list"
46 mkdir -p "archives/${DATE}" && cp "${WORKDIR}/temp/"* "${WORKDIR}/archives/${DATE}"
47 echo "done"
48 }
49
50 Generate_File_List() {
51 printf "Generating list of files... "
52 > "${TEMPFILE}-bare"
53 for i in ${FILEDIRS}; do
54 printf "${i}... "
55 dir="/anime/${i}"
56 [ -d "${dir}" ] && find "${dir}/"* -type f >> "${TEMPFILE}-bare"
57 done
58 echo "done"
59 printf "Checking list validity... "
60 [ -n "$(grep -v ^/ "${TEMPFILE}-bare")" ] && Err_Fatal "Newline in filename. Please check ${TEMPFILE}-bare"
61 echo "done"
62
63 printf "Generating list of filenames... "
64 sed -e 's/.*\/\([^/]*\)/\1/' "${TEMPFILE}-bare" > "${TEMPFILE}-basename"
65 echo "done"
66
67 printf "Sorting and finding duplicates... "
68 sort -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sorted"
69 mv -- "${TEMPFILE}-basename-sorted" "${TEMPFILE}-basename"
70 sort -u -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sortuniq"
71 comm -23 -- "${TEMPFILE}-basename" "${TEMPFILE}-basename-sortuniq" > "${TEMPFILE}-dup-basename"
72 echo "done"
73 if [ "$(wc -l < "${TEMPFILE}-dup-basename")" -gt 0 ]; then
74 > "${TEMPFILE}-dup-bare"
75 printf "Duplicate files detected. Generating list of duplicates... "
76 Progress_Init
77 while read -r dup; do
78 fgrep "${dup}" "${TEMPFILE}-bare" >> "${TEMPFILE}-dup-bare"
79 Progress_Anim
80 done < "${TEMPFILE}-dup-basename"
81 Progress_Done
82 Err_Fatal "Duplicated files detected. Please resolve this before continuing.
83 Check ${TEMPFILE}-dup-basename for list of duplicates"
84 else
85 rm "${TEMPFILE}-dup-basename" "${TEMPFILE}-basename-sortuniq"
86 fi
87 #yay finished
88 }
89
90 Generate_New_List() {
91 printf "Generating list of new files... "
92 Progress_Init
93 > "${TEMPFILE}-newbase"
94 sort "${RESFILE}-basename" > "${TEMPFILE}-res-basename"
95 mv "${TEMPFILE}-res-basename" "${RESFILE}-basename"
96 comm -13 "${RESFILE}-basename" "${TEMPFILE}-basename" > "${TEMPFILE}-newbase"
97 Progress_Anim
98 > "${TEMPFILE}-new"
99 while read -r added; do
100 fgrep "${added}" "${TEMPFILE}-bare" >> "${TEMPFILE}-new"
101 Progress_Anim
102 done < "${TEMPFILE}-newbase"
103 rm "${TEMPFILE}-newbase"
104 Progress_Done
105 }
106
107 Init_Check() {
108 cp "${TEMPFILE}-bare" "${TEMPFILE}-new"
109 }
110
111 Run_ed2k() {
112 #[ "$(echo $(wc -l < "${TEMPFILE}-new"))" -eq 0 ] && Err_Fatal "Not an error. Just lazy to create new handler. (Nothing new)"
113 printf "Generating ed2k links...0"
114 > "${TEMPFILE}-ed2k-newlist"
115 num=0
116 while read -r file; do
117 ed2k -l "${file}" >> "${TEMPFILE}-ed2k-newlist"
118 num=$((num+1))
119 if [ "$((num%10))" -eq 0 ]; then
120 printf "${num}"
121 else
122 printf "."
123 fi
124 done < "${TEMPFILE}-new"
125 echo " done"
126 }
127
128 Generate_Removed() {
129 printf "Generating list of removed files... "
130 Progress_Init
131 cp "${TEMPFILE}-ed2k-newlist" "${ARCDIR}/ed2k-newlist.${DATE}"
132 cat "${RESFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-newlist"
133 sort -u "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlist-temp"
134 mv "${TEMPFILE}-ed2k-newlist-temp" "${TEMPFILE}-ed2k-newlist"
135 Progress_Anim
136 comm -13 "${TEMPFILE}-basename" "${RESFILE}-basename" | sort > "${TEMPFILE}-removed"
137 Progress_Anim
138 if [ "$(echo $(wc -l < "${TEMPFILE}-removed"))" -gt 0 ]; then
139 > "${TEMPFILE}-ed2k-newlisttemp"
140 while read -r deleted; do
141 fgrep "${deleted}" "${TEMPFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-removed"
142 Progress_Anim
143 fgrep -v "${deleted}" "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlisttemp"
144 mv "${TEMPFILE}-ed2k-newlisttemp" "${TEMPFILE}-ed2k-newlist"
145 Progress_Anim
146 done < "${TEMPFILE}-removed"
147 cp "${TEMPFILE}-ed2k-removed" "${ARCDIR}/ed2k-removed.${DATE}"
148 #sed -e 's/ed2k:\/\/|file|\(.*\)|[^|]*|[^|]*|$/\1/' "${TEMPFILE}-ed2k-removed" > "${TEMPFILE}-base-removed"
149 Progress_Anim
150 while read -r deleted; do
151 fgrep "${deleted}" "${RESFILE}-bare" >> "${TEMPFILE}-bare-removed"
152 Progress_Anim
153 done < "${TEMPFILE}-removed"
154 cp "${TEMPFILE}-bare-removed" "${ARCDIR}/bare-removed.${DATE}"
155 fi
156 Progress_Done
157 }
158
159 INIT=
160 UPDATE=
161 DATE=$(date -u "+%Y%m%d-%H.%M")
162 case "$1" in
163 init) INIT=1;;
164 update|up) UPDATE=1;;
165 *) Err_Fatal "You fail.";;
166 esac
167 Check_Dirs
168 [ -n "${UPDATE}" ] && cd "${WORKDIR}" && tar zcf "${WORKDIR}/backup/${DATE}.tar.gz" "temp" "list" "archives"
169 [ -n "${UPDATE}" ] && rm "${WORKDIR}/temp/"* 2>/dev/null
170 Generate_File_List
171 [ -n "${INIT}" ] && Init_Check
172 [ -n "${UPDATE}" ] && Generate_New_List
173 Run_ed2k
174 [ -n "${UPDATE}" ] && Generate_Removed
175 Finish_Copy
176