#!/usr/bin/env bash


PREFIX='/usr'
INFODESC=\''My Little Ponies for your terminal'\'
SHELL='bash'

completions='completion/bash-completion.sh completion/fish-completion.fish completion/zsh-completion.zsh'
shareDirs='ponies ttyponies quotes'
manFiles='manuals/manpage.6 manuals/manpage.es.6'
licenseFiles='COPYING'



oldInstalledFiles='bin/ponysaytruncater lib/ponysay/truncater lib/ponysay/list.pl lib/ponysay/linklist.pl lib/ponysay/pq4ps lib/ponysay/pq4ps.pl lib/ponysay/pq4ps-list lib/ponysay/pq4ps-list.pl bin/ponysay.py bin/ponythink.py'
oldInstalledDirs='lib/ponysay share/ponies share/ttyponies bin/ponysaylist.pl'
oldCompiledFiles='truncater ponysaytruncater ponysay.py.install'
oldCompiledDirs=''

installedFiles='bin/ponysay bin/ponythink doc/ponysay.pdf share/info/ponysay.info.gz share/info/ponythink.info.gz'
installedDirs='share/ponysay lib/ponysay'
compiledFiles='ponysay.info ponysay.info.gz ponysay.install ponysay.install~'
compiledDirs='quotes'

for man in $manFiles; do
    compiledFiles="$compiledFiles $man.gz"
done
for completion in $completions; do
    thinkfile="${completion%%.*}-think.${completion##*.}"
    compiledFiles="$compiledFiles $completion.install $thinkfile"
done
for file in $licenseFiles; do
    installedFiles="$installedFiles share/licenses/ponysay/$file"
done



function getcompdir()
{
    if   [ "$1" == 'bash' ]; then  echo -n 'bash-completion/completions'
    elif [ "$1" == 'fish' ]; then  echo -n 'fish/completions'
    elif [ "$1" == 'zsh'  ]; then  echo -n 'zsh/site-functions'
    fi
}

function getcompfile()
{
    if   [ "$1" == 'bash' ]; then  echo -n "$2"
    elif [ "$1" == 'fish' ]; then  echo -n "$2"'.fish'
    elif [ "$1" == 'zsh'  ]; then  echo -n '_'"$2"
    fi
}



installCache=' install-cache'
installPdf=''
installInfo=' install-info'
installInfoInstall=' install-info-install'
installMan=' install-man'
installManEs=''
installBash=' install-bash'
installFish=' install-fish'
installZsh=' install-zsh'

for arg in "$@"; do
    opt="${arg%%=*}"
    val="${arg##*=}"
    
    if [ "$opt" = '--prefix' ]; then
	PREFIX="$val"
    elif [ "$opt" = '--info-desc' ]; then
	INFODESC=\'"$(sed -e s/\'/\''\\'\'\'/g <<<"$val")"\'
    elif [ "$opt" = '--shell' ]; then
	SHELL="$val"
    
    elif [ "$opt" = '--without-shared-cache' ]; then  installCache=''
    elif [ "$opt" = '--with-pdf'             ]; then  installPdf=' install-pdf'
    elif [ "$opt" = '--without-info'         ]; then  installInfo=''
    elif [ "$opt" = '--without-info-install' ]; then  installInfoInstall=''
    elif [ "$opt" = '--without-man'          ]; then  installMan=''
    elif [ "$opt" = '--with-man-es'          ]; then  installManEs=' install-man-es'
    elif [ "$opt" = '--without-bash'         ]; then  installBash=''
    elif [ "$opt" = '--without-fish'         ]; then  installFish=''
    elif [ "$opt" = '--without-zsh'          ]; then  installZsh=''
    
    elif [ "$opt" = "--everything" ]; then
	installCache=' install-cache'
	installPdf=' install-pdf'
	installInfo=' install-info'
	installInfoInstall=' install-info-install'
	installMan=' install-man'
	installManEs=' install-man-es'
	installBash=' install-bash'
	installFish=' install-fish'
	installZsh=' install-zsh'
    
    else
	echo "$0: error: option not recongised: $arg" >&2
    fi
done

if [ "$installInfo" = '' ]; then
    installInfoInstall=''
fi

compileMethods='core quotes manpages ponysaycompletion ponythinkcompletion'
installMethods='install-min'"$installCache$installPdf$installInfo$installInfoInstall$installMan$installManEs$installBash$installFish$installZsh"
if [ ! "$installInfo" = '' ]; then
    compileMethods="$compileMethods infomanual"
fi



function correctPrefix()
{
    for file in "$@"; do
	echo -en '\t'
	echo 'sed -e '\''s/'\''\'\'''\''\/usr\//'\''\'\''"$(SED_PREFIX)"'\''\//g'\'' < "'"$file"'" > "'"$file"'.install"'
    done
}

