File: debstd

package info (click to toggle)
debmake 3.5.18
  • links: PTS
  • area: main
  • in suites: slink
  • size: 408 kB
  • ctags: 21
  • sloc: sh: 785; makefile: 217
file content (781 lines) | stat: -rwxr-xr-x 21,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/bin/bash
#
# Script to be called from debian/rules to setup all the debian specifc
# required files
# Christoph Lameter, <clameter@debian.org> October 10, 1996
#
# All the parameters are documentation files to be installed.
# (but doc files can also be listed in debian/docs)

# Global Variables

DATE="`822-date`"

PDIR="`pwd`"

OS="`uname`"
if [ "$OS" = "Linux" ]; then
  OSNAME="GNU/Linux"
  OS="GNU"
elif [ "$OS" = "GNU" ]; then
  OSNAME="GNU/Hurd"
else
  OSNAME="$OS"
fi

# Figure out package info we need
LINE=`head -1 debian/changelog`
PACKAGE=`expr "$LINE" : '\(.*\) (.*)'`
VERSION=`expr "$LINE" : '.* (\(.*\))'`

if [ "$LINE" = "" ]; then
  echo "Sorry debstd must be invoked from a debian source archive."
fi

set -- `getopt pmcus $*`
if test $? != 0; then
  echo "Syntax error"
  exit 1
fi

for i;do
  case "$i" in
  -p) PERMS=1;shift
    ;;
  -u) UNDOC=1;shift
    ;;
  -s) SUMS=1;shift
    ;;
  -m) NOAUTOMAN=1;shift
    ;;
  -c) NOCOMPRESS=1;shift
    ;;
  --) shift;break
    ;;
  esac
done

# Tolerate old style debstd invocations
if [ "$PACKAGE" = "$1" ]; then
  echo "Warning: First parameter of debstd is no longer the packagename!"
  shift
fi

# Is this a native Debian package?
if ! expr "$VERSION" : '.*-' >/dev/null; then
  NATIVE=1
fi

# Subroutines

checksums()
{
  pushd $1 >/dev/null
  find . -type f ! -regex "./DEBIAN/.*" -print0 | xargs -0 md5sum | sed -e '/\.\//s///' > DEBIAN/md5sums
  chmod 644 DEBIAN/md5sums
  popd >/dev/null
}

# addscript customizes a given script and adds it to one of the regular
# installation scripts. If the script does not exist then it is generated.
# Parameters:
# $1=scriptname to be appended to in debian/tmp/DEBIAN
addscript()
{
  # If there is no script yet then put in the proper header
  if [ ! -f $CTEMP/DEBIAN/$1 ]; then
    install -p /usr/lib/deb-make/emptyscript $CTEMP/DEBIAN/$1
  else
    if ! expr "`head -1 $CTEMP/DEBIAN/$1`" : "#!/bin/.*sh" >/dev/null ; then
      head -1 $CTEMP/DEBIAN/$1
      echo "Warning: Script $1 does not use /bin/??sh as its interpreter"
    fi
  fi
  # Make sure the files given are separated by blanks!
  FILES=`echo $FILES | tr "\n" " "`
  # Customize the script and add to the script indicated
  sed >>$CTEMP/DEBIAN/$1  -e "s/#PACKAGE#/$PACKAGE/g" \
                -e "s/#DATE#/$DATE/g" \
    -e "s/#INITPARAMS#/$INITPARAMS/g" \
    -e "s/#SCRIPT#/$SCRIPT/g" \
    -e "s/#DESCR#/\"$DESCR\"/g" \
    -e "s/#SECTION_NAME#/\"$SECTION_NAME\"/g" \
    -e "s/#SECTION_MATCH#/\"$SECTION_MATCH\"/g" \
    -e "s&#FILE#&$FILEX&g" \
    -e "s&#PACKAGE#&$CPACKAGE&g" \
    -e "s&#OWNER#&$OWNER&g" \
    -e "s&#GROUP#&$GROUP&g" \
    -e "s&#PERMS#&$PERMS&g" \
    -e "s&#FILES#&$FILES&g"
}


