#! /bin/sh

###########################################################################
#
# SCRIPT to convert and generate the TIGCC Tools Suite on UN*X Systems
#	modified by John David Ratliff 2002-07-21
#	modified by Kevin Kofler 2002-11-15
#
# v 0.2 (tested on Suse 6.x and Suse 7.x)
# v 0.2.1 (fixed sh compatibility, removed GNU extentions)
# v 0.2.2 (now honors CC and CFLAGS environment variables)
#
###########################################################################
#
# This script generates a directory called linuxsrc and linuxbin.
# Unix file versions of all files in the normal src directory tree are
# generated in directory linuxsrc.
# Afterwards the pctools are build and the executables are stored in
# directory linuxbin.
#
# To use the TIGCC Tools Suite please add the linuxbin directory to
# your PATH.
#
# Due to the fact that the commandline parameter style of command
# recode is incompatible between Linux 6.x and 7.x installations 
# recode cannot be used to convert DOS files to UNIX files.
# Instead of recode a dirty "cat -vte" in combination with
# 2 sed filters is used which IS compatible between Linux 6.x and
# 7.x
#
# I cannot understand why programmers are that stupid to change
# the main commandline parameter style of a tool like recode.
# This way the tool disqualify itself for compatible scripts.
#
###########################################################################

# check if CC is set, otherwise default to gcc
if [ -z "$CC" ]
 then CC=gcc
fi
echo ====== makelinux.sh: using compiler: $CC ========

# check if CFLAGS is set, otherwise default to '-Os -s'
if [ -z "$CFLAGS" ]
 then CFLAGS='-Os -s'
fi
# The line is one off by intention, to be aligned for the default CFLAGS
echo ====== using compiler flags: $CFLAGS =============


directories=`ls src`

echo ====== generating linuxbin directory ============

if [ -d linuxbin ]; then
   echo directory linuxbin does already exists
else
   echo generating directory linuxbin
   mkdir linuxbin
fi

echo ====== generating linuxsrc directory tree =======
if [ -d linuxsrc ]; then
   echo directory linuxsrc does already exists
else
   echo generating directory linuxsrc
   mkdir linuxsrc
fi

for i in $directories
do
   if [ -d src/$i ]; then
      if [ -d linuxsrc/$i ]; then
          echo directory linuxsrc/$i does already exists
      else
          echo generating directory linuxsrc/$i
          mkdir linuxsrc/$i          
      fi
   fi
done

echo ====== converting source files ==================

for i in $directories
do
    if [ -d src/$i ]; then
        echo ------ processing directory src/$i -------
        cd src/$i
        allfiles=`ls *.c *.h 2>/dev/null`

        for j in $allfiles 
        do 
            echo converting src/$i/$j ...
            #cat $j | recode ibmpc..lat1 > ../../linuxsrc/$i/$j
            cat -vte $j | sed 's/\^M\$//g' | sed 's/\^I/	/g' > ../../linuxsrc/$i/$j
        done

        cd ../..

    fi
done

echo ====== generating linux binaries ==================
cd linuxsrc/pctools
allfiles=`ls tt*.c`
for j in $allfiles 
do 
    echo building $j ...
    outname=`echo $j | cut -f1 -d.`
    $CC $CFLAGS -o ../../linuxbin/$outname $j
done
cd ../..