function gzCompress()
{
    for file in "$@"; do
	echo -en '\t'
	echo 'gzip -9 -f < "'"$file"'" > "'"$file"'".gz'
    done
}

function thinkCompletion()
{
    for file in "$@"; do
	echo -en '\tsed'
	echo -n ' -e '\''s/ponysay/ponythink/g'\'
	echo -n ' < "'"$file"'.install" | sed'
	echo -n ' -e '\''s/\/ponythink\//\/ponysay\//g'\'
	echo -n ' -e '\''s/\\\/ponythink\\\//\\\/ponysay\\\//g'\'
	thinkfile="${file%%.*}-think.${file##*.}"
	echo ' > "'"$thinkfile"'"'
    done
}



echo "PREFIX = $PREFIX, edit with option --prefix=PREFIX"
echo "INFODESC = $INFODESC, edit with option --info-desc=INFODESC"
echo "SHELL = $SHELL, edit with option --shell=PREFIX"
echo
[ ! "$installCache"        = '' ] && echo "Installing shared cache, add option --without-shared-cache to skip"
[ ! "$installPdf"          = '' ] && echo "Installing PDF manual"
[ ! "$installInfo"         = '' ] && echo "Installing info manual, add option --without-info to skip"
[ ! "$installInfoInstall"  = '' ] && echo "Installing info manual with info-install, add option --without-info to skip"
[ ! "$installMan"          = '' ] && echo "Installing English manpage manual, add option --without-man to skip"
[ ! "$installManEs"        = '' ] && echo "Installing Spanish manpage manual"
[ ! "$installBash"         = '' ] && echo "Installing autocompletion for GNU Bash, add option --without-bash to skip"
[ ! "$installFish"         = '' ] && echo "Installing autocompletion for fish, add option --without-fish to skip"
[ ! "$installZsh"          = '' ] && echo "Installing autocompletion for zsh, add option --without-zsh to skip"
echo
[ ! "$installCache"        = '' ] || echo "Skipping shared cache"
[ ! "$installPdf"          = '' ] || echo "Skipping PDF manual, add option --with-pdf to install"
[ ! "$installInfo"         = '' ] || echo "Skipping info manual"
[ ! "$installInfoInstall"  = '' ] || echo "Skipping info manual installation with info-install"
[ ! "$installMan"          = '' ] || echo "Skipping English manpage manual"
[ ! "$installManEs"        = '' ] || echo "Skipping Spanish manpage manual, add option --with-man-es to install"
[ ! "$installBash"         = '' ] || echo "Skipping autocompletion for GNU Bash"
[ ! "$installFish"         = '' ] || echo "Skipping autocompletion for fish"
[ ! "$installZsh"          = '' ] || echo "Skipping autocompletion for zsh"
echo