# debinit handles the installation of an init.d script
# Parameters:
# $1= name in /etc/init.d
# $2 = scriptname
debinit()
{
  SCRIPT=$1
  install -d $CTEMP/etc/init.d
  install -p $2 $CTEMP/etc/init.d/$SCRIPT
  INITPARAMS=`grep "^FLAGS=" $2`
  if [ "$INITPARAMS" != "" ]; then
    INITPARAMS=`expr "$INITPARAMS" : 'FLAGS="\(.*\)"'`
  fi
  if [ "$INITPARAMS" = "" ]; then
    INITPARAMS="defaults"
  fi
  echo "-- Adding /etc/init.d maintenance commands for $SCRIPT $INITPARAMS"
  if grep -q NO_RESTART_ON_UPGRADE $2; then
    addscript postrm </usr/lib/deb-make/init.postrm
    addscript postinst </usr/lib/deb-make/init.postinst.norestart
  else
    # Add the scripts necessary to manage the init.d script
    addscript postrm </usr/lib/deb-make/init.postrm
    addscript postinst </usr/lib/deb-make/init.postinst
    addscript prerm </usr/lib/deb-make/init.prerm
  fi
}

dive()
{
  addscript preinst <<EOF
dpkg-divert --package $CPACKAGE --add --rename \\
                --divert $2 $1
EOF
  addscript postrm <<EOF
dpkg-divert --package $CPACKAGE --remove --rename \\
                --divert $2 $1
EOF
}

diversion()
{
   addscript postrm <<EOF
# diversion added by debstd $DATE
if [ "\$1" = "remove" ]; then
EOF
   while [ 1 ]; do
  read X
    if [ "$X" = "" ]; then
    echo "fi"|addscript postrm
    return
     else
    dive $X
    fi
   done
}

inetd_processing()
{
  echo "-- inetd.conf processing"
  SECT=""
  read X
  while [ "$X" != "" ]; do
    SERVICE="`expr "$X" : "\(\w*\)"`"
    if expr "$X" : "#:.*:" >/dev/null; then
      SECT="`expr "$X" : "#:\(.*\):"`"
    else
      echo "update-inetd --comment-chars \"#disabled#\" --disable $SERVICE" | addscript postinst
      if [ "$SECT" ]; then
        echo "update-inetd --group $SECT --add \"$X\"" | addscript postinst
      else
        echo "update-inetd --add \"$X\"" | addscript postinst
      fi
      echo "update-inetd --remove \"^$SERVICE\"" | addscript postrm
      echo "update-inetd --comment-chars \"#disabled#\" --enable $SERVICE" | addscript postrm
    fi
    read X
  done
}

#
# Move a list of files given in a file $1 into the tmp archive
# at directory $2. Only do it if $1 exists
#
copyfiles()
{
  if [ -f $1 ]; then
    install -d $CTEMP/$2
    X=`cat $1`
    for i in $X; do
      cp -a $PDIR/$i $CTEMP/$2
    done
  fi
}

setperms()
{
  OWNER=$2
  GROUP=$3
  PERMS=$4
}


#
# Register a suid or gid binary using scripts
#
# One parameter which is the file to be processed
sreg()
{
# Figure out current permissions
  setperms `find $CTEMP/$1 -printf "%p %u %g %m\n"`
  FILEX=$1
  addscript postinst </usr/lib/deb-make/suid.postinst
  addscript postrm </usr/lib/deb-make/suid.postrm
  chmod ug-s $CTEMP/$1
}

