#!/bin/bash
basepath1='/z9/4chan.org/gif'
# \/z4\/sync_mnt-e\/dl\/13b48ab\/test\/ -> '\/z9\/4chan.org\/gif\/test\/'


if ps -ef | grep -v grep | grep cron.sh; then
exit 0
else


if [ $(expr $(date +%s) - $(cat /z9/4chan.org/gif/time.txt)) -gt 86400 ]; then


cat /z9/4chan.org/gif/torun.txt | xargs -d "\n" sh -c 'for args do sh -c "$args"; done' _ 2>> $basepath1/cronlog2.txt
rm /z9/4chan.org/gif/torun.txt

now=$(date +%s); echo $now > /z9/4chan.org/gif/time.txt; curl -sL https://a.4cdn.org/gif/catalog.json > $basepath1/threads/catalog.json.$now; cat /z9/4chan.org/gif/threads/catalog.json.$now | jq . | grep "^\s\s\s\s\s\s\s\s\"no\": " | sed "s/.* //g" | sed "s/,.*//g" | grep -v "^6226033$" > /z9/4chan.org/gif/chkcmd.txt

threadcount=$(wc -l /z9/4chan.org/gif/chkcmd.txt | sed "s/ .*//g")

i=1; while [ $i -lt $(expr $threadcount + 1) ]; do

 ii=$(tail -n+$i /z9/4chan.org/gif/chkcmd.txt | head -n1)

 echo $i; echo $ii

 curl -sL https://a.4cdn.org/gif/thread/$ii.json > /z9/4chan.org/gif/threads/$ii.json.$now

 imgcount=$(expr $(cat /z9/4chan.org/gif/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | wc -l) / 3)

 filename=($(cat /z9/4chan.org/gif/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | tail -n+$(expr $imgcount + 1) | head -n$imgcount))

 ext=($(cat /z9/4chan.org/gif/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | head -n$imgcount | sed "s/\"//g"))

 md5=($(cat /z9/4chan.org/gif/threads/$ii.json.$now | jq ".posts[].ext, .posts[].tim, .posts[].md5" | grep -v "^null$" | tail -n$imgcount | sed "s/\"\|=//g" | sed "s/\//_/g" | sed "s/+/-/g"))

 n=0; while [ $n -lt $imgcount ]; do echo TZ=UTC wget -nc https://i.4cdn.org/gif/${filename[$n]}${ext[$n]} -O /z9/4chan.org/gif/test/${md5[$n]}; n=$(expr $n + 1); done >> /z9/4chan.org/gif/torun.txt

 i=$(expr $i + 1)

done


fi


fi
