#!/bin/sh

# ansi colour escape sequences
BRIGHT='[1m'
RESET='[0m'
BLINK='[5m'
BG_BLK='[40m'
BG_RED='[41m'
BG_GRN='[42m'
BG_YLO='[43m'
BG_BLU='[44m'
BG_PUR='[45m'
BG_CYN='[46m'
BG_WHT='[47m'
FG_BLK='[30m'
FG_RED='[31m'
FG_GRN='[32m'
FG_YLO='[33m'
FG_BLU='[34m'
FG_PUR='[35m'
FG_CYN='[36m'
FG_WHT='[37m'

# ansi shading chars
G1=' '
G2=''
G3=''
G4=''
G5=''

# console control chars
CLS='[H[J'

# cursor control chars
C_UP='[A'
C_DOWN='[B'
C_LEFT='[D'
C_RIGHT='[C'

# workarounds
SPC=' '

# Functions

# Syntax: gotopos x y
gotopos() {
  X=$1
  Y=$2
  printf '['$Y';'$X'H'
}

# Syntax: clearterm x y width height
clearterm() {
  X=$1
  Y=$2
  WIDTH=$3
  HEIGHT=$4
  LINE="                                                                                                                                                                                                                                               "
  LINE2=`printf "$LINE" | cut -c 1-$WIDTH`
  LINE=""
  LINE3=`printf "$LINE" | cut -c 1-$WIDTH`
  LINE=""
  LINE4=`printf "$LINE" | cut -c 1-$WIDTH`
  LINE=""
  LINE5=`printf "$LINE" | cut -c 1-$WIDTH`
  LINE=""
  LINE6=`printf "$LINE" | cut -c 1-$WIDTH`
  L=0
  YY=$Y
  while [ $L -lt $HEIGHT ]; do
    if [ $YY -eq 1 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_WHT""$LINE6\n"; fi
    if [ $YY -eq 2 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_WHT""$LINE5\n"; fi
    if [ $YY -eq 3 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_WHT""$LINE4\n"; fi
    if [ $YY -eq 4 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_WHT""$LINE3\n"; fi
    if [ $YY -eq 5 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_WHT""$LINE2\n"; fi
    if [ $YY -eq 6 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_BLK""$LINE3\n"; fi
    if [ $YY -eq 7 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_BLK""$LINE4\n"; fi
    if [ $YY -eq 8 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_BLK""$LINE5\n"; fi
    if [ $YY -gt 8 ]; then 
      if [ $YY -lt 20 ]; then
       printf '\033['$YY';'$X'H'"$RESET$BRIGHT$BG_WHT$FG_BLK""$LINE6\n"; fi; fi
    if [ $YY -eq 20 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_BLK$FG_BLK""$LINE5\n"; fi
    if [ $YY -eq 21 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_BLK$FG_BLK""$LINE4\n"; fi
    if [ $YY -eq 22 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_BLK$FG_BLK""$LINE3\n"; fi
    if [ $YY -eq 23 ]; then printf '['$YY';'$X'H'"$RESET$BRIGHT$BG_BLK$FG_BLK""$LINE2\n"; fi
    L=$[ $L +1 ]
    YY=$[ $YY +1 ]
  done
  printf $RESET
  gotopos 1 1
}

# Syntax: popupbox x y 'text'
popupbox() {
  TEXT=$3
  X=$1
  Y=$2
  WIDTH=${#TEXT}
  POPUP_LAST_X=$X
  POPUP_LAST_Y=$Y
  POPUP_LAST_WIDTH=$[ $WIDTH + 5 ]
  POPUP_LAST_HEIGHT=4
  XX=$[ $X + 2 ]
  XXX=$[ $X + 1 ]
  YY=$[ $Y + 1 ]
  YYY=$[ $Y + 2 ]
  YYYY=$[ $Y + 3 ]
  WW=$[ $WIDTH + 4 ]
  HH=$[ $HEIGHT + 2 ]
  TOP_BAR="۲                                                                       "
  TOP_BAR2=`printf "$TOP_BAR" | cut -c 1-$WW`
  gotopos $X $Y
  printf "$FG_CYN$BG_CYN$BRIGHT$TOP_BAR2$RESET"
  gotopos $X $YY
  printf "$FG_CYN$BG_CYN$BRIGHT$G5$RESET$BRIGHT$FG_WHT$BG_BLU %-$WIDTH.999s $RESET$FG_CYN$BG_BLU$G5$BG_BLK $RESET" "$TEXT"
  gotopos $X $YYY
  printf "$FG_CYN$BG_CYN$BRIGHT$TOP_BAR2$BG_BLK $RESET"
  gotopos $XXX $YYYY
  printf "$FG_BLK$BG_BLK$TOP_BAR2$RESET"
}

popdownbox() {
  clearterm $POPUP_LAST_X $POPUP_LAST_Y $POPUP_LAST_WIDTH $POPUP_LAST_HEIGHT
}

message() {
  TEXT=$3
  X=$1
  Y=$2
  popupbox $X $Y "$TEXT"
  gotopos 1 23
  printf $BRIGHT$FG_RED$BG_BLK"Press Enter to Continue"$RESET
  gotopos 1 1
  read DUMMY
  clearterm 1 23 50 1
  popdownbox
  clearterm 1 1 1 1
}

askyn() {
  TEXT=$3
  X=$1
  Y=$2
  popupbox $X $Y "$TEXT"
  gotopos 1 23
  printf $BRIGHT$FG_RED$BG_BLK"Press y or n then Enter"$RESET
  gotopos 1 1
  read ANS
  clearterm 1 23 50 1
  popdownbox
  clearterm 1 1 1 1
}

clearterm 1 1 80 23
popupbox 25 3 "Imlib DR-0.3 Install Script"
sleep 1
popupbox 20 10 "You will now be asked some questions."
sleep 2
popdownbox
askyn 5  14 "Do You have Imagemagick installed?"
N=`which convert | grep -v " no "`
if [ $ANS = "y" ]; then
  if [ ! -n "$N" ]; then
    message 2 13 "It seems the convert command is not in the path for /bin/sh"
    message 2 16 "You need to ensure convert is in the path for /bin/sh"
    clear
    exit
  fi
else
  message 2 14 "You will need it installed to load images other than PPM (like JPG)"
fi
N=`which xmkmf | grep -v " no "`
if [ ! -n "$N" ]; then
  message 4 15 "Apparently you don't have the xmkmf command. You need it." 
  message 17 12 "You need it, and possibly GNU make installed aswell."
  clear
  exit
fi
N=`which make | grep -v " no "`
if [ ! -n "$N" ]; then
  message 15 11 "What!!! Yon't have make installed?" 
  message 8 18 "You need it, and possibly even GNU make."
  clear
  exit
fi

message 10 11 "I am about to make Imlib." 
message 4 14 "If you are on a Linux system, you may wish to run ldconfig later." 

# compile & install
clear
make clean
xmkmf -a
make install