#
# Package specific things
#
# The first parameter is the package name
# The second parameter is the directory name of the temp directory
# The third parameter is the prefix for all configuration files to be processed
package()
{
  local i
  local X Y
  CPACKAGE=$1
  CTEMP=$2
  install -d $2/DEBIAN

  # Installation scripts
  for i in postinst preinst prerm postrm configure; do
    if [ -f $3$i ]; then
      install -p $3$i $2/DEBIAN/$i
    fi
  done

  if [ -f $3shlibs ]; then
    install -p -m644 $3shlibs $2/DEBIAN/shlibs
  fi

  # Check for SUIDs and GID bits
  if [ -f $3suid ]; then
    X=`cat $3suid`
    if [ "$X" -a "$X" != "off" ]; then
      echo "-- Manual Suid/gid bit processing"
      for i in $X; do
        (cd $2;ls -l $i)
        sreg $i
      done
    fi
  else
    X=`find $2 -type f -perm +6000`
    if [ "$X" ]; then
      echo "-- Automatic Suid/gid bit processing"
      for i in $X; do
        Y=`expr "$i" : "$2/\(.*\)"`
        (cd $2;ls -l $Y)
        sreg $Y
      done
    fi
  fi

  # Check up on stuff in etc directory
  if [ -d $2/etc ]; then
    if [ -f $3conffiles ]; then
      for i in `cd $2;find etc -type f`; do
        if ! grep -q $i $3conffiles; then
          echo "Warning: File $i provided in binary but is not
listed as a conffile!"
        fi
      done
    else
      echo "Warning: Files installed in etc and no conffile!"
    fi
  fi

  # Deal with scripts in etc directories
  for i in cron.daily cron.weekly cron.monthly rc.boot; do
    if [ -f $3$i ]; then
      install -d $2/etc/$i
      install -p $3$i $2/etc/$i/$1
    fi
  done

  # etc files that could need some tweaking
  for i in services inittab crontab protocols profile shells rpc \
    syslog.conf conf.modules modules aliases \
    X11/Xresources X11/config X11/window-managers X11/xinit ; do
    if [ -f $3$i ]; then
      FILEX="$i"
      echo ===========================================================
      echo WARNING: This package tries to modify /etc/$FILEX.
      echo This might be not ok according to policy.
      echo ===========================================================
      cat /usr/lib/deb-make/etc.first.postinst $3$i \
        /usr/lib/deb-make/etc.last.postinst | addscript postinst
      addscript postrm </usr/lib/deb-make/etc.postrm
      if [ "$i" = "aliases" ]; then
        echo 'newaliases || echo "newaliases command not available"' | addscript postinst
        echo 'newaliases || echo "newaliases command not available"' | addscript postrm
      fi
      if [ "$i" = "services" ]; then
        echo -e "Warning: Please submit modifications to /etc/services to the maintainer\n  of the netbase package"
      fi
    fi
  done

  if [ -f $3diversions ]; then
    diversion <$3diversions
  fi

  if [ -f $3inetd.conf ]; then
    inetd_processing <$3inetd.conf
  fi

  if [ -f $3init.d ]; then
    debinit $1 $3init.d
  fi

  # The case of a daemon without the final d
  if [ -f $3init ]; then
    X=`expr $1 : '\(.*\)d$'`  
          if [ "$X" ]; then
      debinit $X $3init
          else
      echo "Error: debian/init file skipped since packagename has no final d"
    fi
  fi

  # Purging files/directories on "purge" ?

  if [ -f $3purge ]; then
    echo "-- Adding purge actions to postrm"
    FILES="`cat $3purge`"
    addscript postrm </usr/lib/deb-make/purge.postrm
  fi

  if [ -f $3conffiles ]; then
    install -p -m644 $3conffiles $2/DEBIAN/conffiles
    for i in `cat $3conffiles`; do
      if [ ! -e $2/$i ]; then
        echo "Warning: Configuration file $i not provided in binary package!"
      fi
    done
  fi

  copyfiles $3docs usr/doc/$PACKAGE

  copyfiles $3examples usr/doc/$PACKAGE/examples

  if [ -d $2/usr/doc ]; then
    chown -R root.root $2/usr/doc

    find $2/usr/doc -type f -print0 | xargs -0 chmod 644
    find $2/usr/doc -type d -print0 | xargs -0 chmod 755
  fi
  
  if [ -f $3menu ]; then
    install -d $2/usr/lib/menu
    install -p -m644 $3menu $2/usr/lib/menu/$CPACKAGE
    addscript postinst </usr/lib/deb-make/menu.postinstrm
    addscript postrm </usr/lib/deb-make/menu.postinstrm
  fi

  # Info files?
  if [ -f $3info ]; then
    echo "-- Installing info files"
    FILES=""
    . ./$3info
    FILEX="$FILE"
    if [ "$FILES" ]; then
      install -d $2/usr/info
      mkdir /tmp/info$$
      (cd $PDIR;cp $FILES /tmp/info$$)
      (cd $2/usr/info;find /tmp/info$$ -type f -exec cp '{}' . \;)
      rm -rf /tmp/info$$
    fi
    if [ "$SECTION_MATCH" = "" ]; then
      addscript postinst </usr/lib/deb-make/info.postinst
    else
      addscript postinst </usr/lib/deb-make/info.regexp.postinst
    fi
    addscript prerm </usr/lib/deb-make/info.prerm
  fi
    # Check for libraries provided by the package
  SHLIBS="`find $2/lib $2/usr/lib $2/usr/X11R6/lib $2/usr/lib/libc5-compat -type f -name "lib*.so.*" 2>/dev/null`"
  X=`cd $2;find lib usr/lib usr/X11R6/lib usr/lib/libc5-compat -type f -name "lib*.so.*" 2>/dev/null`
  if [ "$X" != "" -a ! -e $3shlibs ]; then
    echo "-- Processing provided ELF libraries"
#   >debian/shlibs.local
    for i in $X; do
      echo "Sharable Library $i"
      LIBRARY=`expr $i : ".*/\(.*\)\.so\..*"`
      VERSION=`expr $i : ".*/.*\.so\.\(.*\)"`
      MAJOR=`expr $VERSION : "\([0-9a-z]*\)\."`
      LIBSTUB=`expr $i : "\(.*\/.*\.so\)\..*"`
      echo "$LIBRARY $MAJOR $1" >>$2/DEBIAN/shlibs
#     echo "$LIBRARY $MAJOR" >>debian/shlibs.local
      if [ "$OS" = "GNU" ]; then
        strip --strip-unneeded $2/$i
      fi
      if [ ! -L $2$LIBSTUB.$MAJOR ]; then
        ln -s $LIBRARY\.so.$VERSION $2/$LIBSTUB.$MAJOR
        echo "Added ELF Library link $LIBRARY.so.$MAJOR"
      fi
#     if [ ! -L $PDIR/debian/tmp/$LIBSTUB ]; then
#       ln -s $LIBRARY\.so.$VERSION $PDIR/debian/tmp/$LIBSTUB
#       echo "Added ELF Library link $LIBRARY.so (in base package)"
#     fi
#     if [ ! -L $2$LIBSTUB ]; then
#       ln -s $LIBRARY\.so.$VERSION $2$LIBSTUB
#       echo "Added ELF Library link $LIBRARY.so"
#     fi
        
    done
    addscript postinst < /usr/lib/deb-make/ldconfig.postinst
  fi
    # Check for static libraries provided by the package
  X=`find $2/lib $2/usr/lib $2/usr/X11R6/lib $2/usr/lib/libc5-compat $2/usr/*-linuxlibc1/lib -type f -name "lib*.a" 2>/dev/null`
  if [ "$X" != "" ]; then
    echo "-- Processing provided static libraries"
    for i in $X; do
      echo "Static Library $i"
      if ! expr "$i" : ".*_g\.a" >/dev/null; then
        if [ "$OS" = "GNU" ]; then
          strip --strip-debug $i
        fi
      fi
    done
  fi


  # Check for executables to run dpkg-shlibdeps
  echo "-- Checking for executable binaries in package"
  X=`find $2 -type f -perm +111| tr "\n" " "`
  if [ "$X" = "" ]; then
    if [ "$SHLIBS" = "" ]; then
      echo "No executables or libraries in the package!"
    else
      echo "-- Figuring out dependencies on ELF Libraries"
      dpkg-shlibdeps -T$3substvars $SHLIBS
    fi
  else
    DEPFILE=`tempfile`
    for i in $X; do
      BINPATH="`expr "$i" : "$2/\(.*\)/.*"`"
      BINNAME="`expr "$i" : "$2/.*/\(.*\)"`"
#     echo  "I=$i 2=$2 PATH=$BINPATH NAME=$BINNAME"
      case "`file $i`" in
        *ELF*)
          if [ ! -f $3nodeps ]; then
            echo $i >> $DEPFILE
          else
            if grep -q "$BINPATH/$BINNAME" $3nodeps; then
              echo -n "No Dependencies for "
            else
              echo $i >> $DEPFILE
            fi
          fi
          echo "$BINPATH/$BINNAME ELF Binary"
          if [ "$OS" = "GNU" ]; then
            strip --remove-section=comment --remove-section=note $i
          else
            strip $i
          fi
          ;;
        *script*)
          echo "$BINPATH/$BINNAME Script"
          ;;
        *perl*) 
          echo "$BINPATH/$BINNAME PERL"
          ;;
        *)  echo "$BINPATH/$BINNAME WARNING: Type unusual: `file $i`"
      esac
      # Check if manpages exist
      case "$BINPATH" in
        DEBIAN|etc/rc.boot|usr/lib/cgi-bin|etc/init.d|etc/cron.*|usr/lib/lib*|usr/lib/*)  SECTION=""
          ;;
        sbin|usr/sbin) SECTION="8"
          ;;
        usr/X11R6/bin) SECTION="1"
          ;;
        bin|usr/bin) SECTION="1"
          ;;
        usr/games) SECTION="6"
          ;;
        *)  SECTION=""
          echo "Warning: Unusual executable location $BINPATH/$BINNAME"
          ;;
      esac
      if [ "$SECTION" ]; then
        Y=`find $2/usr/man $2/usr/X11R6/man -name "$BINNAME.*" 2>/dev/null`
        if [ "$Y" = "" ]; then
          if [ "$UNDOC" ]; then
            if [ "$BINPATH" = "usr/X11R6/bin" ]; then
              MANDIR="$2/usr/X11R6/man/man$SECTION"
              MANLINK="../../../man/man7/undocumented.7.gz"
            else
              MANDIR="$2/usr/man/man$SECTION"
              MANLINK="../man7/undocumented.7.gz"
            fi
            install -d $MANDIR
            ln -s $MANLINK $MANDIR/$BINNAME.$SECTION.gz
            echo "$BINPATH/$BINNAME has no manpage. Link to undocumented.7.gz added."
          else
            echo "Warning : Executable $BINPATH/$BINNAME has no manpage!"
          fi
        fi
      fi
    done
    X="`cat $DEPFILE`"
    rm $DEPFILE
    if [ "$X" = "" ]; then
      if [ "$SHLIBS" ]; then
        echo "-- Figuring out dependencies of ELF libaries"
        dpkg-shlibdeps -T$3substvars $SHLIBS
      fi
    else
      echo "-- Figuring out dependencies of ELF objects"
      dpkg-shlibdeps -T$3substvars -Ldebian/shlibs.local $X $SHLIBS
    fi
  fi

  pushd $2 >/dev/null
  # Compress manpages. Convert manpages that have just .so in them to symlinks
  if [ -d usr/man -o -d usr/X11*/man ]; then
    echo "-- Compressing/Converting Manpages"
    X=`find usr/man usr/X11*/man -type f -size -50c 2>/dev/null`
    if [ "$X" ]; then
        for i in $X; do
        Y=`head -1 $i`
        Y=`expr "$Y" : '\.so \(.*\)'`     
        if [ "$Y" != "" ]; then
          # Convert it to a symlink
          rm $i
          ln -s ../$Y $i
          echo "Manpage $i converted into symlink (../$Y)"
        else
          echo "Manpage $i does not seem to contain .so although smaller than 50 bytes."
          echo "Something might be wrong here!"
        fi
      done
    fi
    X=`find usr/man usr/X11*/man -type f 2>/dev/null`
    if [ "$X" ]; then
      gzip -9 $X
    else
      echo "WARNING: /usr/man exists but no manpages"
    fi
  fi

  # Compress Info
  if [ -d usr/info ]; then
    echo "-- Compressing Info files"
    X="`find usr/info -type f`"
    if [ "$X" ]; then
      gzip -9 $X
    else
      echo "WARNING: /usr/info exists but no content"
    fi
  fi

  # Compress Documentation
  if [ -d usr/doc -a "$NOCOMPRESS" = "" ]; then
    echo "-- Compressing Documentation"
    find usr/doc -type f \
    \( -size +4k -or -name "changelog.Debian" -or -name "changelog" \) \
    ! -name "*.htm*" ! -name "*.gif" ! -name "*.jpg" ! -name "*.jpeg" \
    ! -name "*.css" ! -name "copyright" -print0 | xargs -0 gzip -9
  fi

  # Ensure that the symlinks survived the cure and point them to the gzipped
  # file if necessary
  echo "-- Checking Symlinks"
  OLDIFS="$IFS"
  IFS="
