#!/bin/bash

# split: an image splitting tool
#   this  script splits an image
#       into horizontal pieces
#  vers. 1.2  October 2011
#  by sdjf 
#
# usage:  split [pieces] file
# amount can be 2, 3, 4, 6
#
# dependency: must have installed crop.sh
#
# for examples and links to most recent 
# versions of both split and crop.sh, go to
# http://sdjf.esmartdesign.com/scripts/crop.html
#
# I have done my best to debug this, but you
# use the script at your own risk. Be sure to
# make backups of irreplaceable images.
#
# Problems? Write sdjf at http://www.oesf.org/forum
# 
#
# Script licensed under the GPL.
# Permission granted to copy, modify, and use
# it, but please give credit to sdjf, the
# original author, and report any enhancements
# or bug fixes or so I can share them.
   
usage () {
echo
echo "Usage: split [location]  [amount] file"
echo  "amount:   [half|third|fourth|sixth]"
echo  "file:     full filename for input file"   
echo
}

if ! type ./crop.sh>& /dev/null;then
 echo "ERROR: Missing ./crop.sh"
 echo  "       Please install crop.sh to"
 echo  "       same directory that split is in,"
 echo  "       \"cwd\" to that directory, or"
 echo  "       or create an appropriate symlink"
 echo  "For more information about both scripts, see"
 echo  "http://sdjf.esmartdesign.com/scripts/crop.sh"
  exit
fi

if [ $# -eq "0" ] # Script invoked with no args
then
echo
echo "ERROR: no arguments given"
usage
exit
fi

#  get extension
   type=${2##*.}   

# do initial splitting
case $1 in
2|4|half|two|four|fourth|fourths) ./crop.sh top half $2
        ./crop.sh bottom half $2
;;
3|6|third|sixth|six|three) ./crop.sh top third $2
               ./crop.sh middle third $2
               ./crop.sh bottom third $2
;;
*) echo ERROR: Amount for splitting \'$1\' unrecognize
   echo
usage
exit
;;
esac

# complete splitting or cleanup and exit
case $1 in
2|3|two|three|half|third) echo Completed splitting $2 into $1
exit
;;
4|four|fourth|fourths) ./crop.sh top fourth $2
./crop.sh bottom fourth $2
./crop.sh bottom half ${2/.$type/-top-half.$type} 
./crop.sh top half  ${2/.$type/-bot-half.$type}
# pretty up file names
mv ${2/.$type/-top-half-bot-half.$type} ${2/.$type/-2.$type}
mv ${2/.$type/-bot-half-top-half.$type} ${2/.$type/-3.$type}
rm ${2/.$type/-top-half.$type} ${2/.$type/-bot-half.$type}
;;
6|six|sixth|sixths) 
./crop.sh top sixth $2
./crop.sh bottom half ${2/.$type/-top-3rd.$type}

./crop.sh top half ${2/.$type/-mid-3rd.$type}
./crop.sh bottom half ${2/.$type/-mid-3rd.$type} 
./crop.sh top half ${2/.$type/-bot-3rd.$type}
./crop.sh bottom 6th $2
# pretty up file names
mv ${2/.$type/-top-6th.$type} ${2/.$type/-1.$type} 
mv  ${2/.$type/-top-3rd-bot-half.$type}  ${2/.$type/-2.$type}
mv ${2/.$type/-mid-3rd-top-half.$type} ${2/.$type/-3.$type}

mv ${2/.$type/-mid-3rd-bot-half.$type} ${2/.$type/-4.$type}
mv ${2/.$type/-bot-3rd-top-half.$type} ${2/.$type/-5.$type} 
mv ${2/.$type/-top-6th.$type} ${2/.$type/-6.$type}

rm  ${2/.$type/-top-3rd.$type} ${2/.$type/-mid-3rd.$type} ${2/.$type/-bot-3rd.$type} 
;;
esac
echo Completed splitting $2 into $1
exit

