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


#디렉토리 구조 탐색


#선언부분
	galfile="/home/luark/gallery.html"
	#사진이 저장된 가장 상위디렉토리
	sshotdir="/home/luark/Desktop/works/girlcg/"
	#갤러리 이름
	homename="카쉘 미소녀 갤러리입니다. powered by KasheL EasyWebGallery"
	#웹계정의 갤러리용 디렉토리. 미리 만들어주셔야 합니다.
	#galdir="luark@kashel.net:~/kashel.net/htdocs/miso/"
	galdir="luark@haz3.com:~/public_html/miso/"
	#페이지당 사진 수
	articleperpage="2"
	articlepertable="3"
	#섬네일 사진의 크기
	width="280"
	height="205"
	#홈페이지의 메타데이타
	head="<?php header(\"Content-type: text/html; charset=UTF-8\") ?>"
	metadata="<head><meta http-equiv=\"Content-type\" content=\"text/html; charset=UTF-8\"><title>$homename</title>"
	#예상페이지수 계산
		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 "$head" > $galfile
	echo "<html>" >> $galfile
	echo "$metadata" >> $galfile
	echo "<frameset rows=9%,6%,* border=0>" >> $galfile
	echo "<frame src='sub.html' name='sub' scrolling="no">" >> $galfile
	echo "<frame src='page.html' name='page' scrolling="no">" >> $galfile
	echo "<frame src='pic.html' name='pic' scrolling="auto">" >> $galfile
	echo "</frameset>" >> $galfile
	echo "</html>" >> $galfile
	
#프레임1 sub.html작성
	echo "$head" > sub.html
	echo "<html>$metadata <style>a{text-decoration:none;color:#444444}</style></head> " >> sub.html
	echo "<body bgcolor='#d9d9c7' text='#000000'>$homename      <a href='http://blog.kashel.net' target='new'>Move To KasheL Blog</a><br>" >> sub.html
	
#프레임2 page.html작성
	echo "$head" > page.html
	echo "<html>$metadata <style>a{text-decoration:none;color:#444444}</style></head>" >> page.html
	#echo "<body bgcolor='#ededd9' text='#000000'>1. You can show the page list by clicking Topic.</body></html>" >> page.html
	echo "<body bgcolor='#ededd9' text='#000000'>1. 주제를 선택하시면 페이지 목록이 나옵니다.</body></html>" >> page.html

#프레임3 pic.html작성
	echo "$head" > pic.html
	echo "<html>$metadata  <style>a{text-decoration:none;color:#444444}</style></head>" >> pic.html
	#echo "<body bgcolor='#eeeeda' text='#000000'>2. You can show the pictures by clicking page number.</body></html>" >> pic.html
	echo "<body bgcolor='#eeeeda' text='#000000'>2. 페이지를 선택하시면 갤러리의 섬네일이 나옵니다.</br></br>제목 그대로 미소녀 갤러리 입니다. 여기저기 모에판이나 그 외의 사이트를 돌아다니다가 제 취향이다 싶으면 주워다가 모아놓는 공간입니다.</br></br>취향이라지만 특별한 기준이 있는 것은 아니고 그냥 필이 오는 것을 모아오다 보니 큰 틀거리는 없습니다.</br>굳이 따지자면 그림체가 독특하고 너무 야하지 않고 로리가 아니고(전 누님연방쪽이라..)</br>그리고 일본전통의상 입은것은 제외고 표정이 없는 그림들도 제외입니다.</br>아무튼 이런저런 아름다운 소녀의 CG입니다.(실사는 없습니다.) 그럼 구경해 보세요 우호홋=3</br></body></html>" >> pic.html

#주제목록을 만듭니다.
subcount=0
echo "페이지 목록을 만들고 있습니다."
echo "다음과 같은 주제가 검출되었습니다."
for sub in $(ls $sshotdir);do
echo "$sub"
done
for sub in $(ls $sshotdir);do
	#페이지 목록을 만듭니다.
	echo "$head" >> pagefile_$sub.html
	echo "<html>$metadata  <style>a{text-decoration:none;color:#444444}</style></head><body bgcolor='#ededd9' text='#000000'>Subject : $sub ||" >> pagefile_$sub.html
((howmanysub+=1))
	#그림디렉토리의 파일목록을 출력하여 html파일로 만드는 부분입니다.
	pagenumber=1
		echo "$head" >> galfile_$sub$pagenumber.html
		echo "<html><style>a{text-decoration:none;color:#444444}</style></head><body bgcolor=#eeeeda><center><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
				convert -sample $width'x'$height $sshotdir/$sub/$pic $sshotdir/$sub/thumb.$pic 
				echo -n "<td bgcolor=#ffffff>파일명 : $pic" >> galfile_$sub$pagenumber.html
				echo -n "<center><a href=$sub/$pic><img src=$sub/thumb.$pic bordercolor=#000000></a></center></td>" >> galfile_$sub$pagenumber.html
				((count+=1))


				if ((count%articlepertable));then
					continue
				else
					echo "</tr><tr>" >> galfile_$sub$pagenumber.html
				fi
				#페이지당 그림수를 파악해서 각 배수에 해당할 때마다 페이지링크를 추가하고 해당하지 않으면 그냥 계속합니다.
				if ((count%articleperpage));then
					continue
				else
					echo -n "<a href='galfile_$sub$pagenumber.html' target='pic'>$pagenumber </a>" >> pagefile_$sub.html	
					((pagenumber+=1))
					echo "$head" >> galfile_$sub$pagenumber.html
					echo "<html>  <style>a{text-decoration:none;color:#444444}</style></head><body bgcolor=#eeeeda><center><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 "사진의 인덱싱이 완료되었습니다."
	
#utf8인코딩을 euckr로 바꿉니다.

#mv *html sshot
#파일을 rsync할 준비를 합니다.
	mv $galfile $sshotdir/index.html
	mv *html $sshotdir/
	
#싱크합니다.
	echo "이제 웹계정에 업로드 하도록 하겠습니다. 만약 암호를 묻는다면 계정의 암호를 넣어주세요."
	#rsync -av -e "ssh -p 7373" $sshotdir/* $galdir
	rsync -av $sshotdir/* $galdir
	rm $sshotdir/*.html
	find $sshotdir/ -name "thumb.*" -exec rm {} \;
	
#완성입니다.
	echo "이처럼 손쉬운 방법으로 웹갤러리가 만들어 졌습니다. http://blog.kashel.net 의 luark에게 댓글이라도 남겨주세요"
	