" # This is a newline.
  for i in `find . -type l`; do
    DIRECTORY=`expr $i : "\(.*\)/[^/]*"`
    NAME=`expr $i : ".*/\([^/]*\)"`
    LINKVAL=`find "$DIRECTORY/$NAME" -printf "%l"`
    if [ ! -e $DIRECTORY/$LINKVAL ]; then
      if [ -f $DIRECTORY/$LINKVAL.gz ]; then
        rm $DIRECTORY/$NAME
        ln -s $LINKVAL.gz $DIRECTORY/$NAME.gz
        echo "Changed symlink $i to $i.gz"
      else
        echo "Dangling symlink $i pointing to $LINKVAL"
      fi
    fi
  done
  IFS="$OLDIFS"

  # Some checks
  if [ -d usr/local ]; then
    echo "Warning: usr/local directory exists in $CPACKAGE!"
  fi

  if [ "$PERMS" ]; then
    X=`find . \( -uid +99 -and -uid -60000 \) -or \
              \( -gid +99 -and -gid -60000 \)`
    if [ "$X" ]; then
      for i in $X; do
        echo "Warning: Files $i has illegal user or group id in package $CPACKAGE"
      done
    fi
  fi

  # Check scripts for bashisms
  for i in preinst postinst prerm postrm; do
    if [ -f DEBIAN/$i ]; then if expr "`head -1 DEBIAN/$i`" : "#!/bin/sh" >/dev/null; then
