%%% kautojosa.sty
%%	  ver 0.002a
%%		2004/4/23 \pageref & bug fixed.
%%    ver 0.001
%%    	2004/03/26
%%
%%		Karnes Kim.
%%
%% Autojosa for HLaTeX+Hyperref
%% e.g., \usepackage{hangul-nojosa} %% not hangul.
%%       \usepackage[dvipdfm,CJKbookmarks]{hyperref}  %% for DVPIDFMx
%%
%% PROBLEMS:
%% when you run the latex command for the first time or label changed,
%% many errors will occur. So, you should run latex as
%%  latex --interaction=nonstopmode foo
%% or
%%  latex --interaction=batchmode foo
%% for reduce errors.
%%
%\makeatletter

\newcommand\autojosa@name{}
\newcommand\autojosa@ref@name{}
%\newcommand\autojosa@type{}

%%
%% use \karef{#1}{#2} instead of \ref
%% #1 : labelstring
%% #2 : eun, neun, i, ga, wa, gwa, eul, reul, ra, ira, ro, euro
%% And use \kalabel{labelstring} instead of \label
%%
\newcommand\karef[2]{%
		\ka{ref}{#1}{#2}%
}

\newcommand\kapageref[2]{%
	\ka{pageref}{#1}{#2}%
}

%% Setting case of josa
\newcommand\josa@{%
	\def\case@fj@sa{0}%
}
\newcommand\josa@i{%
	\def\case@fj@sa{1}%
}
%\newcommand\josa@��{%
%	\def\case@fj@sa{0}%
%}
\newcommand\josa@ga{%
	\def\case@fj@sa{1}%
}
\newcommand\josa@eun{%
	\def\case@fj@sa{2}%
}
\newcommand\josa@neun{%
	\def\case@fj@sa{2}%
}
\newcommand\josa@eul{%
	\def\case@fj@sa{3}%
}
\newcommand\josa@reul{%
	\def\case@fj@sa{3}%
}
\newcommand\josa@wa{%
	\def\case@fj@sa{4}%
}
\newcommand\josa@gwa{%
	\def\case@fj@sa{4}%
}
\newcommand\josa@ira{%
	\def\case@fj@sa{5}%
}
\newcommand\josa@ra{%
	\def\case@fj@sa{5}%
}
\newcommand\josa@ro{%
	\def\case@fj@sa{6}%
}
\newcommand\josa@euro{%
	\def\case@fj@sa{6}%
}

\long\def\@sixthofsix#1#2#3#4#5#6{#6}%
%\long\def\@firstofsix#1#2#3#4#5#6{#1}%
\long\def\@secondofsix#1#2#3#4#5#6{#2}%
%\long\def\@thirdofsix#1#2#3#4#5#6{#3}%
%\long\def\@fourthofsix#1#2#3#4#5#6{#4}%
%\long\def\@fifthofsix#1#2#3#4#5#6{#5}%

\newcommand\compare@me@ref{ref}%
\newcommand\compare@me@pageref{pageref}%

%% 
\newcommand\ka[3]{%
	\renewcommand\autojosa@name{#1}%
	\renewcommand\autojosa@ref@name{#2}%
	\csname josa@#3\endcsname%
%	\renewcommand\autojosa@type{#3}%
	\IfFileExists{\@currdir \jobname.aux}%
		{\csname\autojosa@name\endcsname\autojosa@ref@name}{}%
	\ifx\autojosa@name\compare@me@ref%
		\k@autojosaref\autojosa@ref@name
	\else\ifx\autojosa@name\compare@me@pageref%
		\k@autojosapageref\autojosa@ref@name
	\fi\fi
	\put@josa\ignorespaces{}%
}	

\def\k@autojosaref#1{%
	\def\tmp@result{%
		\ka@a@ref{#1}%
	}%
	\k@@autojosa
}

\def\k@autojosapageref#1{%
	\def\tmp@result{%
		\ka@a@pageref{#1}%
	}%
	\k@@autojosa
}

\def\k@@autojosa{%
	\count@\tmp@result
	\ifnum\count@<\z@ \count@-\count@ \fi\count2\count@
	\divide\count2 10 \multiply\count2 10 \advance\count@-\count2
	\ifnum\case@fj@sa<6 %% zero,����,�̰�,�Ͱ�,���
	\ifcase\count@%
		   \set@iga{\case@fj@sa}{0}% 0
		\or\set@iga{\case@fj@sa}{0}% 1
		\or\set@iga{\case@fj@sa}{1}% 2
		\or\set@iga{\case@fj@sa}{0}% 3
		\or\set@iga{\case@fj@sa}{1}% 4
		\or\set@iga{\case@fj@sa}{1}% 5
		\or\set@iga{\case@fj@sa}{0}% 6
		\or\set@iga{\case@fj@sa}{0}% 7
		\or\set@iga{\case@fj@sa}{0}% 8
		\or\set@iga{\case@fj@sa}{1}% 9
		\else\set@iga{\case@fj@sa}{1}% else
	\fi
	\else  %% ��/����
	\ifcase\count@%
		   \set@iga{\case@fj@sa}{1}% 0
		\or\set@iga{\case@fj@sa}{0}% 1
		\or\set@iga{\case@fj@sa}{0}% 2
		\or\set@iga{\case@fj@sa}{1}% 3
		\or\set@iga{\case@fj@sa}{0}% 4
		\or\set@iga{\case@fj@sa}{0}% 5
		\or\set@iga{\case@fj@sa}{1}% 6
		\or\set@iga{\case@fj@sa}{0}% 7
		\or\set@iga{\case@fj@sa}{0}% 8
		\or\set@iga{\case@fj@sa}{0}% 9
		\else\set@iga{\case@fj@sa}{1}% else
	\fi
	\fi
}

%%%
%
\def\set@iga#1#2{%
	\ifcase#1%
		   \make@josa@string{}{}% 0
		\or\make@josa@string{��}{��}% 1
		\or\make@josa@string{��}{��}% 2
		\or\make@josa@string{��}{��}% 3
		\or\make@josa@string{��}{��}% 4
		\or\make@josa@string{�̶�}{��}% 5
		\or\make@josa@string{��}{����}% 6
		\else\make@josa@string{��}{��}% else
	\fi
	\ifcase#2%
		   \put@josa{\first@make@josa@string}%
	 	\or\put@josa{\second@make@josa@string}%
	\fi
}

\def\make@josa@string#1#2{%
	\def\first@make@josa@string{#1}%
	\def\second@make@josa@string{#2}%
}

\def\put@josa#1{%
	#1%
}

\def\ka@G@refundefinedtrue{%
  \gdef\ka@@refundefined{%
    \@latex@warning@no@line{There were undefined references}}}
\let\ka@@refundefined\relax

\def\@set@original@ref#1#2#3{%
  \ifx#1\relax
   \protect\ka@G@refundefinedtrue
   \nfss@text{\reset@font\bfseries ??}%
   \@latex@warning{Reference `#3' on page \thepage \space
             undefined}%
  \else
   \expandafter#2#1\null
  \fi
}

\def\ka@a@ref#1{\expandafter\@set@original@ref\csname r@#1\endcsname
	\@sixthofsix{#1}}
%\def\ka@a@ref#1{\expandafter\@setref\csname r@#1\endcsname
%	\@sixthofsix{#1}}
\def\ka@a@pageref#1{\expandafter\@set@original@ref\csname r@#1\endcsname
    \@secondofsix{#1}}

\def\H@refstepcounter#1{\stepcounter{#1}%
    \protected@edef\@currentlabel
       {\csname p@#1\endcsname\csname the#1\endcsname}%
    \protected@edef\@cool@currentlabel
       {\the\csname c@#1\endcsname}%
}

\def\kalabel#1{%
  \@bsphack
  \begingroup
    \let\label\@gobble
    \def\ref{\protect\ref}%
    \edef\@currentlabstr{%
      \expandafter\strip@prefix\meaning\@currentlabelname
    }%
    \protected@write\@auxout{}{%
      \string\newlabel{#1}{%
        {\@currentlabel}%
        {\thepage}%
        {\expandafter\strip@period\@currentlabstr\relax.\relax\@@@}%
        {\@currentHref}{}%
        {\@cool@currentlabel}{}%
      }%
    }%
  \endgroup
  \@esphack
}

%\makeatother
%%\let\label\kalabel
\endinput