DebChangelogUpdate.dpr 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. program DebChangelogUpdate;
  2. {$APPTYPE CONSOLE}
  3. {$R 'ExecutionLevelAsInvokerManifest.res' 'ExecutionLevelAsInvokerManifest.rc'}
  4. uses
  5. Windows,
  6. Classes,
  7. SysUtils;
  8. type
  9. EIdFailedToRetreiveTimeZoneInfo = class(Exception);
  10. const
  11. RSFailedTimeZoneInfo = 'Failed to obtain timezone';
  12. const
  13. wdays: array[1..7] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); {do not localize}
  14. monthnames: array[1..12] of string = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', {do not localize}
  15. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); {do not localize}
  16. procedure WriteStringToStream(AStream: TStream; const AStr: string);
  17. var LLen : Integer;
  18. begin
  19. LLen := Length(AStr);
  20. if LLen>0 then
  21. begin
  22. AStream.Write(AStr[1],LLen);
  23. end;
  24. end;
  25. function GetEnglishSetting: TFormatSettings;
  26. inline;
  27. begin
  28. Result.CurrencyFormat := $00; // 0 = '$1'
  29. Result.NegCurrFormat := $00; //0 = '($1)'
  30. Result.CurrencyString := '$';
  31. Result.CurrencyDecimals := 2;
  32. Result.ThousandSeparator := ',';
  33. Result.DecimalSeparator := '.';
  34. Result.DateSeparator := '/';
  35. Result.ShortDateFormat := 'M/d/yyyy';
  36. Result.LongDateFormat := 'dddd, MMMM dd, yyyy';
  37. Result.TimeSeparator := ':';
  38. Result.TimeAMString := 'AM';
  39. Result.TimePMString := 'PM';
  40. Result.LongTimeFormat := 'h:mm:ss AMPM';
  41. Result.ShortTimeFormat := 'h:mm AMPM';
  42. Result.ShortMonthNames[1] := monthnames[1]; //'Jan';
  43. Result.ShortMonthNames[2] := monthnames[2]; //'Feb';
  44. Result.ShortMonthNames[3] := monthnames[3]; //'Mar';
  45. Result.ShortMonthNames[4] := monthnames[4]; //'Apr';
  46. Result.ShortMonthNames[5] := monthnames[5]; //'May';
  47. Result.ShortMonthNames[6] := monthnames[6]; //'Jun';
  48. Result.ShortMonthNames[7] := monthnames[7]; //'Jul';
  49. Result.ShortMonthNames[8] := monthnames[8]; //'Aug';
  50. Result.ShortMonthNames[9] := monthnames[9]; //'Sep';
  51. Result.ShortMonthNames[10] := monthnames[10];// 'Oct';
  52. Result.ShortMonthNames[11] := monthnames[11]; //'Nov';
  53. Result.ShortMonthNames[12] := monthnames[12]; //'Dec';
  54. Result.LongMonthNames[1] := 'January';
  55. Result.LongMonthNames[2] := 'February';
  56. Result.LongMonthNames[3] := 'March';
  57. Result.LongMonthNames[4] := 'April';
  58. Result.LongMonthNames[5] := 'May';
  59. Result.LongMonthNames[6] := 'June';
  60. Result.LongMonthNames[7] := 'July';
  61. Result.LongMonthNames[8] := 'August';
  62. Result.LongMonthNames[9] := 'September';
  63. Result.LongMonthNames[10] := 'October';
  64. Result.LongMonthNames[11] := 'November';
  65. Result.LongMonthNames[12] := 'December';
  66. Result.ShortDayNames[1] := wdays[1]; //'Sun';
  67. Result.ShortDayNames[2] := wdays[2]; //'Mon';
  68. Result.ShortDayNames[3] := wdays[3]; //'Tue';
  69. Result.ShortDayNames[4] := wdays[4]; //'Wed';
  70. Result.ShortDayNames[5] := wdays[5]; //'Thu';
  71. Result.ShortDayNames[6] := wdays[6]; //'Fri';
  72. Result.ShortDayNames[7] := wdays[7]; //'Sat';
  73. Result.LongDayNames[1] := 'Sunday';
  74. Result.LongDayNames[2] := 'Monday';
  75. Result.LongDayNames[3] := 'Tuesday';
  76. Result.LongDayNames[4] := 'Wednesday';
  77. Result.LongDayNames[5] := 'Thursday';
  78. Result.LongDayNames[6] := 'Friday';
  79. Result.LongDayNames[7] := 'Saturday';
  80. Result.ListSeparator := ',';
  81. end;
  82. function IndyFormat(const AFormat: string; const Args: array of const): string;
  83. begin
  84. Result := SysUtils.Format(AFormat, Args, GetEnglishSetting);
  85. end;
  86. function DateTimeToGmtOffSetStr(ADateTime: TDateTime; SubGMT: Boolean): string;
  87. var
  88. AHour, AMin, ASec, AMSec: Word;
  89. begin
  90. if (ADateTime = 0.0) and SubGMT then
  91. begin
  92. Result := 'GMT'; {do not localize}
  93. Exit;
  94. end;
  95. DecodeTime(ADateTime, AHour, AMin, ASec, AMSec);
  96. Result := IndyFormat(' %0.2d%0.2d', [AHour, AMin]); {do not localize}
  97. if ADateTime < 0.0 then begin
  98. Result[1] := '-'; {do not localize}
  99. end else begin
  100. Result[1] := '+'; {do not localize}
  101. end;
  102. end;
  103. function OffsetFromUTC: TDateTime;
  104. var
  105. iBias: Integer;
  106. tmez: TTimeZoneInformation;
  107. begin
  108. case GetTimeZoneInformation(tmez) of
  109. TIME_ZONE_ID_INVALID:
  110. begin
  111. raise EIdFailedToRetreiveTimeZoneInfo.Create(RSFailedTimeZoneInfo);
  112. end;
  113. TIME_ZONE_ID_UNKNOWN :
  114. iBias := tmez.Bias;
  115. TIME_ZONE_ID_DAYLIGHT :
  116. iBias := tmez.Bias + tmez.DaylightBias;
  117. TIME_ZONE_ID_STANDARD :
  118. iBias := tmez.Bias + tmez.StandardBias;
  119. else
  120. begin
  121. raise EIdFailedToRetreiveTimeZoneInfo.Create(RSFailedTimeZoneInfo);
  122. end;
  123. end;
  124. {We use ABS because EncodeTime will only accept positive values}
  125. Result := EncodeTime(Abs(iBias) div 60, Abs(iBias) mod 60, 0, 0);
  126. {The GetTimeZone function returns values oriented towards converting
  127. a GMT time into a local time. We wish to do the opposite by returning
  128. the difference between the local time and GMT. So I just make a positive
  129. value negative and leave a negative value as positive}
  130. if iBias > 0 then begin
  131. Result := 0 - Result;
  132. end;
  133. end;
  134. {This should never be localized}
  135. function DateTimeToInternetStr(const Value: TDateTime; const AIsGMT : Boolean = False) : String;
  136. var
  137. wDay,
  138. wMonth,
  139. wYear: Word;
  140. begin
  141. DecodeDate(Value, wYear, wMonth, wDay);
  142. Result := IndyFormat('%s, %d %s %d %s %s', {do not localize}
  143. [wdays[DayOfWeek(Value)], wDay, monthnames[wMonth],
  144. wYear, FormatDateTime('HH":"nn":"ss', Value), {do not localize}
  145. DateTimeToGmtOffSetStr(OffsetFromUTC, AIsGMT)]);
  146. end;
  147. var GFileName : String;
  148. GVer : String;
  149. GFileContents : TStrings;
  150. GPkgName : String;
  151. i : Integer;
  152. GF : TFileStream;
  153. begin
  154. try
  155. { TODO -oUser -cConsole Main : Insert code here }
  156. //parameters
  157. if ParamCount < 5 then
  158. begin
  159. WriteLn(ExtractFileName(ParamStr(0))+' dir major_ver minor_ver point_ver');
  160. WriteLn('');
  161. WriteLn('dir - directory changlog is located in');
  162. WriteLn('pkg_name = name of Debian package');
  163. WriteLn('major_ver - major version of package');
  164. WriteLn('minor_ver - minor version of package');
  165. WriteLn('point_ver - point version of package');
  166. ReadLn;
  167. Exit;
  168. end
  169. else
  170. begin
  171. GFileName := ParamStr(1)+'\changelog';
  172. end;
  173. GPkgName := ParamStr(2);
  174. GVer := IntToStr(StrToIntDef(ParamStr(3),0));
  175. GVer := GVer + '.' + IntToStr(StrToIntDef(ParamStr(4),0));
  176. GVer := GVer + '.0.' + IntToStr(StrToIntDef(ParamStr(5),0));
  177. GFileContents := TStringList.Create;
  178. try
  179. GFileContents.LoadFromFile(GFileName);
  180. if Pos(GPkgName + ' ('+GVer,GFileContents.Text) = 0 then
  181. begin
  182. GFileContents.Insert(0,'');
  183. GFileContents.Insert(0,'');
  184. GFileContents.Insert(0,' -- J. Peter Mugaas <[email protected]> ' + DateTimeToInternetStr(Now) );
  185. GFileContents.Insert(0,'');
  186. GFileContents.Insert(0,' * '+GVer);
  187. GFileContents.Insert(0,'');
  188. GFileContents.Insert(0,GPkgName+' ('+GVer+'-1) unstable; urgency=low');
  189. GF := TFileStream.Create(GFileName,fmCreate,fmShareExclusive);
  190. //We don't use GFileContents.SaveToFile because we want a LF line-ending
  191. //not a CRLF. After all, the Debian changelog is for Unix-like operating systems
  192. try
  193. for i := 0 to GFileContents.Count - 1 do
  194. begin
  195. WriteStringToStream(GF,TrimRight(GFileContents[i])+#10);
  196. end;
  197. finally
  198. FreeAndNil(GF);
  199. end;
  200. WriteLn('changelog updated.')
  201. end
  202. else
  203. begin
  204. WriteLn('changelog did not need update.');
  205. end;
  206. finally
  207. FreeAndNil(GFileContents);
  208. end;
  209. except
  210. on E:Exception do
  211. Writeln(E.Classname, ': ', E.Message);
  212. end;
  213. end.