#     echo "$i uses #!/bin/sh Checking it"
      if ! checkbash DEBIAN/$i; then
        if [ ! -f $PDIR/$3$i ]; then
          ed -s DEBIAN/$i <<EOF
1d
1i
#!/bin/bash -e
.
wq
EOF
        echo "Debmake generated Script changed to use #!/bin/bash"
        fi
      fi
    fi fi
  done
  # Add link to proper document directory
# if [ ! -d usr/doc/$CPACKAGE -a ! -L usr/doc/$CPACKAGE ]; then
#   ln -s $PACKAGE usr/doc/$CPACKAGE;
# fi
  popd >/dev/null
}


packages()
{
  local i
  BASE=$1
  shift
  for i in $*; do
    install -d debian/$i
    echo "** Package $i"
    if [ -f debian/$i.files ]; then
      echo "-- Moving files from debian/tmp into $i"
  (cd debian/tmp;tar --create --remove-files --file - `cat ../$i.files`) \
  | (cd debian/$i;tar xpf -)
    fi
    package $i debian/$i "debian/$i."
    dpkg-gencontrol -isp -p$i -Pdebian/$i -Tdebian/$i.substvars
    chown -R root.root debian/$i
    chmod -R go=rX debian/$i
    if [ -x debian/$i.prebuild ]; then
      (cd debian/$i;../$i.prebuild)
    fi
    if [ "$SUMS" = "" ]; then
      checksums debian/$i
    fi
    dpkg --build debian/$i ..
  done
  if [ -f debian/clean ]; then
    # Try to clean up base package
    find debian/tmp -type d -links 2 -exec rmdir '{}' \; 2>/dev/null
    find debian/tmp -type d -links 2 -exec rmdir '{}' \; 2>/dev/null
  fi
  echo "** Main Package $BASE"
  package $BASE debian/tmp "debian/"
  if [ "$SUMS" = "" ]; then
    checksums debian/tmp
  fi
}