function makeMakefile()
{
    echo 'SHELL='\'"$SHELL"\'
    echo 'PREFIX='\'"$PREFIX"\'
    echo 'INSTALLDIR="$(DESTDIR)$(PREFIX)"'
    echo 'SED_PREFIX=$$(sed -e '\''s/\//\\\//g'\'' <<<$(PREFIX))'
    echo
    echo 'default: '"$compileMethods"
    echo
    echo 'all: core quotes manpages infomanual ponysaycompletion ponythinkcompletion'
    echo
    
    echo "core:"
    correctPrefix 'ponysay'
    echo -en '\t' ; echo 'if (( `env python --version 2>&1 | cut -d " " -f 2 | cut -d "." -f 1` < 3 )); then \'
    echo -en '\t' ; echo '    mv "ponysay.install" "ponysay.install~" ;\'
    echo -en '\t' ; echo '    sed -e '\''s/bin\/env python/bin\/env python3/'\'' < "ponysay.install~" > "ponysay.install" ;\'
    echo -en '\t' ; echo 'fi'
    echo -en '\t' ; echo 'chmod 755 "ponysay.install"'
    echo
    
    echo "quotes:"
    echo -en '\t' ; echo 'mkdir -p quotes'
    echo -en '\t' ; echo 'for ponies in $$(cat ponyquotes/ponies); do                       \'
    echo -en '\t' ; echo '    for pony in $$(echo $$ponies | sed -e '\''s/+/ /g'\''); do    \'
    echo -en '\t' ; echo '        echo '\''Generating quote files for '\''"$$pony";         \'
    echo -en '\t' ; echo '        for file in $$(ls "ponyquotes/" | grep "$$pony\\.*"); do  \'
    echo -en '\t' ; echo '            if [ -f "ponyquotes/$$file" ]; then                   \'
    echo -en '\t' ; echo '                cp "ponyquotes/"$$file "quotes/"$$ponies'\''.'\''$$(echo $$file | cut -d '\''.'\'' -f 2)  \'
    echo -en '\t' ; echo ';fi;done;done;done'
    echo
    
    echo 'manpages:'
    gzCompress $manFiles
    echo
    
    echo 'infomanual:'
    echo -en '\t'
    echo 'makeinfo "manuals/ponysay.texinfo"'
    gzCompress 'ponysay.info'
    echo
    
    echo 'ponysaycompletion:'
    correctPrefix $completions
    echo
    
    echo 'ponythinkcompletion: ponysaycompletion'
    thinkCompletion $completions
    echo
    
    echo 'install-min: core quotes'
    echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/share/ponysay/"'
    for dir in $shareDirs; do
	echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/share/ponysay/'"$dir"'"'
	echo -en '\t' ; echo 'cp -P "'"$dir"'/"*.* "$(INSTALLDIR)/share/ponysay/'"$dir"'/"'
    done
    echo
    echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/bin/"'
    echo -en '\t' ; echo 'install "ponysay.install" "$(INSTALLDIR)/bin/ponysay"'
    echo -en '\t' ; echo 'ln -sf  "ponysay"         "$(INSTALLDIR)/bin/ponythink"'
    echo
    echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/share/licenses/ponysay/"'
    for file in $licenseFiles; do
	echo -en '\t'
	echo 'install "'"$file"'" "$(INSTALLDIR)/share/licenses/ponysay/'"$file"'"'
    done
    echo
    
    echo 'install-cache:'
    echo -en '\t' ; echo 'if [ -d "/var/cache/ponysay" ]; then  rm -r "/var/cache/ponysay"; fi'
    echo -en '\t' ; echo 'mkdir -p "$(DESTDIR)/var/cache/ponysay/"'
    echo -en '\t' ; echo 'chmod 777 "$(DESTDIR)/var/cache/ponysay/"'
    echo
    
    for completion in $completions; do
	shell="${completion##*/}"
	shell="${shell%%-*}"
	thinkfile="${completion%%.*}-think.${completion##*.}"
	shelldir='share/'"$(getcompdir $shell)"'/'
	echo 'install-'"$shell"': ponysaycompletion ponythinkcompletion'
	echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/'"$shelldir"'"'
	echo -en '\t' ; echo 'install "'"$completion"'" "$(INSTALLDIR)/'"$shelldir$(getcompfile $shell ponysay)"'"'
	echo -en '\t' ; echo 'install "'"$thinkfile"'"  "$(INSTALLDIR)/'"$shelldir$(getcompfile $shell ponythink)"'"'
	echo
	installedFiles="$installedFiles $shelldir$(getcompfile $shell ponysay) $shelldir$(getcompfile $shell ponythink)"
    done
    
    echo 'install-pdf:'
    echo -en '\t'
    echo 'install "ponysay.pdf" "$(INSTALLDIR)/doc/ponysay.pdf"'
    echo
    
    echo 'install-info: infomanual'
    echo -en '\t' ; echo 'mkdir -p "$(INSTALLDIR)/share/info"'
    echo -en '\t' ; echo 'install "ponysay.info.gz" "$(INSTALLDIR)/share/info/ponysay.info.gz"'
    echo -en '\t' ; echo 'install "ponysay.info.gz" "$(INSTALLDIR)/share/info/ponythink.info.gz"'
    echo
    
    echo 'install-info-install: install-info'
    echo -en '\t' ; echo 'install-info --dir-file="$(INSTALLDIR)/share/info/dir" --entry="Miscellaneous" --description='"$INFODESC"' "$(INSTALLDIR)/share/info/ponysay.info.gz"'
    echo -en '\t' ; echo 'install-info --dir-file="$(INSTALLDIR)/share/info/dir" --entry="Miscellaneous" --description='"$INFODESC"' "$(INSTALLDIR)/share/info/ponythink.info.gz"'
    echo
    
    for man in $manFiles; do
	lang="${man##*manpage}"
	lang="${lang%%.6}"
	mandir="${lang/\.//}"
	echo 'install-man'"${lang/\./-}"': manpages'
	echo -en '\t' ; echo 'mkdir -p               "$(INSTALLDIR)/share/man'"$mandir"'/man6"'
	echo -en '\t' ; echo 'install "'"$man"'.gz"  "$(INSTALLDIR)/share/man'"$mandir"'/man6/ponysay.6.gz"'
	echo -en '\t' ; echo 'ln -sf  "ponysay.6.gz" "$(INSTALLDIR)/share/man'"$mandir"'/man6/ponythink.6.gz"'
	echo
        installedFiles="$installedFiles share/man$mandir/man6/ponysay.6.gz share/man$mandir/man6/ponythink.6.gz"
    done
    
    echo
    echo 'install: '"$installMethods"
    echo -en '\t' ; echo '@echo -e \\n\\n\'
    cat <<EOF
