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


#디렉토리 구조 탐색


#선언부분
	galfile="/home/luark/gallery.html"
	#사진이 저장된 가장 상위디렉토리
	sshotdir="/home/luark/sshot/"
	#갤러리 이름
	homename="Gallery for Kashel. powered by easywebgallery"
	#웹계정의 갤러리용 디렉토리. 미리 만들어주셔야 합니다.
	galdir="luark@kashel.net:~/kashel.net/htdocs/sshot"
	#페이지당 사진 수
	articleperpage="3"
	#사진의 크기
	width="320"
	height="240"
	#홈페이지의 메타데이타
	metadata="<head><title>$homename</title><meta http-equiv='Content-type' content='text/html; charset=utf8'></head>"
	#예상페이지수 계산
		howmanypics=$(echo $(ls $sshotdir | wc -l))
		howmanypage=$(echo $(ls $sshotdir | wc -l)/$articleperpage | bc)
		howmanysub=$(echo $(ls $sshotdir | wc -l))
		#여분으로 하나더 
			page=$(echo $howmanypage+1 | bc)

#작업할디렉토리로 이동합니다.
	cd $sshotdir;cd ..

#프레임의 기본틀 작성
	echo "<html>" > $galfile
	echo "$metadata" >> $galfile
	echo "<frameset rows=9%,6%,* border=0>" >> $galfile
	echo "<frame src='sub.html' name='sub'>" >> $galfile
	echo "<frame src='page.html' name='page'>" >> $galfile
	echo "<frame src='pic.html' name='pic'>" >> $galfile
	echo "</frameset>" >> $galfile
	echo "</html>" >> $galfile
	
#프레임1 sub.html작성
	echo "<html>$metadata <style>a{text-decoration:none;color:#444444}</style> " > sub.html
	echo "<body bgcolor='#e6e6e6' text='#000000'>$homename      <a href='http://blog.kashel.net' target='new'>Move To KasheL Blog</a><br>" >> sub.html
	
#프레임2 page.html작성
	echo "<html>$metadata <style>a{text-decoration:none;color:#444444}</style>" > page.html
	echo "<body bgcolor='#d6d6d6' text='#000000'>1. You can show the page list by clicking Topic.</body></html>" >> page.html

#프레임3 pic.html작성
	echo "<html>$metadata  <style>a{text-decoration:none;color:#444444}</style>" > pic.html
	echo "<body bgcolor='#c6c6c6' text='#000000'>2. You can show the pictures by clicking page number.</body></html>" >> pic.html

#주제목록을 만듭니다.
subcount=0
echo "페이지 목록을 만들고 있습니다."
echo "다음과 같은 주제가 검출되었습니다."
for sub in $(ls $sshotdir);do
echo "$sub"
done
for sub in $(ls $sshotdir);do
	#페이지 목록을 만듭니다.
	echo "<html>$metadata  <style>a{text-decoration:none;color:#444444}</style><body bgcolor='#d6d6d6' text='#000000'>Subject : $sub ||" >> pagefile_$sub.html
((howmanysub+=1))
	#그림디렉토리의 파일목록을 출력하여 html파일로 만드는 부분입니다.
	pagenumber=1
		echo "<html>  <style>a{text-decoration:none;color:#444444}</style><body bgcolor=#c6c6c6><center><br><br><br><br><table valign=middle width=400 cellspacing=10 cellpadding=10 border=1 bordercolor=#000000 style=border-collapse:collapse;><tr>" >> galfile_$sub$pagenumber.html
	while ((pagenumber<page));do
		count=0
			#목록을 추가하는 부분입니다.
			for pic in $(ls $sshotdir/$sub);do
				echo -n "<td bgcolor=#ffffff>File Name : $pic" >> galfile_$sub$pagenumber.html
				echo -n "<center><a href=$sub/$pic><img src=$sub/$pic width=$width height=$height bordercolor=#000000></a></center></td>" >> galfile_$sub$pagenumber.html
				((count+=1))

				#페이지당 그림수를 파악해서 각 배수에 해당할 때마다 페이지링크를 추가하고 해당하지 않으면 그냥 계속합니다.
				if ((count%articleperpage));then
					continue
				else
					echo -n "<a href='galfile_$sub$pagenumber.html' target='pic'>$pagenumber </a>" >> pagefile_$sub.html	
					((pagenumber+=1))
					echo "<html>  <style>a{text-decoration:none;color:#444444}</style><body bgcolor=#c6c6c6><center><br><br><br><br><table valign=middle cellspacing=10 cellpadding=10 width=400 border=1 bordercolor=#000000 style=border-collapse:collapse;><tr>" >> galfile_$sub$pagenumber.html
					count=0
				fi
			done
			echo "</tr></table></center></body></html>" >> galfile_$sub$pagenumber.html
			
	done
	
	#주제숫자를 파악해서 같으면 html을 종료하고 빠져나가고, 작으면 계속됩니다.
	if ((howmanysub<subcount));then
		echo "</body></html>" >> pagefile_$sub.html
		break
	else ((howmanysub=subcount));
		((subcount+=1))
		continue
	fi

done

#주제 목록을 작성합니다.
	echo "주제 목록을 작성중입니다......"
	echo -n "Subject : " >> sub.html
	for sub in $(ls $sshotdir);do
	echo -n "<a href='pagefile_$sub.html' target='page'>$sub</a>    " >> sub.html
	done
	echo "</body></html>" >> sub.html

echo "사진의 인덱싱이 완료되었습니다."
	
#mv *html sshot
#파일을 rsync할 준비를 합니다.
	mv $galfile $sshotdir/index.html
	mv *html $sshotdir/
	
#싱크합니다.
	echo "이제 웹계정에 업로드 하도록 하겠습니다. 만약 암호를 묻는다면 계정의 암호를 넣어주세요."
	rsync -av -e "ssh -p 7373" $sshotdir/* $galdir
	rm $sshotdir/*.html

#완성입니다.
	echo "이처럼 손쉬운 방법으로 웹갤러리가 만들어 졌습니다. http://blog.kashel.net 의 luark에게 댓글이라도 남겨주세요"
	