#
# Things uniquely done to the base package
#

echo "------------- debstd processing for $OSNAME --------------"
cd debian

install -d -m755 tmp/usr/doc/$PACKAGE
# Required files in /usr/doc/package
if [ ! -e tmp/usr/doc/$PACKAGE/copyright ]; then
  install -p -m644 copyright tmp/usr/doc/$PACKAGE
fi

if [ "$NATIVE" ]; then
  install -p -m644 changelog tmp/usr/doc/$PACKAGE
else
  if [ ! -f tmp/usr/doc/$PACKAGE/changelog.Debian -a ! -f tmp/usr/doc/$PACKAGE/changelog.Debian.gz ]; then
    install -p -m644 changelog tmp/usr/doc/$PACKAGE/changelog.Debian
  fi
fi

# Special README
if [ -f README.debian ]; then
  install -p -m644 README.debian tmp/usr/doc/$PACKAGE/README.debian
fi

# Special TODO
if [ -f TODO ]; then
  install -p -m644 TODO tmp/usr/doc/$PACKAGE/TODO
fi

# Record the Info of the machine the package was build on
# dpkg -l gcc 'libc*' debmake binutils ldso make dpkg-dev | awk '$1 ~ /^.i/ { printf("%s-%s\n", $2, $3) }' >tmp/usr/doc/$PACKAGE/buildinfo.Debian

