#!/bin/sh
# Copyright 1994, 1998  Patrick Volkerding, Moorhead, Minnesota USA 
# Modified by Scott Barnes 2000
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Wed Mar 18 15:32:33 CST 1998
# Patched to avoid possible symlink attacks in /tmp.

make_install_script() {
 COUNT=1
 LINE="`sed -n "$COUNT p" $1`"
 while [ ! "$LINE" = "" ]; do
  LINKGOESIN="`echo "$LINE" | cut -f 1 -d " "`" 
  LINKGOESIN="`dirname $LINKGOESIN`" 
  LINKNAMEIS="`echo "$LINE" | cut -f 1 -d ' '`"
  LINKNAMEIS="`basename "$LINKNAMEIS"`"
  LINKPOINTSTO="`echo "$LINE" | cut -f 3 -d ' '`"
  echo "( cd .$LINKGOESIN ; rm -rf $LINKNAMEIS )"
  echo "( cd .$LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS )"
  COUNT=`expr $COUNT + 1`
  LINE="`sed -n "$COUNT p" $1`"
 done
}

usage() {
 cat << EOF

Usage: makesymins

If symbolic links exist, they will be removed
and an installation script will be made to recreate them later. This script
will be called "doinst.sh". You may add any of your own ash-compatible
shell scripts to this file and rebuild the package if you wish.

EOF
}

TMP=/tmp # This can be a hole, but I'm going to be careful about file
         # creation in there, so don't panic. :^)

# These tests, while by no means exhaustive, will give a usage message
# and exit with a wide range of bad input.

echo "#Symbolic Recreate Script" > syminst.sh
echo
echo "Searching for symbolic links:"
# Get rid of possible pre-existing trouble:
rm -rf $TMP/iNsT-a.$$
touch $TMP/iNsT-a.$$
find . -type l -exec ls -l {} \; | cut -b58- | tee $TMP/iNsT-a.$$
#if [ ! "`filesize $TMP/iNsT-a.$$`" = "0" ]; then
 echo
 echo "Making symbolic link creation script:"
 make_install_script $TMP/iNsT-a.$$ | tee doinst.sh
#fi

echo "Would you like to make this stuff the install script for this package"
echo -n "and remove the symbolic links ([y]es, [n]o)? "
read SCRIPTADD;
if [ "$SCRIPTADD" = "y" ]; then
  if [ -r syminst.sh ]; then
   UPDATE="t"
   cat doinst.sh >> syminst.sh
  else
   mkdir install
   cat doinst.sh > syminst.sh
  fi
  echo
  echo "Removing symbolic links:"
  find . -type l -exec rm -v {} \;
  echo  
  if [ "$UPDATE" = "t" ]; then
  	echo "Updating your ./syminst.sh..."
  else
  	echo "Creating your new ./syminst.sh..."
  fi
fi
rm -f doinst.sh $TMP/iNsT-a.$$
echo
echo "./syminst.sh creation complete."
echo "It is recommended that you make these lines to your installation script for distribution."
echo