'/--------------------------------------------------\\\\\n'\\
'|   ___                                            |\\n'\\
'|  / (_)        o                                  |\\n'\\
'|  \\__   _  _      __                              |\\n'\\
'|  /    / |/ |  | /  \\_|   |                       |\\n'\\
'|  \\___/  |  |_/|/\\__/  \\_/|/                      |\\n'\\
'|              /|         /|                       |\\n'\\
'|              \\|         \\|                       |\\n'\\
'|   ____                                           |\\n'\\
'|  |  _ \\  ___   _ __   _   _  ___   __ _  _   _   |\\n'\\
'|  | |_) |/ _ \\ | '\\''_ \\ | | | |/ __| / _\` || | | |  |\n'\\
'|  |  __/| (_) || | | || |_| |\\__ \\| (_| || |_| |  |\\n'\\
'|  |_|    \\___/ |_| |_| \\__, ||___/ \\__,_| \\__, |  |\\n'\\
'|                       |___/              |___/   |\\n'\\
'\\\\--------------------------------------------------/'
EOF
    echo -en '\t' ; echo '@echo dummy | ./ponysay.install -f ./`if [[ "$$TERM" = "linux" ]]; then echo ttyponies; else echo ponies; fi`/pinkiecannon.pony | tail --lines=30 ; echo -e \\n'
    echo
    
    echo 'uninstall:'
    echo -en '\t' ; echo 'if [ -d "$/var/cache/ponysay" ]; then  rm -rf "$/var/cache/ponysay"; fi'
    for old in $installedDirs; do
	echo -en '\t'
	echo 'if [ -d "$(INSTALLDIR)/'"$old"'" ]; then  rm -rf "$(INSTALLDIR)/'"$old"'"; fi'
    done
    for old in $installedFiles; do
	echo -en '\t'
	echo 'if [ -f "$(INSTALLDIR)/'"$old"'" ]; then  unlink "$(INSTALLDIR)/'"$old"'"; fi'
    done
    echo
    
    echo 'clean:'
    for old in $compiledDirs; do
	echo -en '\t'
	echo 'if [ -d "'"$old"'" ]; then  rm -rf "'"$old"'"; fi'
    done
    for old in $compiledFiles; do
	echo -en '\t'
	echo 'if [ -f "'"$old"'" ]; then  rm -f "'"$old"'"; fi'
    done
    echo
    
    echo 'uninstall-old:'
    for old in $oldInstalledDirs; do
	echo -en '\t'
	echo 'if [ -d "$(INSTALLDIR)/'"$old"'" ]; then  rm -rf "$(INSTALLDIR)/'"$old"'"; fi'
    done
    for old in $oldInstalledFiles; do
	echo -en '\t'
	echo 'if [ -f "$(INSTALLDIR)/'"$old"'" ]; then  unlink "$(INSTALLDIR)/'"$old"'"; fi'
    done
    echo
    
    echo 'clean-old:'
    for old in $oldCompiledDirs; do
	echo -en '\t'
	echo 'if [ -d "'"$old"'" ]; then  rm -rf "'"$old"'"; fi'
    done
    for old in $oldCompiledFiles; do
	echo -en '\t'
	echo 'if [ -f "'"$old"'" ]; then  rm -f "'"$old"'"; fi'
    done
    echo
    
    cat <<EOF
## Scripts for maintainers

ttyponies:
	mkdir -p "ttyponies"
	for pony in \$\$(ls --color=no "ponies/"); do                                                   \\
	    echo "building ttypony: \$\$pony"                                                          ;\\
	    if [ "\`readlink "ponies/\$\$pony"\`" = '' ]; then                                          \\
	        ponysay2ttyponysay < "ponies/\$\$pony" | tty2colourfultty -c 1 > "ttyponies/\$\$pony"  ;\\
		git add "ttyponies/\$\$pony"                                                           ;\\
	    else                                                                                        \\
                ln -sf \`readlink "ponies/\$\$pony"\` "ttyponies/\$\$pony"                             ;\\
		git add "ttyponies/\$\$pony"                                                           ;\\
	    fi                                                                                         ;\\
	done

pdfmanual:
	texi2pdf "manuals/ponysay.texinfo"
	git add  "manuals/ponysay.texinfo" "ponysay.pdf"
	for ext in \`echo aux cp cps fn ky log pg toc tp vr\`; do                \\
	    (if [ -f "ponysay.\$\$ext" ]; then unlink "ponysay.\$\$ext"; fi);    \\
	done
	if [ -d "ponysay.t2d" ]; then rm -r "ponysay.t2d"; fi

EOF
}

makeMakefile > Makefile