cd ..
# Install Documentation files specified on the commandline
echo "-- Installing Documentation"

# Special case of changelog
if [ "$1" ]; then
  if echo "$1" | egrep -qi "change|news|history" ; then
    install -p -m644 $1 debian/tmp/usr/doc/$PACKAGE/changelog
    shift
  fi
fi

while [ "$1" != "" ]; do
  install -p -m644 $1 debian/tmp/usr/doc/$PACKAGE
  shift
done

# Manpage scan
if [ "$NOAUTOMAN" = "" ]; then
  echo "-- Looking for manpages in sourcepackage"
  # Install manpages that might be somewhere in the source package
  for i in `find . -name "*.[1-9]*" ! -name "*.ex" ! -name "*.in"`; do
    if ! expr $i : 'debian/tmp/.*' >/dev/null; then
      if file $i|grep -q roff; then
          if echo $i|grep -q /; then
          NAME=`expr $i : '.*/\(.*\)'`
        else
          NAME=$i
        fi
        SECTION=man`expr $NAME : '.*\.\([123456789]\)'`
        if [ -e debian/tmp/usr/man/$SECTION/$NAME -o -e debian/tmp/usr/X11*/man/$SECTION/$NAME ]; then
          echo "Manpage $NAME exists.";
        else
          if [ ! -d debian/tmp/usr/man/$SECTION ]; then
            install -d debian/tmp/usr/man/$SECTION
          fi
          install -p -m644 $i debian/tmp/usr/man/$SECTION/$NAME
          echo -n "$NAME "
        fi
      fi
    fi
  done
  echo
fi

# Create the binary packages
PACKAGES=`grep "Package:" debian/control | sed -e 's/Package: //g'|tr "\n" " "`

if [ "$PACKAGES" ]; then
  packages $PACKAGES
else
  echo "No Packages specified in debian/control"
fi

if ! grep -q "^SHELL" debian/rules && ! checkbash debian/rules; then
  echo "WARNING: debian/rules file potentially contains a bashism but has no SHELL = ? statement."
  echo "It might be wise to add a line SHELL = /bin/bash to debian/rules"
fi

echo "--------------- debstd processing finished --------------"

exit 0