function fixlinks() { # $1 = install root
  if [ ! -L $1/lib/libncurses.so.$VERSION  -a -f $1/lib/libncurses.so.$VERSION  ]; then
    # hardcode this link for bash of an upgrade from not this spell
    ln -fs libncurses.so.$VERSION   $1/lib/libncursesw.so.$MVERSION
  fi &&
  if [ ! -L $1/lib/libncursesw.so.$VERSION -a -f $1/lib/libncursesw.so.$VERSION ]; then
    # hardcode this link for bash an upgrade from not this spell
    ln -fs libncursesw.so.$VERSION  $1/lib/libncurses.so.$MVERSION
  fi
}

make_normal                                     &&

# setup staging
local TMPSTAGING=/tmp/sorcery/ncurses.$$        &&
mkdir -p $TMPSTAGING/lib                        &&
MVERSION=${VERSION/.*/}                         &&

# install libs to staging
make  install.libs   DESTDIR="$TMPSTAGING"      &&
{ # setting these off to the side -- they are protected as well
  cp $INSTALL_ROOT/lib/lib*curses* $TMPSTAGING/lib/; true; # but we don't care if there is nothing there (e.g. chroot install)
}  &&  ldconfig  &&  ldconfig $TMPSTAGING/lib   &&
fixlinks                      $TMPSTAGING       &&
ldconfig  &&  ldconfig        $TMPSTAGING/lib   &&

# install to real install root
make  install.libs  DESTDIR="$INSTALL_ROOT"     &&
fixlinks                     $INSTALL_ROOT      &&

# get rid of the old version of libncurses.so (otherwise tic will complain during 5.4 -> 5.5)
{ 
mv $INSTALL_ROOT/lib/libncurses.so.$VERSION      \
   $INSTALL_ROOT/lib/libncurses.so-bak.$VERSION  &&
mv $INSTALL_ROOT/lib/libncurses.so.$MVERSION      \
   $INSTALL_ROOT/lib/libncurses.so-bak.$MVERSION &&
{ rm $INSTALL_ROOT/lib/libncurses.so.* ; true; } && # we might not be using an old version
mv $INSTALL_ROOT/lib/libncurses.so-bak.$VERSION  \
   $INSTALL_ROOT/lib/libncurses.so.$VERSION      &&
mv $INSTALL_ROOT/lib/libncurses.so-bak.$MVERSION \
   $INSTALL_ROOT/lib/libncurses.so.$MVERSION     &&
ldconfig; true # but we don't care if there is nothing there (chroot install)
} &&

# get rid of the old version of libncursesw.so  Bug #13408
{ 
mv $INSTALL_ROOT/lib/libncursesw.so.$VERSION      \
   $INSTALL_ROOT/lib/libncursesw.so-bak.$VERSION  &&
mv $INSTALL_ROOT/lib/libncursesw.so.$MVERSION      \
   $INSTALL_ROOT/lib/libncursesw.so-bak.$MVERSION &&
{ rm $INSTALL_ROOT/lib/libncursesw.so.* ; true; } && # we might not be using an old version
mv $INSTALL_ROOT/lib/libncursesw.so-bak.$VERSION  \
   $INSTALL_ROOT/lib/libncursesw.so.$VERSION      &&
mv $INSTALL_ROOT/lib/libncursesw.so-bak.$MVERSION \
   $INSTALL_ROOT/lib/libncursesw.so.$MVERSION     &&
ldconfig; true # but we don't care if there is nothing there (chroot install)
} &&

# Do not install the libs again. Causes problems.  Bug #13408

sedit 's/cd ncurses /# cd ncurses /' Makefile &&

# really install it -- all the libs should be in place
make  install       DESTDIR="$INSTALL_ROOT"     &&

if [[ $UTF8 == y ]]; then
  # links from non-utf-8 ncurses extras to utf-8 ncurses extras
  for  i  in  libformw  libmenuw  libncursesw  libpanelw  libcursesw
  do
    ln  -sf  $i.a                   \
             $INSTALL_ROOT/lib/${i/w/}.a              &&
    ln  -sf  $i.so                  \
             $INSTALL_ROOT/lib/${i/w/}.so             &&
    ln  -sf  $i.so.${MVERSION}      \
             $INSTALL_ROOT/lib/${i/w/}.so.${MVERSION} &&
    ln  -sf  $i.so.${VERSION}       \
             $INSTALL_ROOT/lib/${i/w/}.so.${VERSION}  &&
    ln  -sf  ${i}_g.a               \
             $INSTALL_ROOT/lib/${i/w/}_g.a
  done  &&

  # links from plain curses ${i//[wn]/} and non-utf-8 ncurses ${i/w/} to utf-8 ncurses $i
  for  i  in  libncursesw.a libncursesw.so libncursesw.so.${MVERSION} libncursesw.so.${VERSION} libncursesw_g.a
  do
    ln  -sf  $i                     \
             $INSTALL_ROOT/lib/${i//[wn]/}            &&
    ln  -sf  $i                     \
             $INSTALL_ROOT/lib/${i/w/}
  done

else

  # links from plain curses ${i//[wn]/} and utf-8 ncurses $i to non-utf-8 ncurses ${i/w/}
  for  i  in  libncursesw.a libncursesw.so libncursesw.so.${MVERSION} libncursesw.so.${VERSION} libncursesw_g.a
  do
    ln  -sf  ${i/w/}                \
             $INSTALL_ROOT/lib/${i//[wn]/}            &&
    ln  -sf  ${i/w/}                \
             $INSTALL_ROOT/lib/$i
  done

fi &&
ldconfig                                 &&
rm -rf "/tmp/sorcery/ncurses.$$"            # hardcode for safety
