#!/bin/bash
#이 스크립트는 홈디렉토리에 있는 그림파일들을 이용해 간단하게 
#갤러리는 만들어 줍니다.
#이 스크립트의 작성자는 http://blog.kashel.net 의 luark 입니다.
#GPL을 따르므로 맘대로 고쳐쓰셔도 좋지만 원작자 표시는 지우지 말아주세요 ㅜㅜ


#선언부분
galfile="/home/luark/gallery.html"
sshotdir="/home/luark/sshot/"
homename="Kashel ScrennShots Gallery   by luark"
galdir="luark@kashel.net:~/kashel.net/htdocs/sshot"
articleperpage="10"
#예상페이지수 계산
howmanypics=$(echo $(ls /home/luark/sshot | wc -l))
howmanypage=$(echo $(ls /home/luark/sshot | wc -l)/$articleperpage | bc)
#여분으로 하나더 
page=$(echo $howmanypage+1 | bc)

cd /home/luark/

#프레임의 기본틀 작성
echo "<html>" > $galfile
echo "<head><title>$homename</title><meta http-equiv='Content-type' content='text/html; charset=utf8'></head>" >> $galfile
echo "<frameset rows=10%,*>" >> $galfile
echo "<frame src='top.html'>" >> $galfile
echo "<frame src='pic.html' name='pic'>" >> $galfile
echo "</frameset>" >> $galfile
echo "</html>" >> $galfile

#프레임1 pic.html작성
echo "<html><head><title>$homename</title><meta http-equiv='Content-type' content='text/html; charset=utf8'></head>" > pic.html
echo "<body bgcolor='#ffffff' text='#000000'>To show the screenshots, Click the page number plz</body></html>" >> pic.html

#프레임2 top.html작성
echo "<html><head><title>$homename</title><meta http-equiv='Content-type' content='text/html; charset=utf8'></head>" > top.html
echo "<body bgcolor='#ffffff' text='#000000'>$homename                             <a href='http://blog.kashel.net' target='top'>Move To KasheL Blog</a><br>" >> top.html

echo "now making page $page.."

#그림디렉토리의 파일목록을 출력하여 html파일로 만드는 부분입니다.
	pagenumber=0
	while ((pagenumber<page));do
		count=0
				for i in $(ls $sshotdir);do
					echo -n "File Name : $i<br>" >> galfile_$pagenumber.html
					echo -n "<img src=$i>" >> galfile_$pagenumber.html
					echo "</br></br>" >> galfile_$pagenumber.html
					((count+=1))
					if ((count%10));then
						echo
					else
						((pagenumber+=1))
					fi
				done
	done

#페이지 목록을 만듭니다.
	echo "Now making Page List...."
	pages=0
	while ((pages<=pagenumber));do
		echo "<a href='galfile_$pages.html' target='pic'>$pages</a>" >> top.html	
		((pages+=1))
	done
	echo "</body></html>" >> top.html

#파일을 rsync할 준비를 합니다.
mv $galfile $sshotdir/index.html
mv *html $sshotdir/

#싱크합니다.
echo "work completed, Now Uploading Files..."
rsync -av -e "ssh -p 7373" $sshotdir/* $galdir
rm $sshotdir/*.html

echo "You Have a Web Gallery"
